Skip to content

Commit

Permalink
return resolved init expression of var decl if the type node is type (
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix authored Nov 5, 2023
1 parent 7430bb6 commit cd520e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
33 changes: 25 additions & 8 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1069,16 +1069,24 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
.aligned_var_decl,
=> {
const var_decl = tree.fullVarDecl(node).?;
if (var_decl.ast.type_node != 0) {
const decl_type = .{ .node = var_decl.ast.type_node, .handle = handle };
if (try analyser.resolveTypeOfNodeInternal(decl_type)) |typ|
return typ.instanceTypeVal();
var fallback_type: ?TypeWithHandle = null;

if (var_decl.ast.type_node != 0) blk: {
const type_node = .{ .node = var_decl.ast.type_node, .handle = handle };
const decl_type = try analyser.resolveTypeOfNodeInternal(type_node) orelse break :blk;
if (decl_type.isMetaType()) {
fallback_type = decl_type;
break :blk;
}
return decl_type.instanceTypeVal();
}
if (var_decl.ast.init_node == 0)
return null;

const value = .{ .node = var_decl.ast.init_node, .handle = handle };
return try analyser.resolveTypeOfNodeInternal(value);
if (var_decl.ast.init_node != 0) blk: {
const value = .{ .node = var_decl.ast.init_node, .handle = handle };
return try analyser.resolveTypeOfNodeInternal(value) orelse break :blk;
}

return fallback_type;
},
.identifier => {
const name_token = main_tokens[node];
Expand Down Expand Up @@ -2061,6 +2069,15 @@ pub const TypeWithHandle = struct {
};
}

pub fn isMetaType(self: TypeWithHandle) bool {
if (!self.type.is_type_val) return false;
switch (self.type.data) {
.other => |node| return Analyser.isMetaType(self.handle.tree, node),
.ip_index => |payload| return payload.index == .type_type,
else => return false,
}
}

pub fn isTypeFunc(self: TypeWithHandle) bool {
var buf: [1]Ast.Node.Index = undefined;
const tree = self.handle.tree;
Expand Down
23 changes: 22 additions & 1 deletion tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,27 @@ test "completion - struct init" {
}

test "completion - declarations" {
try testCompletion(
\\const S = struct {
\\ pub const Public = u32;
\\ const Private = u32;
\\};
\\const foo = S.<cursor>
, &.{
.{ .label = "Public", .kind = .Constant, .detail = "const Public = u32" },
.{ .label = "Private", .kind = .Constant, .detail = "const Private = u32" },
});
try testCompletion(
\\const S: type = struct {
\\ pub const Public = u32;
\\ const Private: type = u32;
\\};
\\const foo = S.<cursor>
, &.{
.{ .label = "Public", .kind = .Constant, .detail = "const Public = u32" },
.{ .label = "Private", .kind = .Constant, .detail = "const Private: type = u32" },
});

try testCompletion(
\\const S = struct {
\\ pub fn public() S {}
Expand All @@ -1345,7 +1366,7 @@ test "completion - declarations" {
});

try testCompletion(
\\const S = struct {
\\const S: type = struct {
\\ pub fn public() S {}
\\ fn private() !void {}
\\};
Expand Down

0 comments on commit cd520e5

Please sign in to comment.