highlights.scm

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