highlight(zig): rework query

* improve query performance 120ms -> 6ms
 * correct highlight for field, type and function
 * add highlight for escape/format sequence in string
This commit is contained in:
maxxnino 2021-08-23 14:46:27 +09:00 committed by Stephan Seitz
parent b5153b666e
commit b52cccf899
3 changed files with 48 additions and 102 deletions

View file

@ -2,5 +2,8 @@
(Block)
(ContainerDecl)
(SwitchExpr)
(InitList)
(AsmExpr)
(ErrorSetDecl)
(LINESTRING)
] @fold

View file

@ -4,120 +4,58 @@
(line_comment)
] @comment
(IDENTIFIER) @variable
[
variable: (IDENTIFIER)
variable_type_function: (IDENTIFIER)
] @variable
;field in top level decl, and in struct, union...
(ContainerField
(IDENTIFIER) @field
(SuffixExpr (IDENTIFIER) @type)?
)
parameter: (IDENTIFIER) @parameter
; INFO: field become a function if type is a function?
; const u = union { this_is_function: fn () void };
(ContainerField
(IDENTIFIER) @function
(SuffixExpr (FnProto))
)
[
field_member: (IDENTIFIER)
field_access: (IDENTIFIER)
] @field
;enum and tag union field is constant
;; assume TitleCase is a type
(
[
; union(Tag){}
(ContainerDeclType (SuffixExpr (IDENTIFIER) @type))
; enum{}
(ContainerDeclType "enum")
]
(ContainerField (IDENTIFIER) @constant)?
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
parameter: (IDENTIFIER)
] @type
(#match? @type "^[A-Z]")
)
; INFO: .IDENTIFIER is a field?
(SuffixExpr
"."
(IDENTIFIER) @field
)
; error.OutOfMemory;
(SuffixExpr
"error"
"."
(IDENTIFIER) @constant
)
(VarDecl
(IDENTIFIER) @type
;; assume camelCase is a function
(
[
; const IDENTIFIER = struct/enum/union...
(SuffixExpr (ContainerDecl))
; const A = u8;
(SuffixExpr (BuildinTypeExpr))
]
variable_type_function: (IDENTIFIER)
field_access: (IDENTIFIER)
parameter: (IDENTIFIER)
] @function
(#match? @function "^[a-z]+([A-Z][a-z0-9]*)+$")
)
; const fn_no_comma = fn (i32, i32) void;
(VarDecl
(IDENTIFIER) @function
(SuffixExpr (FnProto))
)
[
function_call: (IDENTIFIER)
function: (IDENTIFIER)
] @function
; var x: IDENTIFIER
type: (SuffixExpr (IDENTIFIER) @type)
exception: "!" @exception
; IDENTIFIER{}
constructor: (SuffixExpr (IDENTIFIER) @constructor)
;{.IDENTIFIER = 1}
(FieldInit (IDENTIFIER) @field)
; var.field
(SuffixOp (IDENTIFIER) @field)
; var.func().func().field
(
(SuffixOp
(IDENTIFIER) @function
(
(ContainerDeclType
[
(ErrorUnionExpr)
"enum"
]
)
.
(FnCallArguments)
)
; func()
(
(
(IDENTIFIER) @function
)
.
(FnCallArguments)
)
; functionn decl
(FnProto
(IDENTIFIER) @function
(SuffixExpr (IDENTIFIER) @type)?
("!")? @exception
)
(ParamDecl
(ParamType (SuffixExpr (IDENTIFIER) @parameter))
)
(ParamDecl
(IDENTIFIER) @parameter
":"
[
(ParamType (SuffixExpr (IDENTIFIER) @type))
(ParamType)
]
)
(SwitchItem
(SuffixExpr
"."
.
(IDENTIFIER) @constant
(ContainerMembers
(ContainerField (IDENTIFIER) @constant)
)
)
field_constant: (IDENTIFIER) @constant
(BUILTINIDENTIFIER) @function.builtin
((BUILTINIDENTIFIER) @include
@ -128,11 +66,13 @@ constructor: (SuffixExpr (IDENTIFIER) @constructor)
(FLOAT) @float
[
(STRINGLITERAL)
(LINESTRING)
(STRINGLITERALSINGLE)
] @string
(CHAR_LITERAL) @character
(EscapeSequence) @string.escape
(FormatSequence) @string.special
[
"allowzero"
@ -275,3 +215,6 @@ constructor: (SuffixExpr (IDENTIFIER) @constructor)
(PtrPayload "|")
(PtrIndexPayload "|")
] @punctuation.bracket
; Error
(ERROR) @error

View file

@ -1,9 +1,9 @@
[
; BUG: why function block not indent
(Block)
(ContainerDecl)
(SwitchExpr)
(InitList)
(ContainerDecl)
] @indent
[
@ -19,5 +19,5 @@
(line_comment)
(container_doc_comment)
(doc_comment)
(STRINGLITERAL)
(LINESTRING)
] @ignore