zigtools / zls

A Zig language server supporting Zig developers with features like autocomplete and goto definition
MIT License
2.96k stars 295 forks source link

no completions after branches with no braces #2014

Closed xdBronch closed 1 month ago

xdBronch commented 2 months ago

Zig Version

0.14.0-dev.1349+6a21875dd

ZLS Version

0.14.0-dev.135+b481aae (with changes to make it compile on master)

Client / Code Editor / Extensions

No response

Steps to Reproduce and Observed Behavior

pub fn main() void {
    if (true) <cursor>
}

ask for completions and nothing happens, same with while.

Expected Behavior

completions

Relevant log output

No response

llogick commented 2 months ago

Well spotted, xd

diff --git a/src/analysis.zig b/src/analysis.zig
index 904b6f0b..ef85ed9b 100644
--- a/src/analysis.zig
+++ b/src/analysis.zig
@@ -3482,6 +3482,8 @@ pub fn getPositionContext(
                     .enum_literal => curr_ctx.ctx = .{
                         .enum_literal = tokenLocAppend(curr_ctx.ctx.loc().?, tok),
                     },
+                    // `if` or `while` condition w/out braces, e.g. `if (true) std.<cursor>`
+                    .parens_expr => curr_ctx.ctx = .{ .var_access = tok.loc },
                     else => {},
                 },
                 .builtin => curr_ctx.ctx = .{ .builtin = tok.loc },
xdBronch commented 2 months ago

hm, this seems to mostly fix it but if i manually request completions (C-space for me) i get nothing, only when i start to type something out does it work

llogick commented 2 months ago

hm, this seems to mostly fix it but if i manually request completions (C-space for me) i get nothing, only when i start to type something out does it work

Hmm

diff --git a/src/analysis.zig b/src/analysis.zig
index 904b6f0b..ef85ed9b 100644
--- a/src/analysis.zig
+++ b/src/analysis.zig
@@ -3482,6 +3482,8 @@ pub fn getPositionContext(
                     .enum_literal => curr_ctx.ctx = .{
                         .enum_literal = tokenLocAppend(curr_ctx.ctx.loc().?, tok),
                     },
+                    // `if` or `while` condition w/out braces, e.g. `if (true) std.<cursor>`
+                    .parens_expr => curr_ctx.ctx = .{ .var_access = tok.loc },
                     else => {},
                 },
                 .builtin => curr_ctx.ctx = .{ .builtin = tok.loc },
diff --git a/src/features/completions.zig b/src/features/completions.zig
index 804ec023..0b96ce1f 100644
--- a/src/features/completions.zig
+++ b/src/features/completions.zig
@@ -876,7 +876,7 @@ pub fn completionAtIndex(

     switch (pos_context) {
         .builtin => try completeBuiltin(&builder),
-        .var_access, .empty => try completeGlobal(&builder),
+        .var_access, .empty, .parens_expr => try completeGlobal(&builder),
         .field_access => |loc| try completeFieldAccess(&builder, loc),
         .global_error_set => try globalSetCompletions(&builder, .error_set),
         .enum_literal => |loc| try completeDot(&builder, loc),
xdBronch commented 2 months ago

yeah that does it thank you, were you planning on PRing this?

llogick commented 2 months ago

... were you planning on PRing this?

Not too eager, anyone is welcome to it.