bring semantic token types and modifiers closer to predefined ones

This commit is contained in:
Techatrix 2023-03-31 18:16:46 +02:00
parent d9965c7834
commit cde544125a
2 changed files with 51 additions and 39 deletions

View File

@ -24,19 +24,27 @@ pub const TokenType = enum(u32) {
builtin, builtin,
label, label,
keywordLiteral, keywordLiteral,
namespace,
@"struct",
@"enum",
@"union",
@"opaque",
}; };
pub const TokenModifiers = packed struct(u16) { pub const TokenModifiers = packed struct(u16) {
namespace: bool = false,
@"struct": bool = false,
@"enum": bool = false,
@"union": bool = false,
@"opaque": bool = false,
declaration: bool = false, declaration: bool = false,
definition: bool = false,
readonly: bool = false,
static: bool = false,
deprecated: bool = false,
abstract: bool = false,
@"async": bool = false, @"async": bool = false,
modification: bool = false,
documentation: bool = false, documentation: bool = false,
defaultLibrary: bool = false,
generic: bool = false, generic: bool = false,
_: u7 = 0, _: u5 = 0,
}; };
const Builder = struct { const Builder = struct {
@ -172,18 +180,22 @@ fn fieldTokenType(container_decl: Ast.Node.Index, handle: *const DocumentStore.H
fn colorIdentifierBasedOnType(builder: *Builder, type_node: Analyser.TypeWithHandle, target_tok: Ast.TokenIndex, tok_mod: TokenModifiers) !void { fn colorIdentifierBasedOnType(builder: *Builder, type_node: Analyser.TypeWithHandle, target_tok: Ast.TokenIndex, tok_mod: TokenModifiers) !void {
if (type_node.type.is_type_val) { if (type_node.type.is_type_val) {
var new_tok_mod = tok_mod; var new_tok_mod = tok_mod;
if (type_node.isNamespace())
new_tok_mod.namespace = true
else if (type_node.isStructType())
new_tok_mod.@"struct" = true
else if (type_node.isEnumType())
new_tok_mod.@"enum" = true
else if (type_node.isUnionType())
new_tok_mod.@"union" = true
else if (type_node.isOpaqueType())
new_tok_mod.@"opaque" = true;
try writeTokenMod(builder, target_tok, .type, new_tok_mod); const token_type: TokenType =
if (type_node.isNamespace())
.namespace
else if (type_node.isStructType())
.@"struct"
else if (type_node.isEnumType())
.@"enum"
else if (type_node.isUnionType())
.@"union"
else if (type_node.isOpaqueType())
.@"opaque"
else
.type;
try writeTokenMod(builder, target_tok, token_type, new_tok_mod);
} else if (type_node.isTypeFunc()) { } else if (type_node.isTypeFunc()) {
try writeTokenMod(builder, target_tok, .type, tok_mod); try writeTokenMod(builder, target_tok, .type, tok_mod);
} else if (type_node.isFunc()) { } else if (type_node.isFunc()) {

View File

@ -202,7 +202,7 @@ test "semantic tokens - field access" {
\\const std = @import("std"); \\const std = @import("std");
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "std", .type, .{ .namespace = true, .declaration = true } }, .{ "std", .namespace, .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "@import", .builtin, .{} }, .{ "@import", .builtin, .{} },
.{ "\"std\"", .string, .{} }, .{ "\"std\"", .string, .{} },
@ -212,17 +212,17 @@ test "semantic tokens - field access" {
\\const Ast = std.zig.Ast; \\const Ast = std.zig.Ast;
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "std", .type, .{ .namespace = true, .declaration = true } }, .{ "std", .namespace, .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "@import", .builtin, .{} }, .{ "@import", .builtin, .{} },
.{ "\"std\"", .string, .{} }, .{ "\"std\"", .string, .{} },
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Ast", .type, .{ .@"struct" = true, .declaration = true } }, .{ "Ast", .@"struct", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "std", .type, .{ .namespace = true } }, .{ "std", .namespace, .{} },
.{ "zig", .type, .{ .namespace = true } }, .{ "zig", .namespace, .{} },
.{ "Ast", .type, .{ .@"struct" = true } }, .{ "Ast", .@"struct", .{} },
}); });
} }
@ -247,7 +247,7 @@ test "semantic tokens - call" {
\\const alpha = ns.foo(); \\const alpha = ns.foo();
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "ns", .type, .{ .namespace = true, .declaration = true } }, .{ "ns", .namespace, .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "struct", .keyword, .{} }, .{ "struct", .keyword, .{} },
.{ "fn", .keyword, .{} }, .{ "fn", .keyword, .{} },
@ -257,7 +257,7 @@ test "semantic tokens - call" {
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "alpha", .variable, .{ .declaration = true } }, .{ "alpha", .variable, .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "ns", .type, .{ .namespace = true } }, .{ "ns", .namespace, .{} },
.{ "foo", .function, .{} }, .{ "foo", .function, .{} },
}); });
} }
@ -492,7 +492,7 @@ test "semantic tokens - struct" {
\\const Foo = struct {}; \\const Foo = struct {};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .namespace = true, .declaration = true } }, .{ "Foo", .namespace, .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "struct", .keyword, .{} }, .{ "struct", .keyword, .{} },
}); });
@ -500,7 +500,7 @@ test "semantic tokens - struct" {
\\const Foo = packed struct(u32) {}; \\const Foo = packed struct(u32) {};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .namespace = true, .declaration = true } }, .{ "Foo", .namespace, .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "packed", .keyword, .{} }, .{ "packed", .keyword, .{} },
.{ "struct", .keyword, .{} }, .{ "struct", .keyword, .{} },
@ -513,7 +513,7 @@ test "semantic tokens - struct" {
\\}; \\};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"struct" = true, .declaration = true } }, .{ "Foo", .@"struct", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "struct", .keyword, .{} }, .{ "struct", .keyword, .{} },
.{ "alpha", .property, .{} }, .{ "alpha", .property, .{} },
@ -528,7 +528,7 @@ test "semantic tokens - struct" {
\\}; \\};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"struct" = true, .declaration = true } }, .{ "Foo", .@"struct", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "struct", .keyword, .{} }, .{ "struct", .keyword, .{} },
.{ "alpha", .property, .{} }, .{ "alpha", .property, .{} },
@ -552,7 +552,7 @@ test "semantic tokens - struct" {
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "u32", .type, .{} }, .{ "u32", .type, .{} },
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"struct" = true, .declaration = true } }, .{ "Foo", .@"struct", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "struct", .keyword, .{} }, .{ "struct", .keyword, .{} },
.{ "u32", .type, .{} }, .{ "u32", .type, .{} },
@ -567,7 +567,7 @@ test "semantic tokens - union" {
\\const Foo = union {}; \\const Foo = union {};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"union" = true, .declaration = true } }, .{ "Foo", .@"union", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "union", .keyword, .{} }, .{ "union", .keyword, .{} },
}); });
@ -575,7 +575,7 @@ test "semantic tokens - union" {
\\const Foo = packed union(enum) {}; \\const Foo = packed union(enum) {};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"union" = true, .declaration = true } }, .{ "Foo", .@"union", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "packed", .keyword, .{} }, .{ "packed", .keyword, .{} },
.{ "union", .keyword, .{} }, .{ "union", .keyword, .{} },
@ -588,7 +588,7 @@ test "semantic tokens - union" {
\\}; \\};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"union" = true, .declaration = true } }, .{ "Foo", .@"union", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "union", .keyword, .{} }, .{ "union", .keyword, .{} },
.{ "E", .variable, .{} }, .{ "E", .variable, .{} },
@ -601,7 +601,7 @@ test "semantic tokens - union" {
\\}; \\};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"union" = true, .declaration = true } }, .{ "Foo", .@"union", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "union", .keyword, .{} }, .{ "union", .keyword, .{} },
.{ "E", .variable, .{} }, .{ "E", .variable, .{} },
@ -615,7 +615,7 @@ test "semantic tokens - union" {
\\}; \\};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"union" = true, .declaration = true } }, .{ "Foo", .@"union", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "union", .keyword, .{} }, .{ "union", .keyword, .{} },
.{ "E", .variable, .{} }, .{ "E", .variable, .{} },
@ -632,7 +632,7 @@ test "semantic tokens - enum" {
\\const Foo = enum {}; \\const Foo = enum {};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"enum" = true, .declaration = true } }, .{ "Foo", .@"enum", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "enum", .keyword, .{} }, .{ "enum", .keyword, .{} },
}); });
@ -643,7 +643,7 @@ test "semantic tokens - enum" {
\\}; \\};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"enum" = true, .declaration = true } }, .{ "Foo", .@"enum", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "enum", .keyword, .{} }, .{ "enum", .keyword, .{} },
.{ "alpha", .enumMember, .{} }, .{ "alpha", .enumMember, .{} },
@ -653,7 +653,7 @@ test "semantic tokens - enum" {
\\const Foo = enum(u4) {}; \\const Foo = enum(u4) {};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"enum" = true, .declaration = true } }, .{ "Foo", .@"enum", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "enum", .keyword, .{} }, .{ "enum", .keyword, .{} },
.{ "u4", .type, .{} }, .{ "u4", .type, .{} },
@ -687,7 +687,7 @@ test "semantic tokens - opaque" {
\\const Foo = opaque {}; \\const Foo = opaque {};
, &.{ , &.{
.{ "const", .keyword, .{} }, .{ "const", .keyword, .{} },
.{ "Foo", .type, .{ .@"opaque" = true, .declaration = true } }, .{ "Foo", .@"opaque", .{ .declaration = true } },
.{ "=", .operator, .{} }, .{ "=", .operator, .{} },
.{ "opaque", .keyword, .{} }, .{ "opaque", .keyword, .{} },
}); });