Skip to content

Commit 1938aa7

Browse files
authored
Improve gdscript-ts-mode highlighting with (#165)
* Added class_name highlighting * Simplified keyword highlighting * Added missing for/in keywords * Added builtin types support * Fixed class_definition face; treat preload() as builtin * Fixed int/bool/float/void not being treated as builtin types * Fixed escape sequences highlighting
1 parent 3f3739d commit 1938aa7

File tree

1 file changed

+81
-27
lines changed

1 file changed

+81
-27
lines changed

gdscript-ts-mode.el

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,64 +68,121 @@ It must be a function with two arguments: TYPE and NAME.")
6868
(if (string= type "class")
6969
"*class definition*"
7070
"*function definition*"))
71+
7172

7273
;;; Keywords
7374

74-
(defvar gdscript-ts--treesit-keywords '("and" "as" "break" "class" "class_name"
75-
"const" "continue" "elif" "else" "enum" "export" "extends" "for" "func" "if" "in" "is"
76-
"master" "match" "not" "onready" "or" "pass" "puppet" "remote" "remotesync" "return" "setget" "signal"
77-
"var" "while"))
75+
(defvar gdscript-ts--keyword-regex
76+
(rx bot (| "func" "var" "const" "set" "get" "setget" "signal" "extends"
77+
"match" "if" "elif" "else" "for" "in" "while" "break" "continue"
78+
"pass" "return" "when" "yield" "await"
79+
"class" "class_name" "abstract" "is" "onready" "tool" "static"
80+
"export" "as" "void" "enum" "assert" "breakpoint"
81+
"sync" "remote" "master" "puppet"
82+
"remotesync" "mastersync" "puppetsync"
83+
"trait" "namespace" "super"
84+
"and" "or" "not"
85+
"await" "yield" "self") eot))
86+
87+
;;; Types
88+
89+
(defvar gdscript-ts--builtin-type-regex
90+
"\\`\\(int\\|bool\\|float\\|void\\|Vector2\\|Vector2i\\|Vector3\\|Vector3i\\|Vector4\\|Vector4i\\|Color\\|Rect2\\|Rect2i\\|Array\\|Basis\\|Dictionary\\|Plane\\|Quat\\|RID\\|Rect3\\|Transform\\|Transform2D\\|Transform3D\\|AABB\\|String\\|Color\\|NodePath\\|PoolByteArray\\|PoolIntArray\\|PoolRealArray\\|PoolStringArray\\|PoolVector2Array\\|PoolVector3Array\\|PoolColorArray\\|bool\\|int\\|float\\|Signal\\|Callable\\|StringName\\|Quaternion\\|Projection\\|PackedByteArray\\|PackedInt32Array\\|PackedInt64Array\\|PackedFloat32Array\\|PackedFloat64Array\\|PackedStringArray\\|PackedVector2Array\\|PackedVector2iArray\\|PackedVector3Array\\|PackedVector3iArray\\|PackedVector4Array\\|PackedColorArray\\|JSON\\|UPNP\\|OS\\|IP\\|JSONRPC\\|XRVRS\\)\\'")
91+
92+
(defvar gdscript-ts--type-regex
93+
"\\`[A-Z][a-zA-Z0-9_]*[a-z][a-zA-Z0-9_]*\\'")
94+
95+
;;; Constants
96+
97+
(defvar gdscript-ts--constant-regex "\\`[A-Z_][A-Z0-9_]+\\'")
7898

79-
8099
;;; Setting
81100

101+
(defvar gdscript-ts--feature-list
102+
'(( comment definition)
103+
( keyword string type annotation)
104+
( number constant escape-sequence)
105+
( bracket delimiter function operator property)))
106+
82107
(defvar gdscript-ts--treesit-settings
83108
(treesit-font-lock-rules
84109
:language 'gdscript
85110
:feature 'comment
86111
'((comment) @font-lock-comment-face)
87112

113+
:language 'gdscript
114+
:feature 'constant
115+
`(([(null) (false) (true)] @font-lock-constant-face)
116+
(const_statement name: (name) @font-lock-constant-face)
117+
(enumerator left: (identifier) @font-lock-constant-face)
118+
((identifier) @font-lock-constant-face
119+
(:match ,gdscript-ts--constant-regex @font-lock-constant-face))
120+
(variable_statement
121+
name: (name) @font-lock-constant-face
122+
(:match ,gdscript-ts--constant-regex @font-lock-constant-face)))
123+
124+
:language 'gdscript
125+
:feature 'bracket
126+
`(["[" "]" "(" ")" "{" "}"] @font-lock-bracket-face)
127+
128+
:language 'gdscript
129+
:feature 'delimiter
130+
`(["," ":" "."] @font-lock-delimiter-face)
131+
132+
:language 'gdscript
133+
:feature 'type
134+
`(((identifier) @font-lock-builtin-face
135+
(:match ,gdscript-ts--builtin-type-regex @font-lock-builtin-face))
136+
(get_node) @font-lock-builtin-face
137+
((identifier) @font-lock-type-face
138+
(:match ,gdscript-ts--type-regex @font-lock-type-face))
139+
(enum_definition name: (_) @font-lock-type-face)
140+
(class_name_statement (name) @font-lock-type-face)
141+
(class_definition (name) @font-lock-type-face))
142+
88143
:language 'gdscript
89144
:feature 'definition
90-
'((function_definition (name) @font-lock-function-name-face)
91-
(class_definition
92-
(name) @font-lock-function-name-face)
93-
(parameters (identifier) @font-lock-variable-name-face))
145+
'((function_definition (name) @font-lock-function-name-face))
146+
147+
:language 'gdscript
148+
:feature 'annotation
149+
'((annotation "@" @font-lock-preprocessor-face
150+
(identifier) @font-lock-preprocessor-face))
94151

95152
:language 'gdscript
96153
:feature 'keyword
97-
`(([,@gdscript-ts--treesit-keywords] @font-lock-keyword-face)
98-
([(false) (true)] @font-lock-keyword-face))
154+
`((ERROR _ @font-lock-keyword-face (:match ,gdscript-ts--keyword-regex @font-lock-keyword-face))
155+
(_ _ @font-lock-keyword-face (:match ,gdscript-ts--keyword-regex @font-lock-keyword-face)))
99156

100157
:language 'gdscript
101158
:feature 'string
102159
'((string) @font-lock-string-face)
103160

104161
:language 'gdscript
105-
:feature 'type
106-
'(((type) @font-lock-type-face)
107-
(get_node) @font-lock-type-face)
108-
109162
:feature 'function
110-
:language 'gdscript
111-
'((call (identifier) @font-lock-function-call-face)
163+
'((call (identifier) @font-lock-builtin-face (:match "preload" @font-lock-builtin-face))
164+
(call (identifier) @font-lock-function-call-face)
112165
(attribute_call (identifier) @font-lock-function-call-face))
113166

114167
:language 'gdscript
115-
:feature 'variable
116-
'((_ (name) @font-lock-variable-name-face))
117-
118168
:feature 'number
119-
:language 'gdscript
120169
'(([(integer) (float)] @font-lock-number-face))
121170

122-
:feature 'property
123171
:language 'gdscript
172+
:feature 'property
124173
'((attribute (identifier) (identifier) @font-lock-property-use-face))
125174

126175
:feature 'operator
127176
:language 'gdscript
128-
`(["+" "-" "*" "/" "^" ">" "<" "="] @font-lock-operator-face)))
177+
`(["+" "+=" "-" "-=" "*" "*=" "/" "/=" "^" "^=" ">" ">="
178+
"<" "<=" "|" "|=" "%" "%=" "&" "&=" ">>" ">>=" "<<" "<<="
179+
"||" "&&" "==" "!=" "->" "~" "=" ":="]
180+
@font-lock-operator-face)
181+
182+
:language 'gdscript
183+
:override t
184+
:feature 'escape-sequence
185+
'((escape_sequence) @font-lock-escape-face)))
129186

130187

131188
;;; Funtion
@@ -220,10 +277,7 @@ Similar to `gdscript-imenu-create-index' but use tree-sitter."
220277
:syntax-table gdscript-mode-syntax-table
221278
(when (treesit-ready-p 'gdscript)
222279
(treesit-parser-create 'gdscript)
223-
(setq-local treesit-font-lock-feature-list
224-
'(( comment definition)
225-
( keyword string type)
226-
( function variable number property operator)))
280+
(setq-local treesit-font-lock-feature-list gdscript-ts--feature-list)
227281
(setq-local treesit-font-lock-settings gdscript-ts--treesit-settings)
228282
;;; TODO: create-imenu
229283
(setq-local imenu-create-index-function

0 commit comments

Comments
 (0)