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 | 398 |
1 files changed, 147 insertions, 251 deletions
diff --git a/runtime/queries/rust/highlights.scm b/runtime/queries/rust/highlights.scm index cc8380cb..7997c5ea 100644 --- a/runtime/queries/rust/highlights.scm +++ b/runtime/queries/rust/highlights.scm @@ -1,97 +1,20 @@ ; ------- -; 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 +(type_parameters + (type_identifier) @type.parameter) +(constrained_type_parameter + left: (type_identifier) @type.parameter) +(optional_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 @@ -107,35 +30,17 @@ (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) @@ -159,7 +64,6 @@ "." ";" "," - ":" ] @punctuation.delimiter [ @@ -181,10 +85,8 @@ "<" ">" ] @punctuation.bracket) -(for_lifetimes ["<" ">"] @punctuation.bracket) (closure_parameters "|" @punctuation.bracket) -(bracketed_type ["<" ">"] @punctuation.bracket) ; --- ; Variables @@ -212,6 +114,11 @@ ; Keywords ; ------- +(for_expression + "for" @keyword.control.repeat) +((identifier) @keyword.control + (#match? @keyword.control "^yield$")) + "in" @keyword.control [ @@ -231,7 +138,6 @@ "continue" "return" "await" - "yield" ] @keyword.control.return "use" @keyword.control.import @@ -257,10 +163,6 @@ "async" ] @keyword -(for_expression - "for" @keyword.control.repeat) -(gen_block "gen" @keyword.control) - [ "struct" "enum" @@ -281,7 +183,6 @@ [ "static" "const" - "raw" "ref" "move" "dyn" @@ -289,69 +190,36 @@ ; TODO: variable.mut to highlight mutable identifiers via locals.scm -; --- -; Remaining Paths -; --- - -(scoped_identifier - path: (identifier)? @namespace - name: (identifier) @namespace) -(scoped_type_identifier - path: (identifier) @namespace) - ; ------- -; Functions +; Constructors ; ------- +; TODO: this is largely guesswork, remove it once we get actual info from locals.scm or r-a -; 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))) +(struct_expression + name: (type_identifier) @constructor) -(call_expression - function: [ - ((identifier) @function) +(tuple_struct_pattern + type: [ + (identifier) @constructor (scoped_identifier - name: (identifier) @function) - (field_expression - field: (field_identifier) @function) + name: (identifier) @constructor) ]) -(generic_function - function: [ - ((identifier) @function) - (scoped_identifier - name: (identifier) @function) - (field_expression - field: (field_identifier) @function.method) +(struct_pattern + type: [ + ((type_identifier) @constructor) + (scoped_type_identifier + name: (type_identifier) @constructor) ]) - -(function_item - name: (identifier) @function) - -(function_signature_item - name: (identifier) @function) +(match_pattern + ((identifier) @constructor) (#match? @constructor "^[A-Z]")) +(or_pattern + ((identifier) @constructor) + ((identifier) @constructor) + (#match? @constructor "^[A-Z]")) ; ------- ; 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_]*$")) @@ -386,43 +254,52 @@ (#match? @type "^[A-Z]") (#match? @constructor "^[A-Z]"))) -(enum_variant (identifier) @type.enum.variant) +; --- +; Other PascalCase identifiers are assumed to be structs. +; --- +((identifier) @type + (#match? @type "^[A-Z]")) ; ------- -; Constructors +; Functions ; ------- -; 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 +(call_expression + function: [ + ((identifier) @function) (scoped_identifier - name: (identifier) @constructor) + name: (identifier) @function) + (field_expression + field: (field_identifier) @function) ]) -(struct_pattern - type: [ - ((type_identifier) @constructor) - (scoped_type_identifier - name: (type_identifier) @constructor) +(generic_function + function: [ + ((identifier) @function) + (scoped_identifier + name: (identifier) @function) + (field_expression + field: (field_identifier) @function.method) ]) -(match_pattern - ((identifier) @constructor) (#match? @constructor "^[A-Z]")) -(or_pattern - ((identifier) @constructor) - ((identifier) @constructor) - (#match? @constructor "^[A-Z]")) + +(function_item + name: (identifier) @function) + +(function_signature_item + name: (identifier) @function) ; --- ; Macros ; --- (attribute + (identifier) @special + arguments: (token_tree (identifier) @type) + (#eq? @special "derive") +) + +(attribute (identifier) @function.macro) -(inner_attribute_item "!" @punctuation) (attribute [ (identifier) @function.macro @@ -446,69 +323,88 @@ (metavariable) @variable.parameter (fragment_specifier) @type -(attribute - (identifier) @special - arguments: (token_tree (identifier) @type) - (#eq? @special "derive") -) +; ------- +; Operators +; ------- + +[ + "*" + "'" + "->" + "=>" + "<=" + "=" + "==" + "!" + "!=" + "%" + "%=" + "&" + "&=" + "&&" + "|" + "|=" + "||" + "^" + "^=" + "*" + "*=" + "-" + "-=" + "+" + "+=" + "/" + "/=" + ">" + "<" + ">=" + ">>" + "<<" + ">>=" + "<<=" + "@" + ".." + "..=" + "'" +] @operator -(token_repetition_pattern) @punctuation.delimiter -(token_repetition_pattern [")" "(" "$"] @punctuation.special) -(token_repetition_pattern "?" @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) ; --- -; 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) +; ------- +; Remaining Identifiers +; ------- -(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")) +"?" @special + +(type_identifier) @type +(identifier) @variable +(field_identifier) @variable.other.member |