Skip to content
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
33 changes: 29 additions & 4 deletions src/features/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,41 @@ const Builder = struct {
const struct_init = tree.fullStructInit(&buffer, node).?;
for (struct_init.ast.fields) |value_node| { // the node of `value` in `.name = value`
const name_token = tree.firstToken(value_node) - 2; // math our way two token indexes back to get the `name`
const name_loc = offsets.tokenToLoc(tree, name_token);
const name = offsets.locToSlice(tree.source, name_loc);
const name = offsets.identifierTokenToNameSlice(tree, name_token);
if (!std.mem.eql(u8, name, decl_name)) continue;

const lookup = try builder.analyser.lookupSymbolFieldInit(handle, name, node, &.{}) orelse continue;
const nodes = switch (tree.nodeTag(node)) {
.struct_init_dot,
.struct_init_dot_comma,
.struct_init_dot_two,
.struct_init_dot_two_comma,
=> try ast.nodesOverlappingIndex(
builder.allocator,
tree,
tree.tokenStart(name_token),
),
// if this isn't an anonymous struct the type can be determined from the `T{}` directly
.struct_init_one,
.struct_init_one_comma,
.struct_init,
.struct_init_comma,
=> &.{node},
else => unreachable,
};

const lookup = try builder.analyser.lookupSymbolFieldInit(
handle,
name,
nodes[0],
nodes[1..],
) orelse return;

if (builder.decl_handle.eql(lookup)) {
try builder.add(handle, name_token);
return;
}
// if we get here then we know that the name of the field matched
// and duplicate fields are invalid so just return early
return;
}
},
.enum_literal => {
Expand Down
10 changes: 6 additions & 4 deletions tests/lsp_features/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,17 @@ test "struct decl access" {

test "struct one field init" {
try testSymbolReferences(
\\const S = struct {<0>: u32};
\\const s = S{.<0> = 0 };
\\const S = struct { <0>: u32 };
\\const s = S{ .<0> = 0 };
\\const s2: S = .{ .<0> = 0 };
);
}

test "struct multi-field init" {
try testSymbolReferences(
\\const S = struct {<0>: u32, a: bool};
\\const s = S{.<0> = 0, .a = true};
\\const S = struct { <0>: u32, a: bool };
\\const s = S{ .<0> = 0, .a = true };
\\const s2: S = .{ .<0> = 0, .a = true };
);
}

Expand Down