Merge pull request #1262 from FnControlOption/block-type

Resolve type of simple labeled block
This commit is contained in:
Lee Cannon 2023-06-24 22:16:54 +01:00 committed by GitHub
commit 7487308948
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -1140,6 +1140,33 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
.handle = handle,
};
},
.block,
.block_semicolon,
.block_two,
.block_two_semicolon,
=> {
const first_token = tree.firstToken(node);
if (token_tags[first_token] != .identifier) return null;
const block_label = tree.tokenSlice(first_token);
var buffer: [2]Ast.Node.Index = undefined;
const statements = ast.blockStatements(tree, node, &buffer).?;
for (statements) |child_idx| {
// TODO: Recursively find matching `break :label` (e.g. inside `if`)
if (node_tags[child_idx] == .@"break") {
if (datas[child_idx].lhs == 0) continue;
if (datas[child_idx].rhs == 0) continue;
const break_label = tree.tokenSlice(datas[child_idx].lhs);
if (!std.mem.eql(u8, block_label, break_label)) continue;
const operand = .{ .node = datas[child_idx].rhs, .handle = handle };
return try analyser.resolveTypeOfNodeInternal(operand);
}
}
},
else => {},
}
return null;

View File

@ -509,6 +509,17 @@ test "completion - block" {
, &.{
.{ .label = "blk", .kind = .Text }, // idk what kind this should be
});
try testCompletion(
\\const S = struct { alpha: u32 };
\\const foo: S = undefined;
\\const bar = blk: {
\\ break :blk foo;
\\};
\\const baz = bar.<cursor>
, &.{
.{ .label = "alpha", .kind = .Field, .detail = "alpha: u32" },
});
}
fn testCompletion(source: []const u8, expected_completions: []const Completion) !void {