Merge pull request #574 from Techatrix/tokenize-undefined

Semantic token for keyword 'undefined'
This commit is contained in:
Auguste Rame 2022-08-05 21:48:19 +02:00 committed by GitHub
commit ea24928002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -627,7 +627,7 @@ fn allDigits(str: []const u8) bool {
return true; return true;
} }
pub fn isTypeIdent(tree: Ast, token_idx: Ast.TokenIndex) bool { pub fn isTypeIdent(text: []const u8) bool {
const PrimitiveTypes = std.ComptimeStringMap(void, .{ const PrimitiveTypes = std.ComptimeStringMap(void, .{
.{"isize"}, .{"usize"}, .{"isize"}, .{"usize"},
.{"c_short"}, .{"c_ushort"}, .{"c_short"}, .{"c_ushort"},
@ -644,7 +644,6 @@ pub fn isTypeIdent(tree: Ast, token_idx: Ast.TokenIndex) bool {
.{"anyframe"}, .{"anytype"}, .{"anyframe"}, .{"anytype"},
}); });
const text = tree.tokenSlice(token_idx);
if (PrimitiveTypes.has(text)) return true; if (PrimitiveTypes.has(text)) return true;
if (text.len == 1) return false; if (text.len == 1) return false;
if (!(text[0] == 'u' or text[0] == 'i')) return false; if (!(text[0] == 'u' or text[0] == 'i')) return false;
@ -693,7 +692,9 @@ pub fn resolveTypeOfNodeInternal(store: *DocumentStore, arena: *std.heap.ArenaAl
return try resolveTypeOfNodeInternal(store, arena, value, bound_type_params); return try resolveTypeOfNodeInternal(store, arena, value, bound_type_params);
}, },
.identifier => { .identifier => {
if (isTypeIdent(handle.tree, main_tokens[node])) { const name = tree.getNodeSource(node);
if (isTypeIdent(name)) {
return TypeWithHandle{ return TypeWithHandle{
.type = .{ .data = .{ .primitive = node }, .is_type_val = true }, .type = .{ .data = .{ .primitive = node }, .is_type_val = true },
.handle = handle, .handle = handle,
@ -704,7 +705,7 @@ pub fn resolveTypeOfNodeInternal(store: *DocumentStore, arena: *std.heap.ArenaAl
store, store,
arena, arena,
handle, handle,
tree.getNodeSource(node), name,
starts[main_tokens[node]], starts[main_tokens[node]],
)) |child| { )) |child| {
switch (child.decl.*) { switch (child.decl.*) {

View File

@ -425,7 +425,11 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
try writeToken(builder, node_data[node].rhs, .errorTag); try writeToken(builder, node_data[node].rhs, .errorTag);
}, },
.identifier => { .identifier => {
if (analysis.isTypeIdent(tree, main_token)) { const name = tree.getNodeSource(node);
if(std.mem.eql(u8,name, "undefined")) {
return try writeToken(builder, main_token, .keywordLiteral);
} else if (analysis.isTypeIdent(name)) {
return try writeToken(builder, main_token, .type); return try writeToken(builder, main_token, .type);
} }
@ -433,7 +437,7 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
store, store,
arena, arena,
handle, handle,
tree.getNodeSource(node), name,
tree.tokens.items(.start)[main_token], tree.tokens.items(.start)[main_token],
)) |child| { )) |child| {
if (child.decl.* == .param_decl) { if (child.decl.* == .param_decl) {