From 7434203ba26b165ab0eb178b7ea615fa71d193d1 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Wed, 13 Nov 2024 01:59:49 +0100 Subject: [PATCH] fix and test assign destructure with non var-decls --- src/features/semantic_tokens.zig | 24 ++++++++++++++++++--- tests/lsp_features/completion.zig | 7 +++--- tests/lsp_features/definition.zig | 10 ++++++++- tests/lsp_features/hover.zig | 16 +++++++++++++- tests/lsp_features/inlay_hints.zig | 9 +++++++- tests/lsp_features/semantic_tokens.zig | 30 +++++++++++++++++++++++++- 6 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/features/semantic_tokens.zig b/src/features/semantic_tokens.zig index d4ea022e2..3cff484e0 100644 --- a/src/features/semantic_tokens.zig +++ b/src/features/semantic_tokens.zig @@ -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); diff --git a/tests/lsp_features/completion.zig b/tests/lsp_features/completion.zig index 575bfb667..c1be4d3b8 100644 --- a/tests/lsp_features/completion.zig +++ b/tests/lsp_features/completion.zig @@ -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}; @@ -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}; \\ \\} , &.{ - .{ .label = "foo", .kind = .Constant, .detail = "u32" }, + .{ .label = "foo", .kind = .Variable, .detail = "u32" }, .{ .label = "bar", .kind = .Constant, .detail = "u64" }, .{ .label = "baz", .kind = .Variable, .detail = "u32" }, }); diff --git a/tests/lsp_features/definition.zig b/tests/lsp_features/definition.zig index 3f720dff6..056723662 100644 --- a/tests/lsp_features/definition.zig +++ b/tests/lsp_features/definition.zig @@ -66,12 +66,20 @@ test "local variable" { test "assign destructure" { try testDefinition( - \\comptime { + \\test { \\ const foo, const bar: u32 = .{ 1, 2 }; \\ _ = foo; \\ _ = <>bar; \\} ); + try testDefinition( + \\test { + \\ var foo: u32 = undefined; + \\ foo, const bar: u32 = .{ 1, 2 }; + \\ _ = <>foo; + \\ _ = bar; + \\} + ); } test "function parameter" { diff --git a/tests/lsp_features/hover.zig b/tests/lsp_features/hover.zig index 31f1e81c6..e8f59e101 100644 --- a/tests/lsp_features/hover.zig +++ b/tests/lsp_features/hover.zig @@ -1111,7 +1111,7 @@ test "var decl alias" { ); } -test "var decl destructuring" { +test "assign destructure" { try testHover( \\test { \\ const foo, const bar = .{ @as(u8, 1), @as(u16, 2), @as(u24, 3) }; @@ -1175,6 +1175,20 @@ test "var decl destructuring" { \\(usize) \\``` ); + try testHover( + \\test { + \\ var foo: u32 = undefined; + \\ var bar: u64 = undefined; + \\ foo, bar = .{ 3, 4 }; + \\}; + , + \\```zig + \\var bar: u64 = undefined + \\``` + \\```zig + \\(u64) + \\``` + ); } test "escaped identifier" { diff --git a/tests/lsp_features/inlay_hints.zig b/tests/lsp_features/inlay_hints.zig index 1f6d0f918..468f71652 100644 --- a/tests/lsp_features/inlay_hints.zig +++ b/tests/lsp_features/inlay_hints.zig @@ -267,7 +267,7 @@ test "var decl" { , .{ .kind = .Type }); } -test "var decl destructuring" { +test "assign destructure" { try testInlayHints( \\test { \\ const foo, const bar = .{@as(u32, 1), 2}; @@ -283,6 +283,13 @@ test "var decl destructuring" { \\ const foo, const bar: u64, var baz = [_]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" { diff --git a/tests/lsp_features/semantic_tokens.zig b/tests/lsp_features/semantic_tokens.zig index 51bec7f9a..4db4e8222 100644 --- a/tests/lsp_features/semantic_tokens.zig +++ b/tests/lsp_features/semantic_tokens.zig @@ -183,7 +183,7 @@ test "var decl" { }); } -test "var decl destructure" { +test "assign destructure" { try testSemanticTokens( \\test { \\ var alpha: bool, var beta = .{ 1, 2 }; @@ -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" {