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,
label,
keywordLiteral,
namespace,
@"struct",
@"enum",
@"union",
@"opaque",
};
pub const TokenModifiers = packed struct(u16) {
namespace: bool = false,
@"struct": bool = false,
@"enum": bool = false,
@"union": bool = false,
@"opaque": bool = false,
declaration: bool = false,
definition: bool = false,
readonly: bool = false,
static: bool = false,
deprecated: bool = false,
abstract: bool = false,
@"async": bool = false,
modification: bool = false,
documentation: bool = false,
defaultLibrary: bool = false,
generic: bool = false,
_: u7 = 0,
_: u5 = 0,
};
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 {
if (type_node.type.is_type_val) {
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()) {
try writeTokenMod(builder, target_tok, .type, tok_mod);
} else if (type_node.isFunc()) {

View File

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