Debian Codegraph @

runtime/queries/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 "..." ; variadic `...` in extern fn signatures
55 "'"
56 ] @operator
57
58 ; -------
59 ; Paths
60 ; -------
61
62 (use_declaration
63 argument: (identifier) @namespace)
64 (use_wildcard
65 (identifier) @namespace)
66 (extern_crate_declaration
67 name: (identifier) @namespace
68 alias: (identifier)? @namespace)
69 (mod_item
70 name: (identifier) @namespace)
71 (scoped_use_list
72 path: (identifier)? @namespace)
73 (use_list
74 (identifier) @namespace)
75 (use_as_clause
76 path: (identifier)? @namespace
77 alias: (identifier) @namespace)
78
79 ; -------
80 ; Types
81 ; -------
82
83 (type_parameter
84 name: (type_identifier) @type.parameter)
85 ((type_arguments (type_identifier) @constant)
86 (#match? @constant "^[A-Z_]+$"))
87 (type_arguments (type_identifier) @type)
88 ; `_` placeholder token in any pattern position:
89 ; `let _ =`, `match { _ => }`, `|_|`, `fn f(_: T)`, `(_, _)`, `[_, x]`
90 ("_" @comment.unused)
91 ; `_` in `Vec<_>`
92 ((type_arguments (type_identifier) @comment.unused)
93 (#eq? @comment.unused "_"))
94 ; `_` in `Rc<[_]>`
95 ((array_type (type_identifier) @comment.unused)
96 (#eq? @comment.unused "_"))
97
98 ; ---
99 ; Primitives
100 ; ---
101
102 (escape_sequence) @constant.character.escape
103 (primitive_type) @type.builtin
104 (boolean_literal) @constant.builtin.boolean
105 (integer_literal) @constant.numeric.integer
106 (float_literal) @constant.numeric.float
107 (char_literal) @constant.character
108 [
109 (string_literal)
110 (raw_string_literal)
111 ] @string
112
113 ; -------
114 ; Comments
115 ; -------
116
117 (shebang) @comment
118 (line_comment) @comment.line
119 (block_comment) @comment.block
120
121 ; Doc Comments
122 (line_comment
123 (outer_doc_comment_marker "/" @comment.line.documentation)
124 (doc_comment)) @comment.line.documentation
125 (line_comment
126 (inner_doc_comment_marker "!" @comment.line.documentation)
127 (doc_comment)) @comment.line.documentation
128
129 (block_comment
130 (outer_doc_comment_marker) @comment.block.documentation
131 (doc_comment) "*/" @comment.block.documentation) @comment.block.documentation
132 (block_comment
133 (inner_doc_comment_marker) @comment.block.documentation
134 (doc_comment) "*/" @comment.block.documentation) @comment.block.documentation
135
136 ; ---
137 ; Extraneous
138 ; ---
139
140 (self) @variable.builtin
141
142 (field_initializer
143 (field_identifier) @variable.other.member)
144 (shorthand_field_initializer
145 (identifier) @variable.other.member)
146 (shorthand_field_identifier) @variable.other.member
147
148 (lifetime
149 "'" @label
150 (identifier) @label)
151 (label
152 "'" @label
153 (identifier) @label)
154
155 ; ---
156 ; Punctuation
157 ; ---
158
159 [
160 "::"
161 "."
162 ";"
163 ","
164 ":"
165 ] @punctuation.delimiter
166
167 [
168 "("
169 ")"
170 "["
171 "]"
172 "{"
173 "}"
174 "#"
175 ] @punctuation.bracket
176 (type_arguments
177 [
178 "<"
179 ">"
180 ] @punctuation.bracket)
181 (type_parameters
182 [
183 "<"
184 ">"
185 ] @punctuation.bracket)
186 (for_lifetimes ["<" ">"] @punctuation.bracket)
187 (closure_parameters
188 "|" @punctuation.bracket)
189 (bracketed_type ["<" ">"] @punctuation.bracket)
190
191 ; ---
192 ; Variables
193 ; ---
194
195 (let_declaration
196 pattern: [
197 ((identifier) @variable)
198 ((tuple_pattern
199 (identifier) @variable))
200 ])
201
202 ; It needs to be anonymous to not conflict with `call_expression` further below.
203 (_
204 value: (field_expression
205 value: (identifier)? @variable
206 field: (field_identifier) @variable.other.member))
207
208 (parameter
209 pattern: (identifier) @variable.parameter)
210 (closure_parameters
211 (identifier) @variable.parameter)
212
213 ; Mutable variables
214
215 (let_declaration
216 (mutable_specifier)
217 pattern: (identifier) @variable.mutable)
218 (mut_pattern
219 (mutable_specifier)
220 (identifier) @variable.mutable)
221
222 (parameter
223 (mutable_specifier)
224 pattern: (identifier) @variable.parameter.mutable)
225
226 (self_parameter
227 (mutable_specifier)
228 (self) @variable.builtin.mutable)
229
230 ; -------
231 ; Keywords
232 ; -------
233
234 "in" @keyword.control
235
236 [
237 "match"
238 "if"
239 "else"
240 "try"
241 ] @keyword.control.conditional
242
243 [
244 "while"
245 "loop"
246 ] @keyword.control.repeat
247
248 [
249 "break"
250 "continue"
251 "return"
252 "await"
253 "yield"
254 ] @keyword.control.return
255
256 "use" @keyword.control.import
257 (mod_item "mod" @keyword.control.import !body)
258 (use_as_clause "as" @keyword.control.import)
259
260 (type_cast_expression "as" @keyword.operator)
261
262 [
263 (crate)
264 (super)
265 "as"
266 "pub"
267 "mod"
268 "extern"
269
270 "impl"
271 "where"
272 "trait"
273 "for"
274
275 "default"
276 "async"
277 ] @keyword
278
279 (for_expression
280 "for" @keyword.control.repeat)
281 (gen_block "gen" @keyword.control)
282
283 [
284 "struct"
285 "enum"
286 "union"
287 "type"
288 ] @keyword.storage.type
289
290 "let" @keyword.storage
291 "fn" @keyword.function
292 "unsafe" @keyword.storage.modifier
293 "macro_rules!" @function.macro
294
295 (mutable_specifier) @keyword.storage.modifier.mut
296
297 (reference_type "&" @keyword.storage.modifier.ref)
298 (self_parameter "&" @keyword.storage.modifier.ref)
299
300 [
301 "static"
302 "const"
303 "raw"
304 "ref"
305 "move"
306 "dyn"
307 ] @keyword.storage.modifier
308
309 ; ---
310 ; Remaining Paths
311 ; ---
312
313 (scoped_identifier
314 path: (identifier)? @namespace
315 name: (identifier) @namespace)
316 (scoped_type_identifier
317 path: (identifier) @namespace)
318
319 ; -------
320 ; Functions
321 ; -------
322
323 ; highlight `baz` in `any_function(foo::bar::baz)` as function
324 ; This generically works for an unlimited number of path segments:
325 ;
326 ; - `f(foo::bar)`
327 ; - `f(foo::bar::baz)`
328 ; - `f(foo::bar::baz::quux)`
329 ;
330 ; We know that in the above examples, the last component of each path is a function
331 ; as the only other valid thing (following Rust naming conventions) would be a module at
332 ; that position, however you cannot pass modules as arguments
333 (call_expression
334 function: _
335 arguments: (arguments
336 (scoped_identifier
337 path: _
338 name: (identifier) @function)))
339
340 (call_expression
341 function: [
342 ((identifier) @function)
343 (scoped_identifier
344 name: (identifier) @function)
345 ; method call `obj.method()` — consistent with the generic_function case below
346 (field_expression
347 field: (field_identifier) @function.method)
348 ])
349 (generic_function
350 function: [
351 ((identifier) @function)
352 (scoped_identifier
353 name: (identifier) @function)
354 (field_expression
355 field: (field_identifier) @function.method)
356 ])
357
358 (function_item
359 name: (identifier) @function)
360 (function_signature_item
361 name: (identifier) @function)
362
363 (function_item
364 (visibility_modifier)
365 name: (identifier) @function.public)
366 (function_signature_item
367 (visibility_modifier)
368 name: (identifier) @function.public)
369
370 ; -------
371 ; Guess Other Types
372 ; -------
373 ; Other PascalCase identifiers are assumed to be structs.
374
375 ((identifier) @type
376 (#match? @type "^[A-Z]"))
377
378 (never_type "!" @type)
379
380 ((identifier) @constant
381 (#match? @constant "^[A-Z][A-Z\\d_]*$"))
382
383 ; ---
384 ; PascalCase identifiers in call_expressions (e.g. `Ok()`)
385 ; are assumed to be enum constructors.
386 ; ---
387
388 (call_expression
389 function: [
390 ((identifier) @constructor
391 (#match? @constructor "^[A-Z]"))
392 (scoped_identifier
393 name: ((identifier) @constructor
394 (#match? @constructor "^[A-Z]")))
395 ])
396
397 ; ---
398 ; PascalCase identifiers under a path which is also PascalCase
399 ; are assumed to be constructors if they have methods or fields.
400 ; ---
401
402 (field_expression
403 value: (scoped_identifier
404 path: [
405 (identifier) @type
406 (scoped_identifier
407 name: (identifier) @type)
408 ]
409 name: (identifier) @constructor
410 (#match? @type "^[A-Z]")
411 (#match? @constructor "^[A-Z]")))
412
413 (enum_variant (identifier) @type.enum.variant)
414
415
416 ; -------
417 ; Constructors
418 ; -------
419 ; TODO: this is largely guesswork, remove it once we get actual info from locals.scm or r-a
420
421 (struct_expression
422 name: (type_identifier) @constructor)
423
424 (tuple_struct_pattern
425 type: [
426 (identifier) @constructor
427 (scoped_identifier
428 name: (identifier) @constructor)
429 ])
430 (struct_pattern
431 type: [
432 ((type_identifier) @constructor)
433 (scoped_type_identifier
434 name: (type_identifier) @constructor)
435 ])
436 (match_pattern
437 ((identifier) @constructor) (#match? @constructor "^[A-Z]"))
438 (or_pattern
439 ((identifier) @constructor)
440 ((identifier) @constructor)
441 (#match? @constructor "^[A-Z]"))
442
443 (match_pattern
444 (scoped_identifier
445 path: [(identifier) @type (scoped_identifier name: (identifier) @type)]
446 name: (identifier) @type.enum.variant
447 (#match? @type "^[A-Z]")
448 (#match? @type.enum.variant "^[A-Z]")))
449
450 (match_pattern
451 (struct_pattern
452 type: (scoped_type_identifier
453 path: [(identifier) @type (scoped_identifier name: (identifier) @type)]
454 name: (type_identifier) @type.enum.variant
455 (#match? @type "^[A-Z]")
456 (#match? @type.enum.variant "^[A-Z]"))))
457
458 (match_pattern
459 (tuple_struct_pattern
460 type: (scoped_identifier
461 path: [(identifier) @type (scoped_identifier name: (identifier) @type)]
462 name: (identifier) @type.enum.variant
463 (#match? @type "^[A-Z]")
464 (#match? @type.enum.variant "^[A-Z]"))))
465
466 (match_pattern
467 (scoped_identifier
468 name: (identifier) @constant
469 (#match? @constant "^[A-Z_]+$")))
470
471 ; ---
472 ; Macros
473 ; ---
474
475 (attribute
476 (identifier) @function.macro)
477 (inner_attribute_item "!" @punctuation)
478 (attribute
479 [
480 (identifier) @function.macro
481 (scoped_identifier
482 name: (identifier) @function.macro)
483 ]
484 (token_tree (identifier) @function.macro)?)
485
486 (inner_attribute_item) @attribute
487
488 (macro_definition
489 name: (identifier) @function.macro)
490 (macro_invocation
491 macro: [
492 ((identifier) @function.macro)
493 (scoped_identifier
494 name: (identifier) @function.macro)
495 ]
496 "!" @function.macro)
497
498 (metavariable) @variable.parameter
499 (fragment_specifier) @type
500
501 (attribute
502 (identifier) @special
503 arguments: (token_tree (identifier) @type)
504 (#eq? @special "derive")
505 )
506
507 (token_repetition_pattern) @punctuation.delimiter
508 (token_repetition_pattern [")" "(" "$"] @punctuation.special)
509 (token_repetition_pattern "?" @operator)
510
511 ; ---
512 ; Prelude
513 ; ---
514
515 ((identifier) @type.enum.variant.builtin
516 (#any-of? @type.enum.variant.builtin "Some" "None" "Ok" "Err"))
517
518
519 (call_expression
520 (identifier) @function.builtin
521 (#any-of? @function.builtin
522 "drop"
523 "size_of"
524 "size_of_val"
525 "align_of"
526 "align_of_val"))
527
528 ((type_identifier) @type.builtin
529 (#any-of?
530 @type.builtin
531 "Send"
532 "Sized"
533 "Sync"
534 "Unpin"
535 "Drop"
536 "Fn"
537 "FnMut"
538 "FnOnce"
539 "AsMut"
540 "AsRef"
541 "From"
542 "Into"
543 "DoubleEndedIterator"
544 "ExactSizeIterator"
545 "Extend"
546 "IntoIterator"
547 "Iterator"
548 "Option"
549 "Result"
550 "Clone"
551 "Copy"
552 "Debug"
553 "Default"
554 "Eq"
555 "Hash"
556 "Ord"
557 "PartialEq"
558 "PartialOrd"
559 "ToOwned"
560 "Box"
561 "String"
562 "ToString"
563 "Vec"
564 "FromIterator"
565 "TryFrom"
566 "TryInto"))