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.scm445
1 files changed, 163 insertions, 282 deletions
diff --git a/runtime/queries/rust/highlights.scm b/runtime/queries/rust/highlights.scm
index cc8380cb..d3c29270 100644
--- a/runtime/queries/rust/highlights.scm
+++ b/runtime/queries/rust/highlights.scm
@@ -1,98 +1,14 @@
; -------
-; 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
; ---
@@ -107,35 +23,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)
@@ -146,9 +44,8 @@
(lifetime
"'" @label
(identifier) @label)
-(label
- "'" @label
- (identifier) @label)
+(loop_label
+ (identifier) @type)
; ---
; Punctuation
@@ -159,7 +56,6 @@
"."
";"
","
- ":"
] @punctuation.delimiter
[
@@ -181,10 +77,8 @@
"<"
">"
] @punctuation.bracket)
-(for_lifetimes ["<" ">"] @punctuation.bracket)
(closure_parameters
"|" @punctuation.bracket)
-(bracketed_type ["<" ">"] @punctuation.bracket)
; ---
; Variables
@@ -208,17 +102,23 @@
(closure_parameters
(identifier) @variable.parameter)
+
+
; -------
; Keywords
; -------
+(for_expression
+ "for" @keyword.control.repeat)
+((identifier) @keyword.control
+ (#match? @keyword.control "^yield$"))
+
"in" @keyword.control
[
"match"
"if"
"else"
- "try"
] @keyword.control.conditional
[
@@ -229,9 +129,10 @@
[
"break"
"continue"
+
"return"
+
"await"
- "yield"
] @keyword.control.return
"use" @keyword.control.import
@@ -253,25 +154,24 @@
"trait"
"for"
+ "unsafe"
"default"
+ "macro_rules!"
+
"async"
] @keyword
-(for_expression
- "for" @keyword.control.repeat)
-(gen_block "gen" @keyword.control)
-
[
"struct"
"enum"
"union"
+
"type"
] @keyword.storage.type
"let" @keyword.storage
+
"fn" @keyword.function
-"unsafe" @keyword.special
-"macro_rules!" @function.macro
(mutable_specifier) @keyword.storage.modifier.mut
@@ -281,7 +181,6 @@
[
"static"
"const"
- "raw"
"ref"
"move"
"dyn"
@@ -289,37 +188,69 @@
; TODO: variable.mut to highlight mutable identifiers via locals.scm
+; -------
+; Guess Other Types
+; -------
+
+((identifier) @constant
+ (#match? @constant "^[A-Z][A-Z\\d_]*$"))
+
; ---
-; Remaining Paths
+; PascalCase identifiers in call_expressions (e.g. `Ok()`)
+; are assumed to be enum constructors.
; ---
-(scoped_identifier
- path: (identifier)? @namespace
- name: (identifier) @namespace)
-(scoped_type_identifier
- path: (identifier) @namespace)
+(call_expression
+ function: [
+ ((identifier) @type.variant
+ (#match? @type.variant "^[A-Z]"))
+ (scoped_identifier
+ name: ((identifier) @type.variant
+ (#match? @type.variant "^[A-Z]")))
+ ])
+
+; ---
+; 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)
+ ])
+
+; ---
+; Other PascalCase identifiers are assumed to be structs.
+; ---
+
+((identifier) @type
+ (#match? @type "^[A-Z]"))
+
+
+
+(attribute
+ (identifier) @_macro
+ arguments: (token_tree (identifier) @constant.numeric.integer)
+ (#eq? @_macro "derive")
+)
+@special
+
; -------
; 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)
@@ -343,86 +274,12 @@
(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
@@ -446,69 +303,93 @@
(metavariable) @variable.parameter
(fragment_specifier) @type
-(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) @variable.other.member