Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic support for destructuring syntax #1447

Merged
merged 4 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const zls_version = std.SemanticVersion{ .major = 0, .minor = 12, .patch = 0 };
pub fn build(b: *std.build.Builder) !void {
comptime {
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse("0.12.0-dev.284+cab9da35b") catch unreachable; // Enable FailingAllocator to fail on resize
const min_zig = std.SemanticVersion.parse("0.12.0-dev.389+61b70778b") catch unreachable; // compiler: implement destructuring syntax
if (current_zig.order(min_zig) == .lt) {
@compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
}
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 38 additions & 3 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2514,6 +2514,11 @@ pub const Declaration = union(enum) {
identifier: Ast.TokenIndex,
array_expr: Ast.Node.Index,
},
assign_destructure: struct {
/// tag is .assign_destructure
node: Ast.Node.Index,
index: u32,
},
array_index: Ast.TokenIndex,
switch_payload: Switch,
label_decl: struct {
Expand Down Expand Up @@ -2603,6 +2608,12 @@ pub const DeclWithHandle = struct {
.array_index => |ai| ai,
.label_decl => |ld| ld.label,
.error_token => |et| et,
.assign_destructure, => |payload| {
const lhs_count = tree.extra_data[tree.nodes.items(.data)[payload.node].lhs];
const lhs_exprs = tree.extra_data[tree.nodes.items(.data)[payload.node].lhs + 1 ..][0..lhs_count];
const expr = lhs_exprs[payload.index];
return getDeclNameToken(tree, expr).?;
},
.switch_payload => |payload| {
const case = payload.getCase(tree);
const payload_token = case.payload_token.?;
Expand Down Expand Up @@ -2776,6 +2787,7 @@ pub const DeclWithHandle = struct {
})) orelse return null,
.Single,
),
.assign_destructure => null, // TODO
.array_index => TypeWithHandle{
.type = .{ .data = .array_index, .is_type_val = false },
.handle = self.handle,
Expand Down Expand Up @@ -4035,9 +4047,32 @@ fn makeScopeAt(

for (statements) |idx| {
try makeScopeInternal(context, tree, idx);
if (tree.fullVarDecl(idx)) |var_decl| {
const name = tree.tokenSlice(var_decl.ast.mut_token + 1);
try context.putVarDecl(scope_index, name, .{ .ast_node = idx });
switch (tags[idx]) {
.global_var_decl,
.local_var_decl,
.aligned_var_decl,
.simple_var_decl,
=> {
const var_decl = tree.fullVarDecl(idx).?;
const name = tree.tokenSlice(var_decl.ast.mut_token + 1);
try context.putVarDecl(scope_index, name, .{ .ast_node = idx });
},
.assign_destructure => {
const lhs_count = tree.extra_data[data[idx].lhs];
const lhs_exprs = tree.extra_data[data[idx].lhs + 1 ..][0..lhs_count];

for (lhs_exprs, 0..) |lhs_node, i| {
const var_decl = tree.fullVarDecl(lhs_node) orelse continue;
const name = tree.tokenSlice(var_decl.ast.mut_token + 1);
try context.putVarDecl(scope_index, name, .{
.assign_destructure = .{
.node = idx,
.index = @intCast(i),
},
});
}
},
else => continue,
}
}
},
Expand Down
25 changes: 15 additions & 10 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
.assign_sub_sat,
.assign_shl_sat,
.assign,
.assign_destructure,
.merge_error_sets,
.mul,
.div,
Expand Down Expand Up @@ -616,8 +617,7 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
.switch_comma, .@"switch" => {
const lhs = datas[n].lhs;
const l_brace = tree.lastToken(lhs) + 2; //lparen + rbrace
return findMatchingRBrace(token_tags, l_brace)
orelse @intCast(tree.tokens.len - 1);
return findMatchingRBrace(token_tags, l_brace) orelse @intCast(tree.tokens.len - 1);
},
.@"asm" => {
const extra = tree.extraData(datas[n].rhs, Node.Asm);
Expand Down Expand Up @@ -678,25 +678,21 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
.block_two_semicolon,
.block_two,
=> {
return findMatchingRBrace(token_tags, main_tokens[n])
orelse @intCast(tree.tokens.len - 1);
return findMatchingRBrace(token_tags, main_tokens[n]) orelse @intCast(tree.tokens.len - 1);
},
.container_decl_trailing,
.container_decl_two_trailing,
.container_decl_two,
=> {
// + 1 for the lbrace
return findMatchingRBrace(token_tags, main_tokens[n] + 1)
orelse @intCast(tree.tokens.len - 1);
return findMatchingRBrace(token_tags, main_tokens[n] + 1) orelse @intCast(tree.tokens.len - 1);
},
.container_decl_arg,
.container_decl_arg_trailing,
=> {
// + 4 for the lparen, identifier, rparen, lbrace
const l_brace = findNextLBrace(token_tags, main_tokens[n])
orelse return @intCast(tree.tokens.len - 1);
return findMatchingRBrace(token_tags, l_brace)
orelse @intCast(tree.tokens.len - 1);
const l_brace = findNextLBrace(token_tags, main_tokens[n]) orelse return @intCast(tree.tokens.len - 1);
return findMatchingRBrace(token_tags, l_brace) orelse @intCast(tree.tokens.len - 1);
},
.array_init_dot_two,
.builtin_call_two,
Expand Down Expand Up @@ -1387,6 +1383,15 @@ pub fn iterateChildren(
try callback(context, tree, var_decl.init_node);
},

.assign_destructure => {
const lhs_count = tree.extra_data[node_data[node].lhs];
const lhs_exprs = tree.extra_data[node_data[node].lhs + 1 ..][0..lhs_count];
for (lhs_exprs) |lhs_node| {
try callback(context, tree, lhs_node);
}
try callback(context, tree, node_data[node].rhs);
},

.array_type_sentinel => {
const array_type = tree.arrayTypeSentinel(node).ast;
try callback(context, tree, array_type.elem_count);
Expand Down
1 change: 1 addition & 0 deletions src/build_runner/master.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn main() !void {
global_cache_directory,
host,
&cache,
dependencies.root_deps,
);

defer builder.destroy();
Expand Down
2 changes: 2 additions & 0 deletions src/features/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ fn handleUnusedVariableOrConstant(builder: *Builder, actions: *std.ArrayListUnma

const node = switch (decl.decl.*) {
.ast_node => |node| node,
.assign_destructure => |payload| payload.node,
else => return,
};

const first_token = tree.firstToken(node);
const last_token = ast.lastToken(tree, node) + 1;

if (last_token >= tree.tokens.len) return;
if (token_tags[last_token] != .semicolon) return;

const new_text = try createDiscardText(builder, identifier_name, token_starts[first_token], false);
Expand Down
1 change: 1 addition & 0 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: Analyser.Decl
.error_union_payload,
.error_union_error,
.array_payload,
.assign_destructure,
.array_index,
.switch_payload,
.label_decl,
Expand Down
1 change: 1 addition & 0 deletions src/features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub fn hoverSymbol(
.error_union_payload,
.error_union_error,
.array_payload,
.assign_destructure,
.array_index,
.switch_payload,
.label_decl,
Expand Down
1 change: 1 addition & 0 deletions src/features/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ pub fn symbolReferences(
.error_union_payload,
.error_union_error,
.array_payload,
.assign_destructure,
.switch_payload,
=> {
try builder.collectReferences(curr_handle, 0);
Expand Down
11 changes: 11 additions & 0 deletions src/features/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,17 @@ fn writeNodeTokens(builder: *Builder, node: Ast.Node.Index) error{OutOfMemory}!v
try writeToken(builder, main_token, token_type);
try writeNodeTokens(builder, node_data[node].rhs);
},
.assign_destructure => {
const lhs_count = tree.extra_data[node_data[node].lhs];
const lhs_exprs = tree.extra_data[node_data[node].lhs + 1 ..][0..lhs_count];

for (lhs_exprs) |lhs_node| {
try writeNodeTokens(builder, lhs_node);
}

try writeToken(builder, main_token, .operator);
try writeNodeTokens(builder, node_data[node].rhs);
},
.array_access,
.error_union,
.switch_range,
Expand Down
Loading
Loading