Completion of unions in switches and its tags

This commit is contained in:
Luuk de Gram 2021-03-09 12:35:56 +01:00
parent 20cb0b7307
commit 858f3cb282
No known key found for this signature in database
GPG Key ID: A002B174963DBB7D
2 changed files with 39 additions and 33 deletions

View File

@ -1941,7 +1941,7 @@ pub const DeclWithHandle = struct {
.param_decl => |p| p.name_token.?, .param_decl => |p| p.name_token.?,
.pointer_payload => |pp| pp.name, .pointer_payload => |pp| pp.name,
.array_payload => |ap| ap.identifier, .array_payload => |ap| ap.identifier,
.switch_payload => |sp| sp.node + @boolToInt(token_tags[sp.node] == .asterisk), .switch_payload => |sp| sp.node,
.label_decl => |ld| ld, .label_decl => |ld| ld,
}; };
} }
@ -2948,13 +2948,18 @@ fn makeScopeInternal(
try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, while_node.ast.else_expr); try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, while_node.ast.else_expr);
} }
}, },
.switch_case, .@"switch",
.switch_case_one, .switch_comma,
=> { => {
const switch_case: ast.full.SwitchCase = switch (node) { const cond = data[node_idx].lhs;
.switch_case => tree.switchCase(node_idx), const extra = tree.extraData(data[node_idx].rhs, ast.Node.SubRange);
.switch_case_one => tree.switchCaseOne(node_idx), const cases = tree.extra_data[extra.start..extra.end];
else => unreachable,
for(cases)|case| {
const switch_case: ast.full.SwitchCase = switch (tags[case]) {
.switch_case => tree.switchCase(case),
.switch_case_one => tree.switchCaseOne(case),
else => continue,
}; };
if (switch_case.payload_token) |payload| { if (switch_case.payload_token) |payload| {
@ -2962,7 +2967,7 @@ fn makeScopeInternal(
scope.* = .{ scope.* = .{
.range = .{ .range = .{
.start = offsets.tokenLocation(tree, payload).start, .start = offsets.tokenLocation(tree, payload).start,
.end = offsets.tokenLocation(tree, tree.lastToken(switch_case.ast.target_expr)).end, .end = offsets.tokenLocation(tree, tree.lastToken(switch_case.ast.target_expr)).end + 1,
}, },
.decls = std.StringHashMap(Declaration).init(allocator), .decls = std.StringHashMap(Declaration).init(allocator),
.uses = &.{}, .uses = &.{},
@ -2977,14 +2982,15 @@ fn makeScopeInternal(
try scope.decls.putNoClobber(name, .{ try scope.decls.putNoClobber(name, .{
.switch_payload = .{ .switch_payload = .{
.node = payload, .node = name_token,
.switch_expr = switch_case.ast.target_expr, .switch_expr = cond,
.items = switch_case.ast.values, .items = switch_case.ast.values,
}, },
}); });
} }
try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, switch_case.ast.target_expr); try makeScopeInternal(allocator, scopes, error_completions, enum_completions, tree, switch_case.ast.target_expr);
}
}, },
.global_var_decl, .global_var_decl,
.local_var_decl, .local_var_decl,

View File

@ -959,7 +959,7 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: analysis.Decl
}, },
.switch_payload => |payload| { .switch_payload => |payload| {
try context.completions.append(.{ try context.completions.append(.{
.label = tree.tokenSlice(tree.firstToken(payload.node)), .label = tree.tokenSlice(payload.node),
.kind = .Variable, .kind = .Variable,
}); });
}, },