highlights.scm

  1; Variables
  2(identifier) @variable
  3
  4(call_expression
  5  function: (member_expression
  6    object: (identifier) @type
  7    (#any-of? @type
  8      "Promise" "Array" "Object" "Map" "Set" "WeakMap" "WeakSet" "Date" "Error" "TypeError"
  9      "RangeError" "SyntaxError" "ReferenceError" "EvalError" "URIError" "RegExp" "Function"
 10      "Number" "String" "Boolean" "Symbol" "BigInt" "Proxy" "ArrayBuffer" "DataView")))
 11
 12; Special identifiers
 13(type_annotation) @type
 14
 15(type_identifier) @type
 16
 17(predefined_type) @type.builtin
 18
 19(type_alias_declaration
 20  (type_identifier) @type)
 21
 22(type_alias_declaration
 23  value: (_
 24    (type_identifier) @type))
 25
 26(interface_declaration
 27  (type_identifier) @type)
 28
 29(class_declaration
 30  (type_identifier) @type.class)
 31
 32(extends_clause
 33  value: (identifier) @type.class)
 34
 35(extends_type_clause
 36  type: (type_identifier) @type)
 37
 38(implements_clause
 39  (type_identifier) @type)
 40
 41; Enables ts-pretty-errors
 42; The Lsp returns "snippets" of typescript, which are not valid typescript in totality,
 43; but should still be highlighted
 44; Highlights object literals by hijacking the statement_block pattern, but only if
 45; the statement block follows an object literal pattern
 46(statement_block
 47  (labeled_statement
 48    ; highlight the label like a property name
 49    label: (statement_identifier) @property.name
 50    body: [
 51      ; match a terminating expression statement
 52      (expression_statement
 53        ; single identifier - treat as a type name
 54        [
 55          (identifier) @type.name
 56          ; object - treat as a property - type pair
 57          (object
 58            (pair
 59              key: (_) @property.name
 60              value: (_) @type.name))
 61          ; subscript_expression - treat as an array declaration
 62          (subscript_expression
 63            object: (_) @type.name
 64            index: (_))
 65          ; templated string - treat each identifier contained as a type name
 66          (template_string
 67            (template_substitution
 68              (identifier) @type.name))
 69        ])
 70      ; match a nested statement block
 71      (statement_block) @nested
 72    ]))
 73
 74; Inline type imports: import { type Foo } or import { type Foo as Bar }
 75(import_specifier
 76  "type"
 77  name: (identifier) @type)
 78
 79(import_specifier
 80  "type"
 81  alias: (identifier) @type)
 82
 83; Full type imports: import type { Foo } or import type { Foo as Bar }
 84(import_statement
 85  "type"
 86  (import_clause
 87    (named_imports
 88      (import_specifier
 89        name: (identifier) @type))))
 90
 91(import_statement
 92  "type"
 93  (import_clause
 94    (named_imports
 95      (import_specifier
 96        alias: (identifier) @type))))
 97
 98([
 99  (identifier)
100  (shorthand_property_identifier)
101  (shorthand_property_identifier_pattern)
102] @constant
103  (#match? @constant "^_*[A-Z_][A-Z\\d_]*$"))
104
105; Properties
106(property_identifier) @property
107
108(shorthand_property_identifier) @property
109
110(shorthand_property_identifier_pattern) @property
111
112(private_property_identifier) @property
113
114; Function and method calls
115(call_expression
116  function: (identifier) @function)
117
118(call_expression
119  function: (member_expression
120    property: [
121      (property_identifier)
122      (private_property_identifier)
123    ] @function.method))
124
125(new_expression
126  constructor: (identifier) @type)
127
128(nested_type_identifier
129  module: (identifier) @type)
130
131; Function and method definitions
132(function_expression
133  name: (identifier) @function)
134
135(function_declaration
136  name: (identifier) @function)
137
138(method_definition
139  name: [
140    (property_identifier)
141    (private_property_identifier)
142  ] @function.method)
143
144(method_definition
145  name: (property_identifier) @constructor
146  (#eq? @constructor "constructor"))
147
148(pair
149  key: [
150    (property_identifier)
151    (private_property_identifier)
152  ] @function.method
153  value: [
154    (function_expression)
155    (arrow_function)
156  ])
157
158(assignment_expression
159  left: (member_expression
160    property: [
161      (property_identifier)
162      (private_property_identifier)
163    ] @function.method)
164  right: [
165    (function_expression)
166    (arrow_function)
167  ])
168
169(variable_declarator
170  name: (identifier) @function
171  value: [
172    (function_expression)
173    (arrow_function)
174  ])
175
176(assignment_expression
177  left: (identifier) @function
178  right: [
179    (function_expression)
180    (arrow_function)
181  ])
182
183(arrow_function) @function
184
185; Parameters
186(required_parameter
187  (identifier) @variable.parameter)
188
189(required_parameter
190  (_
191    ([
192      (identifier)
193      (shorthand_property_identifier_pattern)
194    ]) @variable.parameter))
195
196(optional_parameter
197  (identifier) @variable.parameter)
198
199(optional_parameter
200  (_
201    ([
202      (identifier)
203      (shorthand_property_identifier_pattern)
204    ]) @variable.parameter))
205
206(catch_clause
207  parameter: (identifier) @variable.parameter)
208
209(index_signature
210  name: (identifier) @variable.parameter)
211
212(arrow_function
213  parameter: (identifier) @variable.parameter)
214
215(type_predicate
216  name: (identifier) @variable.parameter)
217
218; Literals
219(this) @variable.special
220
221(super) @variable.special
222
223[
224  (null)
225  (undefined)
226] @constant.builtin
227
228[
229  (true)
230  (false)
231] @boolean
232
233(literal_type
234  [
235    (null)
236    (undefined)
237    (true)
238    (false)
239  ] @type.builtin)
240
241(comment) @comment
242
243(hash_bang_line) @comment
244
245[
246  (string)
247  (template_string)
248  (template_literal_type)
249] @string
250
251(escape_sequence) @string.escape
252
253(regex) @string.regex
254
255(regex_flags) @keyword.operator.regex
256
257(number) @number
258
259; Tokens
260[
261  ";"
262  "?."
263  "."
264  ","
265  ":"
266  "?"
267] @punctuation.delimiter
268
269[
270  "..."
271  "-"
272  "--"
273  "-="
274  "+"
275  "++"
276  "+="
277  "*"
278  "*="
279  "**"
280  "**="
281  "/"
282  "/="
283  "%"
284  "%="
285  "<"
286  "<="
287  "<<"
288  "<<="
289  "="
290  "=="
291  "==="
292  "!"
293  "!="
294  "!=="
295  "=>"
296  ">"
297  ">="
298  ">>"
299  ">>="
300  ">>>"
301  ">>>="
302  "~"
303  "^"
304  "&"
305  "|"
306  "^="
307  "&="
308  "|="
309  "&&"
310  "||"
311  "??"
312  "&&="
313  "||="
314  "??="
315  "..."
316] @operator
317
318(regex
319  "/" @string.regex)
320
321(ternary_expression
322  [
323    "?"
324    ":"
325  ] @operator)
326
327[
328  "("
329  ")"
330  "["
331  "]"
332  "{"
333  "}"
334] @punctuation.bracket
335
336(template_substitution
337  "${" @punctuation.special
338  "}" @punctuation.special) @embedded
339
340(template_type
341  "${" @punctuation.special
342  "}" @punctuation.special) @embedded
343
344(type_arguments
345  "<" @punctuation.bracket
346  ">" @punctuation.bracket)
347
348(type_parameters
349  "<" @punctuation.bracket
350  ">" @punctuation.bracket)
351
352(decorator
353  "@" @punctuation.special)
354
355(union_type
356  "|" @punctuation.special)
357
358(intersection_type
359  "&" @punctuation.special)
360
361(type_annotation
362  ":" @punctuation.special)
363
364(index_signature
365  ":" @punctuation.special)
366
367(type_predicate_annotation
368  ":" @punctuation.special)
369
370(public_field_definition
371  "?" @punctuation.special)
372
373(property_signature
374  "?" @punctuation.special)
375
376(method_signature
377  "?" @punctuation.special)
378
379(optional_parameter
380  ([
381    "?"
382    ":"
383  ]) @punctuation.special)
384
385; Keywords
386[
387  "abstract"
388  "as"
389  "async"
390  "await"
391  "debugger"
392  "declare"
393  "default"
394  "delete"
395  "extends"
396  "get"
397  "implements"
398  "in"
399  "infer"
400  "instanceof"
401  "is"
402  "keyof"
403  "module"
404  "namespace"
405  "new"
406  "of"
407  "override"
408  "private"
409  "protected"
410  "public"
411  "readonly"
412  "satisfies"
413  "set"
414  "static"
415  "target"
416  "typeof"
417  "using"
418  "void"
419  "with"
420] @keyword
421
422[
423  "const"
424  "let"
425  "var"
426  "function"
427  "class"
428  "enum"
429  "interface"
430  "type"
431] @keyword.declaration
432
433[
434  "export"
435  "from"
436  "import"
437] @keyword.import
438
439[
440  "break"
441  "case"
442  "catch"
443  "continue"
444  "do"
445  "else"
446  "finally"
447  "for"
448  "if"
449  "return"
450  "switch"
451  "throw"
452  "try"
453  "while"
454  "yield"
455] @keyword.control
456
457(switch_default
458  "default" @keyword.control)