zigtools / zls

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

Bug in identifier detection #2035

Open Sekky61 opened 2 hours ago

Sekky61 commented 2 hours ago

Zig Version

0.14.0-dev.1573+4d81e8ee9

ZLS Version

0.14.0-dev.160+cf9b49a

Client / Code Editor / Extensions

NVIM v0.10.1 and lspconfig

Steps to Reproduce and Observed Behavior

const std = @import("std");
const print = std.debug.print;

const Enum = enum {
    source,
    @"source.fixAll",
};

pub fn main() void {
    const @"identifier with spaces in it" = Enum.source;

    switch (@"identifier with spaces in it") {
        .source => print("abc\n", .{}),
        .@"source.fixAll" => print("abcd--\n", .{}),
    }
}

Reproduction:

  1. Hover over the @"identifier with spaces in it" and perform Go to definition
  2. Hover over the word source in .@"source.fixAll" and perform Go to definition
  3. Hover over the word fixAll in .@"source.fixAll" and perform Go to definition

Notice that:

Expected Behavior

I expected the cursor to go to the definition of the variable/enum variant.

Relevant log output

No response

Sekky61 commented 2 hours ago

As for the fix, the problem seems to be in the identifierLocFromIndex function. It does not work with identifiers with special characters (the @"" syntax).

I propose that identifierLocFromIndex should handle the @"" syntax. If you agree, I would like to take a look at this issue