Debian Codegraph @

Xtree-house/test-grammars/rust/highlights.scm

raw symbols SCIP
1 ; -------
2 ; Basic identifiers
3 ; -------
4
5 ; We do not style ? as an operator on purpose as it allows styling ? differently, as many highlighters do. @operator.special might have been a better scope, but @special is already documented so the change would break themes (including the intent of the default theme)
6 "?" @special
7
8 (type_identifier) @type
9 (identifier) @variable
10 (field_identifier) @variable.other.member
11
12 ; -------
13 ; Operators
14 ; -------
15
16 [
17 "*"
18 "'"
19 "->"
20 "=>"
21 "<="
22 "="
23 "=="
24 "!"
25 "!="
26 "%"
27 "%="
28 "&"
29 "&="
30 "&&"
31 "|"
32 "|="
33 "||"
34 "^"
35 "^="
36 "*"
37 "*="
38 "-"
39 "-="
40 "+"
41 "+="
42 "/"
43 "/="
44 ">"
45 "<"
46 ">="
47 ">>"
48 "<<"
49 ">>="
50 "<<="
51 "@"
52 ".."
53 "..="
54 "'"
55 ] @operator
56
57 ; -------
58 ; Paths
59 ; -------
60
61 (use_declaration
62 argument: (identifier) @namespace)
63 (use_wildcard
64 (identifier) @namespace)
65 (extern_crate_declaration
66 name: (identifier) @namespace
67 alias: (identifier)? @namespace)
68 (mod_item
69 name: (identifier) @namespace)
70 (scoped_use_list
71 path: (identifier)? @namespace)
72 (use_list
73 (identifier) @namespace)
74 (use_as_clause
75 path: (identifier)? @namespace
76 alias: (identifier) @namespace)
77
78 ; -------
79 ; Types
80 ; -------
81
82 (type_parameters
83 (type_identifier) @type.parameter)
84 (constrained_type_parameter
85 left: (type_identifier) @type.parameter)
86 (optional_type_parameter
87 name: (type_identifier) @type.parameter)
88 ((type_arguments (type_identifier) @constant)
89 (#match? @constant "^[A-Z_]+$"))
90 (type_arguments (type_identifier) @type)
91 (tuple_struct_pattern "_" @comment.unused)
92 ((type_arguments (type_identifier) @comment.unused)
93 (#eq? @comment.unused "_"))
94
95 ; ---
96 ; Primitives
97 ; ---
98
99 (escape_sequence) @constant.character.escape
100 (primitive_type) @type.builtin
101 (boolean_literal) @constant.builtin.boolean
102 (integer_literal) @constant.numeric.integer
103 (float_literal) @constant.numeric.float
104 (char_literal) @constant.character
105 [
106 (string_literal)
107 (raw_string_literal)
108 ] @string
109 (outer_doc_comment_marker "/" @comment)
110 (inner_doc_comment_marker "!" @comment)
111 [
112 (line_comment)
113 (block_comment)
114 ] @comment
115
116 ; ---
117 ; Extraneous
118 ; ---
119
120 (self) @variable.builtin
121
122 (field_initializer
123 (field_identifier) @variable.other.member)
124 (shorthand_field_initializer
125 (identifier) @variable.other.member)
126 (shorthand_field_identifier) @variable.other.member
127
128 (lifetime
129 "'" @label
130 (identifier) @label)
131 (label
132 "'" @label
133 (identifier) @label)
134
135 ; ---
136 ; Punctuation
137 ; ---
138
139 [
140 "::"
141 "."
142 ";"
143 ","
144 ":"
145 ] @punctuation.delimiter
146
147 [
148 "("
149 ")"
150 "["
151 "]"
152 "{"
153 "}"
154 "#"
155 ] @punctuation.bracket
156 (type_arguments
157 [
158 "<"
159 ">"
160 ] @punctuation.bracket)
161 (type_parameters
162 [
163 "<"
164 ">"
165 ] @punctuation.bracket)
166 (for_lifetimes ["<" ">"] @punctuation.bracket)
167 (closure_parameters
168 "|" @punctuation.bracket)
169 (bracketed_type ["<" ">"] @punctuation.bracket)
170
171 ; ---
172 ; Variables
173 ; ---
174
175 (let_declaration
176 pattern: [
177 ((identifier) @variable)
178 ((tuple_pattern
179 (identifier) @variable))
180 ])
181
182 ; It needs to be anonymous to not conflict with `call_expression` further below.
183 (_
184 value: (field_expression
185 value: (identifier)? @variable
186 field: (field_identifier) @variable.other.member))
187
188 (parameter
189 pattern: (identifier) @variable.parameter)
190 (closure_parameters
191 (identifier) @variable.parameter)
192
193 ; -------
194 ; Keywords
195 ; -------
196
197 (for_expression
198 "for" @keyword.control.repeat)
199 (gen_block "gen" @keyword.control)
200
201 "in" @keyword.control
202
203 [
204 "match"
205 "if"
206 "else"
207 "try"
208 ] @keyword.control.conditional
209
210 [
211 "while"
212 "loop"
213 ] @keyword.control.repeat
214
215 [
216 "break"
217 "continue"
218 "return"
219 "await"
220 "yield"
221 ] @keyword.control.return
222
223 "use" @keyword.control.import
224 (mod_item "mod" @keyword.control.import !body)
225 (use_as_clause "as" @keyword.control.import)
226
227 (type_cast_expression "as" @keyword.operator)
228
229 ((generic_type
230 type: (type_identifier) @keyword)
231 (#eq? @keyword "use"))
232
233 [
234 (crate)
235 (super)
236 "as"
237 "pub"
238 "mod"
239 "extern"
240
241 "impl"
242 "where"
243 "trait"
244 "for"
245
246 "default"
247 "async"
248 ] @keyword
249
250 [
251 "struct"
252 "enum"
253 "union"
254 "type"
255 ] @keyword.storage.type
256
257 "let" @keyword.storage
258 "fn" @keyword.function
259 "unsafe" @keyword.special
260 "macro_rules!" @function.macro
261
262 (mutable_specifier) @keyword.storage.modifier.mut
263
264 (reference_type "&" @keyword.storage.modifier.ref)
265 (self_parameter "&" @keyword.storage.modifier.ref)
266
267 [
268 "static"
269 "const"
270 "raw"
271 "ref"
272 "move"
273 "dyn"
274 ] @keyword.storage.modifier
275
276 ; TODO: variable.mut to highlight mutable identifiers via locals.scm
277
278 ; ---
279 ; Remaining Paths
280 ; ---
281
282 (scoped_identifier
283 path: (identifier)? @namespace
284 name: (identifier) @namespace)
285 (scoped_type_identifier
286 path: (identifier) @namespace)
287
288 ; -------
289 ; Functions
290 ; -------
291
292 (call_expression
293 function: [
294 ((identifier) @function)
295 (scoped_identifier
296 name: (identifier) @function)
297 (field_expression
298 field: (field_identifier) @function)
299 ])
300 (generic_function
301 function: [
302 ((identifier) @function)
303 (scoped_identifier
304 name: (identifier) @function)
305 (field_expression
306 field: (field_identifier) @function.method)
307 ])
308
309 (function_item
310 name: (identifier) @function)
311
312 (function_signature_item
313 name: (identifier) @function)
314
315 ; -------
316 ; Guess Other Types
317 ; -------
318 ; Other PascalCase identifiers are assumed to be structs.
319
320 ((identifier) @type
321 (#match? @type "^[A-Z]"))
322
323 (never_type "!" @type)
324
325 ((identifier) @constant
326 (#match? @constant "^[A-Z][A-Z\\d_]*$"))
327
328 ; ---
329 ; PascalCase identifiers in call_expressions (e.g. `Ok()`)
330 ; are assumed to be enum constructors.
331 ; ---
332
333 (call_expression
334 function: [
335 ((identifier) @constructor
336 (#match? @constructor "^[A-Z]"))
337 (scoped_identifier
338 name: ((identifier) @constructor
339 (#match? @constructor "^[A-Z]")))
340 ])
341
342 ; ---
343 ; PascalCase identifiers under a path which is also PascalCase
344 ; are assumed to be constructors if they have methods or fields.
345 ; ---
346
347 (field_expression
348 value: (scoped_identifier
349 path: [
350 (identifier) @type
351 (scoped_identifier
352 name: (identifier) @type)
353 ]
354 name: (identifier) @constructor
355 (#match? @type "^[A-Z]")
356 (#match? @constructor "^[A-Z]")))
357
358 (enum_variant (identifier) @type.enum.variant)
359
360
361 ; -------
362 ; Constructors
363 ; -------
364 ; TODO: this is largely guesswork, remove it once we get actual info from locals.scm or r-a
365
366 (struct_expression
367 name: (type_identifier) @constructor)
368
369 (tuple_struct_pattern
370 type: [
371 (identifier) @constructor
372 (scoped_identifier
373 name: (identifier) @constructor)
374 ])
375 (struct_pattern
376 type: [
377 ((type_identifier) @constructor)
378 (scoped_type_identifier
379 name: (type_identifier) @constructor)
380 ])
381 (match_pattern
382 ((identifier) @constructor) (#match? @constructor "^[A-Z]"))
383 (or_pattern
384 ((identifier) @constructor)
385 ((identifier) @constructor)
386 (#match? @constructor "^[A-Z]"))
387
388 ; ---
389 ; Macros
390 ; ---
391
392 (attribute
393 (identifier) @function.macro)
394 (inner_attribute_item "!" @punctuation)
395 (attribute
396 [
397 (identifier) @function.macro
398 (scoped_identifier
399 name: (identifier) @function.macro)
400 ]
401 (token_tree (identifier) @function.macro)?)
402
403 (inner_attribute_item) @attribute
404
405 (macro_definition
406 name: (identifier) @function.macro)
407 (macro_invocation
408 macro: [
409 ((identifier) @function.macro)
410 (scoped_identifier
411 name: (identifier) @function.macro)
412 ]
413 "!" @function.macro)
414
415 (metavariable) @variable.parameter
416 (fragment_specifier) @type
417
418 (attribute
419 (identifier) @special
420 arguments: (token_tree (identifier) @type)
421 (#eq? @special "derive")
422 )
423
424 ; ---
425 ; Prelude
426 ; ---
427
428 ((identifier) @type.enum.variant.builtin
429 (#any-of? @type.enum.variant.builtin "Some" "None" "Ok" "Err"))
430
431
432 (call_expression
433 (identifier) @function.builtin
434 (#any-of? @function.builtin
435 "drop"
436 "size_of"
437 "size_of_val"
438 "align_of"
439 "align_of_val"))
440
441 ((type_identifier) @type.builtin
442 (#any-of?
443 @type.builtin
444 "Send"
445 "Sized"
446 "Sync"
447 "Unpin"
448 "Drop"
449 "Fn"
450 "FnMut"
451 "FnOnce"
452 "AsMut"
453 "AsRef"
454 "From"
455 "Into"
456 "DoubleEndedIterator"
457 "ExactSizeIterator"
458 "Extend"
459 "IntoIterator"
460 "Iterator"
461 "Option"
462 "Result"
463 "Clone"
464 "Copy"
465 "Debug"
466 "Default"
467 "Eq"
468 "Hash"
469 "Ord"
470 "PartialEq"
471 "PartialOrd"
472 "ToOwned"
473 "Box"
474 "String"
475 "ToString"
476 "Vec"
477 "FromIterator"
478 "TryFrom"
479 "TryInto"))