minor semantic token fixes (#1044)

* improve analysis and highlighting of error sets

* emit semantic token for unresolved identifiers

* minor semantic token fixes
This commit is contained in:
Techatrix 2023-03-11 17:36:48 +00:00 committed by GitHub
parent f234b91a50
commit 333fcb5633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 28 deletions

View File

@ -2730,24 +2730,28 @@ fn makeScopeInternal(allocator: std.mem.Allocator, context: ScopeContext, node_i
}); });
const scope_index = scopes.len - 1; const scope_index = scopes.len - 1;
// All identifiers in main_token..data.lhs are error fields. // All identifiers in main_token..data.rhs are error fields.
var i = main_tokens[node_idx]; var tok_i = main_tokens[node_idx] + 2;
while (i < data[node_idx].rhs) : (i += 1) { while (tok_i < data[node_idx].rhs) : (tok_i += 1) {
if (token_tags[i] == .identifier) { switch (token_tags[tok_i]) {
const name = offsets.tokenToSlice(tree, i); .doc_comment, .comma => {},
if (try scopes.items(.decls)[scope_index].fetchPut(allocator, name, .{ .error_token = i })) |_| { .identifier => {
// TODO Record a redefinition error. const name = offsets.tokenToSlice(tree, tok_i);
} if (try scopes.items(.decls)[scope_index].fetchPut(allocator, name, .{ .error_token = tok_i })) |_| {
const gop = try context.errors.getOrPut(allocator, .{ // TODO Record a redefinition error.
.label = name, }
.kind = .Constant, const gop = try context.errors.getOrPut(allocator, .{
//.detail = .label = name,
.insertText = name, .kind = .Constant,
.insertTextFormat = .PlainText, //.detail =
}); .insertText = name,
if (!gop.found_existing) { .insertTextFormat = .PlainText,
gop.key_ptr.detail = try std.fmt.allocPrint(allocator, "error.{s}", .{name}); });
} if (!gop.found_existing) {
gop.key_ptr.detail = try std.fmt.allocPrint(allocator, "error.{s}", .{name});
}
},
else => {},
} }
} }
}, },

View File

@ -402,6 +402,18 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
} }
} }
}, },
.error_set_decl => {
try writeToken(builder, main_token, .keyword);
var tok_i = main_tokens[node] + 2;
while (tok_i < node_data[node].rhs) : (tok_i += 1) {
switch (token_tags[tok_i]) {
.doc_comment, .comma => {},
.identifier => try writeToken(builder, tok_i, .errorTag),
else => {},
}
}
},
.error_value => { .error_value => {
if (node_data[node].lhs > 0) { if (node_data[node].lhs > 0) {
try writeToken(builder, node_data[node].lhs - 1, .keyword); try writeToken(builder, node_data[node].lhs - 1, .keyword);
@ -431,11 +443,10 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
defer bound_type_params.deinit(builder.store.allocator); defer bound_type_params.deinit(builder.store.allocator);
if (try child.resolveType(allocator, builder.store, &bound_type_params)) |decl_type| { if (try child.resolveType(allocator, builder.store, &bound_type_params)) |decl_type| {
try colorIdentifierBasedOnType(builder, decl_type, main_token, .{}); return try colorIdentifierBasedOnType(builder, decl_type, main_token, .{});
} else {
try writeTokenMod(builder, main_token, .variable, .{});
} }
} }
return try writeToken(builder, main_token, .variable);
}, },
.fn_proto, .fn_proto,
.fn_proto_one, .fn_proto_one,
@ -776,9 +787,6 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
.unreachable_literal => { .unreachable_literal => {
try writeToken(builder, main_token, .keywordLiteral); try writeToken(builder, main_token, .keywordLiteral);
}, },
.error_set_decl => {
try writeToken(builder, main_token, .keyword);
},
.@"asm", .@"asm",
.asm_output, .asm_output,
.asm_input, .asm_input,
@ -804,8 +812,11 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
.@"catch" => { .@"catch" => {
try callWriteNodeTokens(allocator, .{ builder, node_data[node].lhs }); try callWriteNodeTokens(allocator, .{ builder, node_data[node].lhs });
try writeToken(builder, main_token, .keyword); try writeToken(builder, main_token, .keyword);
if (token_tags[main_token + 1] == .pipe) if (token_tags[main_token + 1] == .pipe) {
try writeToken(builder, main_token + 1, .variable); try writeTokenMod(builder, main_token + 2, .variable, .{
.declaration = true,
});
}
try callWriteNodeTokens(allocator, .{ builder, node_data[node].rhs }); try callWriteNodeTokens(allocator, .{ builder, node_data[node].rhs });
}, },
.add, .add,
@ -941,7 +952,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
if (ptr_type.size == .One) try writeToken(builder, main_token, .operator); if (ptr_type.size == .One) try writeToken(builder, main_token, .operator);
if (ptr_type.ast.sentinel != 0) { if (ptr_type.ast.sentinel != 0) {
return try callWriteNodeTokens(allocator, .{ builder, ptr_type.ast.sentinel }); try callWriteNodeTokens(allocator, .{ builder, ptr_type.ast.sentinel });
} }
try writeToken(builder, ptr_type.allowzero_token, .keyword); try writeToken(builder, ptr_type.allowzero_token, .keyword);
@ -953,7 +964,6 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
if (ptr_type.ast.bit_range_start != 0) { if (ptr_type.ast.bit_range_start != 0) {
try callWriteNodeTokens(allocator, .{ builder, ptr_type.ast.bit_range_start }); try callWriteNodeTokens(allocator, .{ builder, ptr_type.ast.bit_range_start });
try writeToken(builder, tree.firstToken(ptr_type.ast.bit_range_end - 1), .operator);
try callWriteNodeTokens(allocator, .{ builder, ptr_type.ast.bit_range_end }); try callWriteNodeTokens(allocator, .{ builder, ptr_type.ast.bit_range_end });
} }
} }