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,43 +2948,49 @@ 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,
};
if (switch_case.payload_token) |payload| { for(cases)|case| {
var scope = try scopes.addOne(allocator); const switch_case: ast.full.SwitchCase = switch (tags[case]) {
scope.* = .{ .switch_case => tree.switchCase(case),
.range = .{ .switch_case_one => tree.switchCaseOne(case),
.start = offsets.tokenLocation(tree, payload).start, else => continue,
.end = offsets.tokenLocation(tree, tree.lastToken(switch_case.ast.target_expr)).end,
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &.{},
.tests = &.{},
.data = .other,
}; };
errdefer scope.decls.deinit();
// if payload is *name than get next token if (switch_case.payload_token) |payload| {
const name_token = payload + @boolToInt(token_tags[payload] == .asterisk); var scope = try scopes.addOne(allocator);
const name = tree.tokenSlice(name_token); scope.* = .{
.range = .{
.start = offsets.tokenLocation(tree, payload).start,
.end = offsets.tokenLocation(tree, tree.lastToken(switch_case.ast.target_expr)).end + 1,
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &.{},
.tests = &.{},
.data = .other,
};
errdefer scope.decls.deinit();
try scope.decls.putNoClobber(name, .{ // if payload is *name than get next token
.switch_payload = .{ const name_token = payload + @boolToInt(token_tags[payload] == .asterisk);
.node = payload, const name = tree.tokenSlice(name_token);
.switch_expr = switch_case.ast.target_expr,
.items = switch_case.ast.values, try scope.decls.putNoClobber(name, .{
}, .switch_payload = .{
}); .node = name_token,
.switch_expr = cond,
.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,
}); });
}, },