From a30d310d7fdfdb1d6831d718cf57f01dbf83a911 Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Fri, 5 Aug 2022 14:20:16 +0200 Subject: [PATCH] better semantic token for keyword `undefined` --- src/analysis.zig | 9 +++++---- src/semantic_tokens.zig | 8 ++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index ea0b0a461..bfb8e2e0c 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -627,7 +627,7 @@ fn allDigits(str: []const u8) bool { return true; } -pub fn isTypeIdent(tree: Ast, token_idx: Ast.TokenIndex) bool { +pub fn isTypeIdent(text: []const u8) bool { const PrimitiveTypes = std.ComptimeStringMap(void, .{ .{"isize"}, .{"usize"}, .{"c_short"}, .{"c_ushort"}, @@ -644,7 +644,6 @@ pub fn isTypeIdent(tree: Ast, token_idx: Ast.TokenIndex) bool { .{"anyframe"}, .{"anytype"}, }); - const text = tree.tokenSlice(token_idx); if (PrimitiveTypes.has(text)) return true; if (text.len == 1) 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); }, .identifier => { - if (isTypeIdent(handle.tree, main_tokens[node])) { + const name = tree.getNodeSource(node); + + if (isTypeIdent(name)) { return TypeWithHandle{ .type = .{ .data = .{ .primitive = node }, .is_type_val = true }, .handle = handle, @@ -704,7 +705,7 @@ pub fn resolveTypeOfNodeInternal(store: *DocumentStore, arena: *std.heap.ArenaAl store, arena, handle, - tree.getNodeSource(node), + name, starts[main_tokens[node]], )) |child| { switch (child.decl.*) { diff --git a/src/semantic_tokens.zig b/src/semantic_tokens.zig index f756fccf8..301458eaf 100644 --- a/src/semantic_tokens.zig +++ b/src/semantic_tokens.zig @@ -425,7 +425,11 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D try writeToken(builder, node_data[node].rhs, .errorTag); }, .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); } @@ -433,7 +437,7 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D store, arena, handle, - tree.getNodeSource(node), + name, tree.tokens.items(.start)[main_token], )) |child| { if (child.decl.* == .param_decl) {