Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix signature help when cursor is after the lparen of the function call #1718

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/features/signature_help.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ pub fn getSignatureInfo(analyser: *Analyser, arena: std.mem.Allocator, handle: *
// to scan up to find a function or builtin call
const first_token = tree.firstToken(innermost_block);
// We start by finding the token that includes the current cursor position
const last_token = offsets.sourceIndexToTokenIndex(tree, absolute_index);
const last_token = blk: {
const last_token = offsets.sourceIndexToTokenIndex(tree, absolute_index);
switch (token_tags[last_token]) {
.l_brace, .l_paren, .l_bracket => break :blk last_token,
else => break :blk last_token - 1,
}
};

// We scan the tokens from last to first, adding and removing open and close
// delimiter tokens to a stack, while keeping track of commas corresponding
Expand Down
44 changes: 42 additions & 2 deletions tests/lsp_features/signature_help.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,31 @@ test "signature help - simple" {
\\ foo(<cursor>)
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(<cursor>,0)
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(0,<cursor>)
\\}
, "fn foo(a: u32, b: u32) void", 1);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(0,<cursor>55)
\\}
, "fn foo(a: u32, b: u32) void", 1);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(0,5<cursor>5)
\\}
, "fn foo(a: u32, b: u32) void", 1);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(0,55<cursor>)
\\}
, "fn foo(a: u32, b: u32) void", 1);
}

test "signature help - syntax error resistance" {
Expand All @@ -35,14 +55,34 @@ test "signature help - syntax error resistance" {
\\ foo(<cursor>
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(5<cursor>
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(5<cursor>5
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(<cursor>55
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(<cursor>;
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(<cursor>);
\\ foo(<cursor>,
\\}
, "fn foo(a: u32, b: u32) void", 0);
try testSignatureHelp(
\\fn foo(a: u32, b: u32) void {
\\ foo(<cursor>,;
\\}
, "fn foo(a: u32, b: u32) void", 0);
}
Expand Down Expand Up @@ -204,7 +244,7 @@ fn testSignatureHelp(source: []const u8, expected_label: []const u8, expected_ac
var ctx = try Context.init();
defer ctx.deinit();

const test_uri = try ctx.addDocument(source);
const test_uri = try ctx.addDocument(text);

const params = types.SignatureHelpParams{
.textDocument = .{ .uri = test_uri },
Expand Down