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

Inconsistent completion of function parentheses when snippets are disabled #1939

Open
castholm opened this issue Jul 4, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@castholm
Copy link

castholm commented Jul 4, 2024

Zig Version

0.13.0

Zig Language Server Version

0.13.0

Client / Code Editor / Extensions

vscode-zig

Steps to Reproduce and Observed Behavior

I have zig.zls.enableSnippets set to false in the settings. Despite this, ZLS completes function call parentheses differently depending on the signature, sometimes inserting just the function name and other times inserting the function name followed by (). This inconsistent behavior is undesirable and hurts muscle memory. I disabled snippets because I prefer just completing the names without any automatic insertion of parentheses.

fn functionZero() void {}

fn functionOne(a: i32) void {
    _ = a;
}

fn functionAnytype(a: anytype) void {
    _ = a;
}

const Foo = struct {
    fn method(foo: Foo) void {
        _ = foo;
    }
};

pub fn main() void {
    const foo: Foo = .{};
    _ = &foo;

    // Below, '_' represents the position of the caret

    // "funZ_"
    // <tab>
    // "functionZero()_"

    // "funO_"
    // <tab>
    // "functionOne_"

    // "funA_"
    // <tab>
    // "functionAnytype()_"

    // "foo.m_"
    // <tab>
    // "foo.method()_"
}

Of note is that when snippets are enabled, completion of the anytype function places the caret after the inserted () and not inbetween, so this is a bug regardless of whether the user has enabled snippets.

Expected Behavior

When snippets are disabled, only the name should be inserted. The parentheses should never be inserted regardless of whether the call takes any arguments or not.

I fixed this problem for myself locally by making the following changes to completions.zig:

diff --git a/src/features/completions.zig b/src/features/completions.zig
index 599f781..9cc4ff9 100644
--- a/src/features/completions.zig
+++ b/src/features/completions.zig
@@ -329,7 +329,8 @@ fn functionTypeCompletion(
     const new_text = switch (new_text_format) {
         .only_name => func_name,
         .snippet => blk: {
-            if (use_snippets and builder.server.config.enable_argument_placeholders) {
+            if (!use_snippets) break :blk func_name;
+            if (builder.server.config.enable_argument_placeholders) {
                 break :blk try std.fmt.allocPrint(builder.arena, "{}", .{Analyser.fmtFunction(.{
                     .fn_proto = func,
                     .tree = &tree,
@@ -358,12 +359,10 @@ fn functionTypeCompletion(
                     }
 
                     // Non-self parameter, leave the cursor in the parentheses
-                    if (!use_snippets) break :blk func_name;
                     break :blk try std.fmt.allocPrint(builder.arena, "{s}(${{1:}})", .{func_name});
                 },
                 else => {
                     // Atleast one non-self parameter, leave the cursor in the parentheses
-                    if (!use_snippets) break :blk func_name;
                     break :blk try std.fmt.allocPrint(builder.arena, "{s}(${{1:}})", .{func_name});
                 },
             }

This is enough for me but I'm not sure if it's a good general solution.

Relevant log output

No response

@castholm castholm added the bug Something isn't working label Jul 4, 2024
@nwormek
Copy link

nwormek commented Sep 2, 2024

Appearantly, that behavior is desired:

add empty parentheses when there are no arguments even if snippets are disabled

#1763

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants