Skip to content

Commit

Permalink
resolve assign destructure (#1803)
Browse files Browse the repository at this point in the history
Co-authored-by: Techatrix <[email protected]>
  • Loading branch information
gabydd and Techatrix authored Mar 1, 2024
1 parent 20ca8f5 commit 0f12bba
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3660,7 +3660,24 @@ pub const DeclWithHandle = struct {
})) orelse return null,
.Single,
),
.assign_destructure => null, // TODO
.assign_destructure => |pay| {
const type_node = pay.getFullVarDecl(tree).ast.type_node;
if (type_node != 0) {
if (try analyser.resolveTypeOfNode(.{
.node = type_node,
.handle = self.handle,
})) |ty| return try ty.instanceTypeVal(analyser);
}
const node = try analyser.resolveTypeOfNode(.{
.node = tree.nodes.items(.data)[pay.node].rhs,
.handle = self.handle,
}) orelse return null;
return switch (node.data) {
.array => |array_info| try array_info.elem_ty.instanceTypeVal(analyser),
.other => try analyser.resolveTupleFieldType(node, pay.index),
else => null,
};
},
.label_decl => |decl| try analyser.resolveTypeOfNodeInternal(.{
.node = decl.block,
.handle = self.handle,
Expand Down
54 changes: 54 additions & 0 deletions tests/lsp_features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,60 @@ test "var decl alias" {
);
}

test "hover - destructuring" {
try testHover(
\\fn func() void {
\\ const f<cursor>oo, const bar = .{ 1, 2 };
\\}
,
\\```zig
\\foo
\\```
\\```zig
\\(comptime_int)
\\```
);
try testHover(
\\fn func() void {
\\ const foo, const b<cursor>ar, const baz = .{ 1, 2, 3 };
\\}
,
\\```zig
\\bar
\\```
\\```zig
\\(comptime_int)
\\```
);
try testHover(
\\fn thing() !struct {usize, isize} {
\\ return .{1, 2};
\\}
\\fn ex() void {
\\ const f<cursor>oo, const bar = try thing();
\\}
,
\\```zig
\\foo
\\```
\\```zig
\\(usize)
\\```
);
try testHover(
\\fn func() void {
\\ const foo, const b<cursor>ar: u32, const baz = undefined;
\\}
,
\\```zig
\\bar
\\```
\\```zig
\\(u32)
\\```
);
}

// https://github.com/zigtools/zls/issues/1378
test "type reference cycle" {
try testHover(
Expand Down

0 comments on commit 0f12bba

Please sign in to comment.