highlights.scm

  1; Variables
  2
  3(identifier) @variable
  4
  5; Special identifiers
  6
  7((identifier) @type
  8 (#match? @type "^[A-Z]"))
  9(type_identifier) @type
 10(predefined_type) @type.builtin
 11
 12;; Enables ts-pretty-errors
 13;; The Lsp returns "snippets" of typescript, which are not valid typescript in totality,
 14;; but should still be highlighted
 15;; Highlights object literals by hijacking the statement_block pattern, but only if
 16;; the statement block follows an object literal pattern
 17((statement_block
 18   (labeled_statement
 19     ;; highlight the label like a property name
 20     label: (statement_identifier) @property.name
 21     body: [
 22       ;; match a terminating expression statement
 23       (expression_statement
 24            ;; single identifier - treat as a type name
 25           [(identifier) @type.name
 26            ;; object - treat as a property - type pair
 27            (object
 28                (pair
 29                    key: (_) @property.name
 30                    value: (_) @type.name))
 31            ;; subscript_expression - treat as an array declaration
 32            (subscript_expression
 33                object: (_) @type.name
 34                index: (_)
 35                )
 36            ;; templated string - treat each identifier contained as a type name
 37            (template_string
 38                (template_substitution
 39                    (identifier) @type.name))
 40            ])
 41       ;; match a nested statement block
 42       (statement_block) @nested
 43     ])))
 44
 45(import_specifier
 46  "type"
 47  name: (identifier) @type
 48  alias: (identifier) @type
 49)
 50
 51(import_statement
 52  "type"
 53  (import_clause
 54    (named_imports
 55      (import_specifier
 56        name: (identifier) @type
 57        alias: (identifier) @type
 58      )
 59    )
 60  )
 61)
 62
 63([
 64  (identifier)
 65  (shorthand_property_identifier)
 66  (shorthand_property_identifier_pattern)
 67 ] @constant
 68 (#match? @constant "^_*[A-Z_][A-Z\\d_]*$"))
 69
 70; Properties
 71
 72(property_identifier) @property
 73(shorthand_property_identifier) @property
 74(shorthand_property_identifier_pattern) @property
 75(private_property_identifier) @property
 76
 77; Function and method calls
 78
 79(call_expression
 80  function: (identifier) @function)
 81
 82(call_expression
 83  function: (member_expression
 84    property: [(property_identifier) (private_property_identifier)] @function.method))
 85
 86; Function and method definitions
 87
 88(function_expression
 89  name: (identifier) @function)
 90(function_declaration
 91  name: (identifier) @function)
 92(method_definition
 93  name: [(property_identifier) (private_property_identifier)] @function.method)
 94(method_definition
 95    name: (property_identifier) @constructor
 96    (#eq? @constructor "constructor"))
 97
 98(pair
 99  key: [(property_identifier) (private_property_identifier)] @function.method
100  value: [(function_expression) (arrow_function)])
101
102(assignment_expression
103  left: (member_expression
104    property: [(property_identifier) (private_property_identifier)] @function.method)
105  right: [(function_expression) (arrow_function)])
106
107(variable_declarator
108  name: (identifier) @function
109  value: [(function_expression) (arrow_function)])
110
111(assignment_expression
112  left: (identifier) @function
113  right: [(function_expression) (arrow_function)])
114
115(arrow_function) @function
116
117; Literals
118
119(this) @variable.special
120(super) @variable.special
121
122[
123  (null)
124  (undefined)
125] @constant.builtin
126
127[
128  (true)
129  (false)
130] @boolean
131
132(literal_type
133  [
134    (null)
135    (undefined)
136    (true)
137    (false)
138  ] @type.builtin
139)
140
141(comment) @comment
142
143(hash_bang_line) @comment
144
145[
146  (string)
147  (template_string)
148  (template_literal_type)
149] @string
150
151(escape_sequence) @string.escape
152
153(regex) @string.regex
154(regex_flags) @keyword.operator.regex
155(number) @number
156
157; Tokens
158
159[
160  ";"
161  "?."
162  "."
163  ","
164  ":"
165  "?"
166] @punctuation.delimiter
167
168[
169  "..."
170  "-"
171  "--"
172  "-="
173  "+"
174  "++"
175  "+="
176  "*"
177  "*="
178  "**"
179  "**="
180  "/"
181  "/="
182  "%"
183  "%="
184  "<"
185  "<="
186  "<<"
187  "<<="
188  "="
189  "=="
190  "==="
191  "!"
192  "!="
193  "!=="
194  "=>"
195  ">"
196  ">="
197  ">>"
198  ">>="
199  ">>>"
200  ">>>="
201  "~"
202  "^"
203  "&"
204  "|"
205  "^="
206  "&="
207  "|="
208  "&&"
209  "||"
210  "??"
211  "&&="
212  "||="
213  "??="
214  "..."
215] @operator
216
217(regex "/" @string.regex)
218
219(ternary_expression
220  [
221    "?"
222    ":"
223  ] @operator
224)
225
226[
227  "("
228  ")"
229  "["
230  "]"
231  "{"
232  "}"
233]  @punctuation.bracket
234
235(template_substitution
236  "${" @punctuation.special
237  "}" @punctuation.special) @embedded
238
239(template_type
240  "${" @punctuation.special
241  "}" @punctuation.special) @embedded
242
243(type_arguments
244  "<" @punctuation.bracket
245  ">" @punctuation.bracket)
246
247(decorator "@" @punctuation.special)
248
249; Keywords
250
251[
252  "abstract"
253  "as"
254  "async"
255  "await"
256  "class"
257  "const"
258  "debugger"
259  "declare"
260  "default"
261  "delete"
262  "enum"
263  "export"
264  "extends"
265  "from"
266  "function"
267  "get"
268  "implements"
269  "import"
270  "in"
271  "infer"
272  "instanceof"
273  "interface"
274  "is"
275  "keyof"
276  "let"
277  "module"
278  "namespace"
279  "new"
280  "of"
281  "override"
282  "private"
283  "protected"
284  "public"
285  "readonly"
286  "satisfies"
287  "set"
288  "static"
289  "target"
290  "type"
291  "typeof"
292  "using"
293  "var"
294  "void"
295  "with"
296] @keyword
297
298[
299  "break"
300  "case"
301  "catch"
302  "continue"
303  "do"
304  "else"
305  "finally"
306  "for"
307  "if"
308  "return"
309  "switch"
310  "throw"
311  "try"
312  "while"
313  "yield"
314] @keyword.control
315
316(switch_default "default" @keyword.control)