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;
// All identifiers in main_token..data.lhs are error fields.
var i = main_tokens[node_idx];
while (i < data[node_idx].rhs) : (i += 1) {
if (token_tags[i] == .identifier) {
const name = offsets.tokenToSlice(tree, i);
if (try scopes.items(.decls)[scope_index].fetchPut(allocator, name, .{ .error_token = i })) |_| {
// TODO Record a redefinition error.
}
const gop = try context.errors.getOrPut(allocator, .{
.label = name,
.kind = .Constant,
//.detail =
.insertText = name,
.insertTextFormat = .PlainText,
});
if (!gop.found_existing) {
gop.key_ptr.detail = try std.fmt.allocPrint(allocator, "error.{s}", .{name});
}
// All identifiers in main_token..data.rhs are error fields.
var tok_i = main_tokens[node_idx] + 2;
while (tok_i < data[node_idx].rhs) : (tok_i += 1) {
switch (token_tags[tok_i]) {
.doc_comment, .comma => {},
.identifier => {
const name = offsets.tokenToSlice(tree, tok_i);
if (try scopes.items(.decls)[scope_index].fetchPut(allocator, name, .{ .error_token = tok_i })) |_| {
// TODO Record a redefinition error.
}
const gop = try context.errors.getOrPut(allocator, .{
.label = name,
.kind = .Constant,
//.detail =
.insertText = name,
.insertTextFormat = .PlainText,
});
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 => {
if (node_data[node].lhs > 0) {
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);
if (try child.resolveType(allocator, builder.store, &bound_type_params)) |decl_type| {
try colorIdentifierBasedOnType(builder, decl_type, main_token, .{});
} else {
try writeTokenMod(builder, main_token, .variable, .{});
return try colorIdentifierBasedOnType(builder, decl_type, main_token, .{});
}
}
return try writeToken(builder, main_token, .variable);
},
.fn_proto,
.fn_proto_one,
@ -776,9 +787,6 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
.unreachable_literal => {
try writeToken(builder, main_token, .keywordLiteral);
},
.error_set_decl => {
try writeToken(builder, main_token, .keyword);
},
.@"asm",
.asm_output,
.asm_input,
@ -804,8 +812,11 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
.@"catch" => {
try callWriteNodeTokens(allocator, .{ builder, node_data[node].lhs });
try writeToken(builder, main_token, .keyword);
if (token_tags[main_token + 1] == .pipe)
try writeToken(builder, main_token + 1, .variable);
if (token_tags[main_token + 1] == .pipe) {
try writeTokenMod(builder, main_token + 2, .variable, .{
.declaration = true,
});
}
try callWriteNodeTokens(allocator, .{ builder, node_data[node].rhs });
},
.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.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);
@ -953,7 +964,6 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
if (ptr_type.ast.bit_range_start != 0) {
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 });
}
}