Skip to content

Commit

Permalink
fix and test assign destructure with non var-decls
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Nov 13, 2024
1 parent b2557fa commit 7434203
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
24 changes: 21 additions & 3 deletions src/features/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,27 @@ fn writeNodeTokens(builder: *Builder, node: Ast.Node.Index) error{OutOfMemory}!v
const resolved_type = try builder.analyser.resolveTypeOfNode(.{ .node = init_expr, .handle = handle });

for (lhs_exprs, 0..) |lhs_node, index| {
const var_decl = tree.fullVarDecl(lhs_node).?;
const field_type = if (resolved_type) |ty| try builder.analyser.resolveTupleFieldType(ty, index) else null;
try writeVarDecl(builder, var_decl, field_type);
switch (node_tags[lhs_node]) {
.global_var_decl,
.local_var_decl,
.aligned_var_decl,
.simple_var_decl,
=> {
const var_decl = tree.fullVarDecl(lhs_node).?;
const field_type = if (resolved_type) |ty| try builder.analyser.resolveTupleFieldType(ty, index) else null;
try writeVarDecl(builder, var_decl, field_type);
},
.identifier => {
const name_token = main_tokens[lhs_node];
const maybe_type = if (resolved_type) |ty| try builder.analyser.resolveTupleFieldType(ty, index) else null;
const ty = maybe_type orelse {
try writeIdentifier(builder, name_token);
continue;
};
try colorIdentifierBasedOnType(builder, ty, name_token, false, .{});
},
else => {},
}
}

try writeToken(builder, main_token, .operator);
Expand Down
7 changes: 4 additions & 3 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ test "symbol lookup on identifier named after primitive" {
});
}

test "var decl destructuring" {
test "assign destructure" {
try testCompletion(
\\test {
\\ const foo, var bar: u32 = .{42, 7};
Expand Down Expand Up @@ -211,11 +211,12 @@ test "var decl destructuring" {
});
try testCompletion(
\\test {
\\ const foo, const bar: u64, var baz = [_]u32{1, 2, 3};
\\ var foo: u32 = undefined;
\\ foo, const bar: u64, var baz = [_]u32{1, 2, 3};
\\ <cursor>
\\}
, &.{
.{ .label = "foo", .kind = .Constant, .detail = "u32" },
.{ .label = "foo", .kind = .Variable, .detail = "u32" },
.{ .label = "bar", .kind = .Constant, .detail = "u64" },
.{ .label = "baz", .kind = .Variable, .detail = "u32" },
});
Expand Down
10 changes: 9 additions & 1 deletion tests/lsp_features/definition.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ test "local variable" {

test "assign destructure" {
try testDefinition(
\\comptime {
\\test {
\\ const foo, const <def><decl>bar</decl></def>: <tdef>u32</tdef> = .{ 1, 2 };
\\ _ = foo;
\\ _ = <>bar;
\\}
);
try testDefinition(
\\test {
\\ var <def><decl>foo</decl></def>: <tdef>u32</tdef> = undefined;
\\ foo, const bar: u32 = .{ 1, 2 };
\\ _ = <>foo;
\\ _ = bar;
\\}
);
}

test "function parameter" {
Expand Down
16 changes: 15 additions & 1 deletion tests/lsp_features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ test "var decl alias" {
);
}

test "var decl destructuring" {
test "assign destructure" {
try testHover(
\\test {
\\ const f<cursor>oo, const bar = .{ @as(u8, 1), @as(u16, 2), @as(u24, 3) };
Expand Down Expand Up @@ -1175,6 +1175,20 @@ test "var decl destructuring" {
\\(usize)
\\```
);
try testHover(
\\test {
\\ var foo: u32 = undefined;
\\ var bar: u64 = undefined;
\\ foo, bar<cursor> = .{ 3, 4 };
\\};
,
\\```zig
\\var bar: u64 = undefined
\\```
\\```zig
\\(u64)
\\```
);
}

test "escaped identifier" {
Expand Down
9 changes: 8 additions & 1 deletion tests/lsp_features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ test "var decl" {
, .{ .kind = .Type });
}

test "var decl destructuring" {
test "assign destructure" {
try testInlayHints(
\\test {
\\ const foo<u32>, const bar<comptime_int> = .{@as(u32, 1), 2};
Expand All @@ -283,6 +283,13 @@ test "var decl destructuring" {
\\ const foo<u32>, const bar: u64, var baz<u32> = [_]u32{1, 2, 3};
\\}
, .{ .kind = .Type });
try testInlayHints(
\\test {
\\ var foo: u32 = undefined;
\\ var bar: u64 = undefined;
\\ foo, bar = .{ 3, 4 };
\\}
, .{ .kind = .Type });
}

test "function alias" {
Expand Down
30 changes: 29 additions & 1 deletion tests/lsp_features/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ test "var decl" {
});
}

test "var decl destructure" {
test "assign destructure" {
try testSemanticTokens(
\\test {
\\ var alpha: bool, var beta = .{ 1, 2 };
Expand Down Expand Up @@ -220,6 +220,34 @@ test "var decl destructure" {
.{ "struct", .keyword, .{} },
.{ "enum", .keyword, .{} },
});
try testSemanticTokens(
\\test {
\\ var foo: u32 = undefined;
\\ var bar: u64 = undefined;
\\ foo, bar = .{ 3, 4 };
\\};
, &.{
.{ "test", .keyword, .{} },

.{ "var", .keyword, .{} },
.{ "foo", .variable, .{ .declaration = true } },
.{ "u32", .type, .{} },
.{ "=", .operator, .{} },
.{ "undefined", .keywordLiteral, .{} },

.{ "var", .keyword, .{} },
.{ "bar", .variable, .{ .declaration = true } },
.{ "u64", .type, .{} },
.{ "=", .operator, .{} },
.{ "undefined", .keywordLiteral, .{} },

.{ "foo", .variable, .{} },
.{ "bar", .variable, .{} },
.{ "=", .operator, .{} },

.{ "3", .number, .{} },
.{ "4", .number, .{} },
});
}

test "local var decl" {
Expand Down

0 comments on commit 7434203

Please sign in to comment.