Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'runtime/queries/rust/highlights.scm')
| -rw-r--r-- | runtime/queries/rust/highlights.scm | 535 |
1 files changed, 180 insertions, 355 deletions
diff --git a/runtime/queries/rust/highlights.scm b/runtime/queries/rust/highlights.scm index cc8380cb..956a5dac 100644 --- a/runtime/queries/rust/highlights.scm +++ b/runtime/queries/rust/highlights.scm @@ -1,154 +1,55 @@ ; ------- -; Basic identifiers +; Tree-Sitter doesn't allow overrides in regards to captures, +; though it is possible to affect the child node of a captured +; node. Thus, the approach here is to flip the order so that +; overrides are unnecessary. ; ------- -; 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) -"?" @special -(type_identifier) @type -(identifier) @variable -(field_identifier) @variable.other.member - -; ------- -; Operators -; ------- - -[ - "*" - "'" - "->" - "=>" - "<=" - "=" - "==" - "!" - "!=" - "%" - "%=" - "&" - "&=" - "&&" - "|" - "|=" - "||" - "^" - "^=" - "*" - "*=" - "-" - "-=" - "+" - "+=" - "/" - "/=" - ">" - "<" - ">=" - ">>" - "<<" - ">>=" - "<<=" - "@" - ".." - "..=" - "'" -] @operator - -; ------- -; Paths -; ------- - -(use_declaration - argument: (identifier) @namespace) -(use_wildcard - (identifier) @namespace) -(extern_crate_declaration - name: (identifier) @namespace - alias: (identifier)? @namespace) -(mod_item - name: (identifier) @namespace) -(scoped_use_list - path: (identifier)? @namespace) -(use_list - (identifier) @namespace) -(use_as_clause - path: (identifier)? @namespace - alias: (identifier) @namespace) ; ------- ; Types ; ------- -(type_parameter - name: (type_identifier) @type.parameter) -((type_arguments (type_identifier) @constant) - (#match? @constant "^[A-Z_]+$")) -(type_arguments (type_identifier) @type) -; `_` in `(_, _)` -(tuple_struct_pattern "_" @comment.unused) -; `_` in `Vec<_>` -((type_arguments (type_identifier) @comment.unused) - (#eq? @comment.unused "_")) -; `_` in `Rc<[_]>` -((array_type (type_identifier) @comment.unused) - (#eq? @comment.unused "_")) - ; --- ; Primitives ; --- -(escape_sequence) @constant.character.escape +(escape_sequence) @escape (primitive_type) @type.builtin (boolean_literal) @constant.builtin.boolean -(integer_literal) @constant.numeric.integer -(float_literal) @constant.numeric.float -(char_literal) @constant.character [ + (integer_literal) + (float_literal) +] @number +[ + (char_literal) (string_literal) (raw_string_literal) ] @string - -; ------- -; Comments -; ------- - -(shebang) @comment -(line_comment) @comment.line -(block_comment) @comment.block - -; Doc Comments -(line_comment - (outer_doc_comment_marker "/" @comment.line.documentation) - (doc_comment)) @comment.line.documentation -(line_comment - (inner_doc_comment_marker "!" @comment.line.documentation) - (doc_comment)) @comment.line.documentation - -(block_comment - (outer_doc_comment_marker) @comment.block.documentation - (doc_comment) "*/" @comment.block.documentation) @comment.block.documentation -(block_comment - (inner_doc_comment_marker) @comment.block.documentation - (doc_comment) "*/" @comment.block.documentation) @comment.block.documentation +[ + (line_comment) + (block_comment) +] @comment ; --- ; Extraneous ; --- (self) @variable.builtin +(enum_variant (identifier) @type.enum.variant) (field_initializer - (field_identifier) @variable.other.member) + (field_identifier) @property) (shorthand_field_initializer - (identifier) @variable.other.member) -(shorthand_field_identifier) @variable.other.member + (identifier) @variable.property) +(shorthand_field_identifier) @variable.property (lifetime "'" @label (identifier) @label) -(label - "'" @label - (identifier) @label) +(loop_label + (identifier) @type) ; --- ; Punctuation @@ -158,8 +59,6 @@ "::" "." ";" - "," - ":" ] @punctuation.delimiter [ @@ -169,7 +68,6 @@ "]" "{" "}" - "#" ] @punctuation.bracket (type_arguments [ @@ -181,145 +79,133 @@ "<" ">" ] @punctuation.bracket) -(for_lifetimes ["<" ">"] @punctuation.bracket) -(closure_parameters - "|" @punctuation.bracket) -(bracketed_type ["<" ">"] @punctuation.bracket) ; --- -; Variables +; Parameters ; --- -(let_declaration - pattern: [ - ((identifier) @variable) - ((tuple_pattern - (identifier) @variable)) - ]) - -; It needs to be anonymous to not conflict with `call_expression` further below. -(_ - value: (field_expression - value: (identifier)? @variable - field: (field_identifier) @variable.other.member)) - (parameter pattern: (identifier) @variable.parameter) (closure_parameters (identifier) @variable.parameter) + + ; ------- ; Keywords ; ------- -"in" @keyword.control - -[ - "match" - "if" - "else" - "try" -] @keyword.control.conditional - +(for_expression + "for" @keyword.control) +((identifier) @keyword.control + (#match? @keyword.control "^yield$")) [ "while" "loop" -] @keyword.control.repeat - -[ + "in" "break" "continue" - "return" - "await" - "yield" -] @keyword.control.return -"use" @keyword.control.import -(mod_item "mod" @keyword.control.import !body) -(use_as_clause "as" @keyword.control.import) + "match" + "if" + "else" + "return" -(type_cast_expression "as" @keyword.operator) + "await" +] @keyword.control [ (crate) (super) "as" + "use" "pub" "mod" "extern" + "fn" + "struct" + "enum" "impl" "where" "trait" "for" + "type" + "union" + "unsafe" "default" + "macro_rules!" + + "let" + "ref" + "move" + + "dyn" + "static" + "const" "async" ] @keyword -(for_expression - "for" @keyword.control.repeat) -(gen_block "gen" @keyword.control) +(mutable_specifier) @keyword.mut -[ - "struct" - "enum" - "union" - "type" -] @keyword.storage.type +; TODO: variable.mut to highlight mutable identifiers via locals.scm -"let" @keyword.storage -"fn" @keyword.function -"unsafe" @keyword.special -"macro_rules!" @function.macro +; ------- +; Guess Other Types +; ------- -(mutable_specifier) @keyword.storage.modifier.mut +((identifier) @constant + (#match? @constant "^[A-Z][A-Z\\d_]+$")) -(reference_type "&" @keyword.storage.modifier.ref) -(self_parameter "&" @keyword.storage.modifier.ref) +; --- +; PascalCase identifiers in call_expressions (e.g. `Ok()`) +; are assumed to be enum constructors. +; --- -[ - "static" - "const" - "raw" - "ref" - "move" - "dyn" -] @keyword.storage.modifier +(call_expression + function: [ + ((identifier) @type.variant + (#match? @type.variant "^[A-Z]")) + (scoped_identifier + name: ((identifier) @type.variant + (#match? @type.variant "^[A-Z]"))) + ]) -; TODO: variable.mut to highlight mutable identifiers via locals.scm +; --- +; Assume that types in match arms are enums and not +; tuple structs. Same for `if let` expressions. +; --- + +(match_pattern + (scoped_identifier + name: (identifier) @constructor)) +(tuple_struct_pattern + type: [ + ((identifier) @constructor) + (scoped_identifier + name: (identifier) @constructor) + ]) +(struct_pattern + type: [ + ((type_identifier) @constructor) + (scoped_type_identifier + name: (type_identifier) @constructor) + ]) ; --- -; Remaining Paths +; Other PascalCase identifiers are assumed to be structs. ; --- -(scoped_identifier - path: (identifier)? @namespace - name: (identifier) @namespace) -(scoped_type_identifier - path: (identifier) @namespace) +((identifier) @type + (#match? @type "^[A-Z]")) + + ; ------- ; Functions ; ------- -; highlight `baz` in `any_function(foo::bar::baz)` as function -; This generically works for an unlimited number of path segments: -; -; - `f(foo::bar)` -; - `f(foo::bar::baz)` -; - `f(foo::bar::baz::quux)` -; -; We know that in the above examples, the last component of each path is a function -; as the only other valid thing (following Rust naming conventions) would be a module at -; that position, however you cannot pass modules as arguments -(call_expression - function: _ - arguments: (arguments - (scoped_identifier - path: _ - name: (identifier) @function))) - (call_expression function: [ ((identifier) @function) @@ -340,97 +226,13 @@ (function_item name: (identifier) @function) -(function_signature_item - name: (identifier) @function) - -; ------- -; Guess Other Types -; ------- -; Other PascalCase identifiers are assumed to be structs. - -((identifier) @type - (#match? @type "^[A-Z]")) - -(never_type "!" @type) - -((identifier) @constant - (#match? @constant "^[A-Z][A-Z\\d_]*$")) - -; --- -; PascalCase identifiers in call_expressions (e.g. `Ok()`) -; are assumed to be enum constructors. -; --- - -(call_expression - function: [ - ((identifier) @constructor - (#match? @constructor "^[A-Z]")) - (scoped_identifier - name: ((identifier) @constructor - (#match? @constructor "^[A-Z]"))) - ]) - -; --- -; PascalCase identifiers under a path which is also PascalCase -; are assumed to be constructors if they have methods or fields. -; --- - -(field_expression - value: (scoped_identifier - path: [ - (identifier) @type - (scoped_identifier - name: (identifier) @type) - ] - name: (identifier) @constructor - (#match? @type "^[A-Z]") - (#match? @constructor "^[A-Z]"))) - -(enum_variant (identifier) @type.enum.variant) - - -; ------- -; Constructors -; ------- -; TODO: this is largely guesswork, remove it once we get actual info from locals.scm or r-a - -(struct_expression - name: (type_identifier) @constructor) - -(tuple_struct_pattern - type: [ - (identifier) @constructor - (scoped_identifier - name: (identifier) @constructor) - ]) -(struct_pattern - type: [ - ((type_identifier) @constructor) - (scoped_type_identifier - name: (type_identifier) @constructor) - ]) -(match_pattern - ((identifier) @constructor) (#match? @constructor "^[A-Z]")) -(or_pattern - ((identifier) @constructor) - ((identifier) @constructor) - (#match? @constructor "^[A-Z]")) - ; --- ; Macros ; --- -(attribute - (identifier) @function.macro) -(inner_attribute_item "!" @punctuation) -(attribute - [ - (identifier) @function.macro - (scoped_identifier - name: (identifier) @function.macro) - ] - (token_tree (identifier) @function.macro)?) - +(meta_item + (identifier) @attribute) +(attribute_item) @attribute (inner_attribute_item) @attribute (macro_definition @@ -444,71 +246,94 @@ "!" @function.macro) (metavariable) @variable.parameter -(fragment_specifier) @type +(fragment_specifier) @variable.parameter -(attribute - (identifier) @special - arguments: (token_tree (identifier) @type) - (#eq? @special "derive") -) -(token_repetition_pattern) @punctuation.delimiter -(token_repetition_pattern [")" "(" "$"] @punctuation.special) -(token_repetition_pattern "?" @operator) + +; ------- +; Operators +; ------- + +[ + "*" + "'" + "->" + "=>" + "<=" + "=" + "==" + "!" + "!=" + "%" + "%=" + "&" + "&=" + "&&" + "|" + "|=" + "||" + "^" + "^=" + "*" + "*=" + "-" + "-=" + "+" + "+=" + "/" + "/=" + ">" + "<" + ">=" + ">>" + "<<" + ">>=" + "@" + ".." + "..=" + "'" +] @operator + + + +; ------- +; Paths +; ------- + +(use_declaration + argument: (identifier) @namespace) +(use_wildcard + (identifier) @namespace) +(extern_crate_declaration + name: (identifier) @namespace) +(mod_item + name: (identifier) @namespace) +(scoped_use_list + path: (identifier)? @namespace) +(use_list + (identifier) @namespace) +(use_as_clause + path: (identifier)? @namespace + alias: (identifier) @namespace) ; --- -; Prelude +; Remaining Paths ; --- -((identifier) @type.enum.variant.builtin - (#any-of? @type.enum.variant.builtin "Some" "None" "Ok" "Err")) +(scoped_identifier + path: (identifier)? @namespace + name: (identifier) @namespace) +(scoped_type_identifier + path: (identifier) @namespace) + -(call_expression - (identifier) @function.builtin - (#any-of? @function.builtin - "drop" - "size_of" - "size_of_val" - "align_of" - "align_of_val")) - -((type_identifier) @type.builtin - (#any-of? - @type.builtin - "Send" - "Sized" - "Sync" - "Unpin" - "Drop" - "Fn" - "FnMut" - "FnOnce" - "AsMut" - "AsRef" - "From" - "Into" - "DoubleEndedIterator" - "ExactSizeIterator" - "Extend" - "IntoIterator" - "Iterator" - "Option" - "Result" - "Clone" - "Copy" - "Debug" - "Default" - "Eq" - "Hash" - "Ord" - "PartialEq" - "PartialOrd" - "ToOwned" - "Box" - "String" - "ToString" - "Vec" - "FromIterator" - "TryFrom" - "TryInto")) +; ------- +; Remaining Identifiers +; ------- + +"?" @special + +(type_identifier) @type +(identifier) @variable +(field_identifier) @property |