Skip to content

Commit

Permalink
Put param hint on previous line for multiline string arg
Browse files Browse the repository at this point in the history
  • Loading branch information
FnControlOption authored and Techatrix committed Feb 9, 2025
1 parent 1ce6d64 commit 258de38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,15 @@ const Builder = struct {
hints: std.ArrayListUnmanaged(InlayHint) = .empty,
hover_kind: types.MarkupKind,

fn appendParameterHint(self: *Builder, token_index: Ast.TokenIndex, label: []const u8, tooltip: []const u8, tooltip_noalias: bool, tooltip_comptime: bool) !void {
fn appendParameterHint(
self: *Builder,
node_tag: Ast.Node.Tag,
token_index: Ast.TokenIndex,
label: []const u8,
tooltip: []const u8,
tooltip_noalias: bool,
tooltip_comptime: bool,
) !void {
// adding tooltip_noalias & tooltip_comptime to InlayHint should be enough
const tooltip_text = blk: {
if (tooltip.len == 0) break :blk "";
Expand All @@ -171,7 +179,10 @@ const Builder = struct {
};

try self.hints.append(self.arena, .{
.index = offsets.tokenToIndex(self.handle.tree, token_index),
.index = if (node_tag == .multiline_string_literal)
offsets.tokenToLoc(self.handle.tree, token_index - 1).end
else
offsets.tokenToIndex(self.handle.tree, token_index),
.label = try std.fmt.allocPrint(self.arena, "{s}:", .{label}),
.kind = .Parameter,
.tooltip = .{
Expand Down Expand Up @@ -276,6 +287,7 @@ fn writeCallHint(
offsets.nodeToSlice(fn_node.handle.tree, param.type_expr);

try builder.appendParameterHint(
tree.nodes.items(.tag)[arg],
tree.firstToken(arg),
parameter_name,
tooltip,
Expand Down Expand Up @@ -317,6 +329,7 @@ fn writeBuiltinHint(builder: *Builder, parameters: []const Ast.Node.Index, argum
if (label.len == 0 or std.mem.eql(u8, label, "...")) return;

try builder.appendParameterHint(
tree.nodes.items(.tag)[parameter],
tree.firstToken(parameter),
label,
std.mem.trim(u8, type_expr, " \t\n"),
Expand Down
19 changes: 19 additions & 0 deletions tests/lsp_features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ test "function call" {
, .{ .kind = .Parameter });
}

test "function call with multiline string literal" {
try testInlayHints(
\\fn foo(bar: []const u8) void {}
\\const _ = foo(<bar>
\\ \\alpha
\\ \\beta
\\);
, .{ .kind = .Parameter });
}

test "extern function call" {
try testInlayHints(
\\extern fn foo(u32, beta: bool, []const u8) void;
Expand Down Expand Up @@ -130,6 +140,15 @@ test "builtin call" {
});
}

test "builtin call with multiline string literal" {
try testInlayHints(
\\const _ = @compileError(<msg>
\\ \\foo
\\ \\bar
\\);
, .{ .kind = .Parameter });
}

test "exclude single argument" {
try testInlayHints(
\\fn func1(alpha: u32) void {}
Expand Down

0 comments on commit 258de38

Please sign in to comment.