style(rust): better formatting

This commit is contained in:
Amaan Qureshi 2023-05-08 21:07:41 -04:00
parent 6df6dd2622
commit 5182759e17

View file

@ -5,57 +5,71 @@
; Identifier conventions ; Identifier conventions
(identifier) @variable (identifier) @variable
((identifier) @type ((identifier) @type
(#lua-match? @type "^[A-Z]")) (#lua-match? @type "^[A-Z]"))
(const_item (const_item
name: (identifier) @constant) name: (identifier) @constant)
; Assume all-caps names are constants ; Assume all-caps names are constants
((identifier) @constant ((identifier) @constant
(#lua-match? @constant "^[A-Z][A-Z%d_]*$")) (#lua-match? @constant "^[A-Z][A-Z%d_]*$"))
; Other identifiers ; Other identifiers
(type_identifier) @type (type_identifier) @type
(primitive_type) @type.builtin (primitive_type) @type.builtin
(field_identifier) @field (field_identifier) @field
(shorthand_field_initializer
(identifier) @field) (shorthand_field_initializer (identifier) @field)
(mod_item (mod_item
name: (identifier) @namespace) name: (identifier) @namespace)
(self) @variable.builtin (self) @variable.builtin
(loop_label ["'" (identifier)] @label) (loop_label ["'" (identifier)] @label)
; Function definitions ; Function definitions
(function_item (identifier) @function) (function_item (identifier) @function)
(function_signature_item (identifier) @function) (function_signature_item (identifier) @function)
(parameter (identifier) @parameter) (parameter (identifier) @parameter)
(closure_parameters (_) @parameter) (closure_parameters (_) @parameter)
; Function calls ; Function calls
(call_expression (call_expression
function: (identifier) @function.call) function: (identifier) @function.call)
(call_expression (call_expression
function: (scoped_identifier function: (scoped_identifier
(identifier) @function.call .)) (identifier) @function.call .))
(call_expression (call_expression
function: (field_expression function: (field_expression
field: (field_identifier) @function.call)) field: (field_identifier) @function.call))
(generic_function (generic_function
function: (identifier) @function.call) function: (identifier) @function.call)
(generic_function (generic_function
function: (scoped_identifier function: (scoped_identifier
name: (identifier) @function.call)) name: (identifier) @function.call))
(generic_function (generic_function
function: (field_expression function: (field_expression
field: (field_identifier) @function.call)) field: (field_identifier) @function.call))
; Assume other uppercase names are enum constructors ; Assume other uppercase names are enum constructors
((field_identifier) @constant ((field_identifier) @constant
(#lua-match? @constant "^[A-Z]")) (#lua-match? @constant "^[A-Z]"))
@ -63,25 +77,33 @@
name: (identifier) @constant) name: (identifier) @constant)
; Assume that uppercase names in paths are types ; Assume that uppercase names in paths are types
(scoped_identifier (scoped_identifier
path: (identifier) @namespace) path: (identifier) @namespace)
(scoped_identifier (scoped_identifier
(scoped_identifier (scoped_identifier
name: (identifier) @namespace)) name: (identifier) @namespace))
(scoped_type_identifier (scoped_type_identifier
path: (identifier) @namespace) path: (identifier) @namespace)
(scoped_type_identifier (scoped_type_identifier
path: (identifier) @type path: (identifier) @type
(#lua-match? @type "^[A-Z]")) (#lua-match? @type "^[A-Z]"))
(scoped_type_identifier (scoped_type_identifier
(scoped_identifier (scoped_identifier
name: (identifier) @namespace)) name: (identifier) @namespace))
((scoped_identifier ((scoped_identifier
path: (identifier) @type) path: (identifier) @type)
(#lua-match? @type "^[A-Z]")) (#lua-match? @type "^[A-Z]"))
((scoped_identifier ((scoped_identifier
name: (identifier) @type) name: (identifier) @type)
(#lua-match? @type "^[A-Z]")) (#lua-match? @type "^[A-Z]"))
((scoped_identifier ((scoped_identifier
name: (identifier) @constant) name: (identifier) @constant)
(#lua-match? @constant "^[A-Z][A-Z%d_]*$")) (#lua-match? @constant "^[A-Z][A-Z%d_]*$"))
@ -105,14 +127,19 @@
(scoped_use_list (scoped_use_list
path: (identifier) @namespace) path: (identifier) @namespace)
(scoped_use_list (scoped_use_list
path: (scoped_identifier path: (scoped_identifier
(identifier) @namespace)) (identifier) @namespace))
(use_list (scoped_identifier (identifier) @namespace . (_))) (use_list (scoped_identifier (identifier) @namespace . (_)))
(use_list (identifier) @type (#lua-match? @type "^[A-Z]")) (use_list (identifier) @type (#lua-match? @type "^[A-Z]"))
(use_as_clause alias: (identifier) @type (#lua-match? @type "^[A-Z]")) (use_as_clause alias: (identifier) @type (#lua-match? @type "^[A-Z]"))
;; Correct enum constructors ; Correct enum constructors
(call_expression (call_expression
function: (scoped_identifier function: (scoped_identifier
"::" "::"
@ -120,9 +147,11 @@
(#lua-match? @constant "^[A-Z]")) (#lua-match? @constant "^[A-Z]"))
; Assume uppercase names in a match arm are constants. ; Assume uppercase names in a match arm are constants.
((match_arm ((match_arm
pattern: (match_pattern (identifier) @constant)) pattern: (match_pattern (identifier) @constant))
(#lua-match? @constant "^[A-Z]")) (#lua-match? @constant "^[A-Z]"))
((match_arm ((match_arm
pattern: (match_pattern pattern: (match_pattern
(scoped_identifier (scoped_identifier
@ -132,31 +161,35 @@
((identifier) @constant.builtin ((identifier) @constant.builtin
(#any-of? @constant.builtin "Some" "None" "Ok" "Err")) (#any-of? @constant.builtin "Some" "None" "Ok" "Err"))
;; Macro definitions ; Macro definitions
"$" @function.macro "$" @function.macro
(metavariable) @function.macro (metavariable) @function.macro
(macro_definition "macro_rules!" @function.macro) (macro_definition "macro_rules!" @function.macro)
;; Attribute macros ; Attribute macros
(attribute_item (attribute (identifier) @function.macro)) (attribute_item (attribute (identifier) @function.macro))
(attribute (scoped_identifier (identifier) @function.macro .)) (attribute (scoped_identifier (identifier) @function.macro .))
;; Derive macros (assume all arguments are types) ; Derive macros (assume all arguments are types)
; (attribute ; (attribute
; (identifier) @_name ; (identifier) @_name
; arguments: (attribute (attribute (identifier) @type)) ; arguments: (attribute (attribute (identifier) @type))
; (#eq? @_name "derive")) ; (#eq? @_name "derive"))
;; Function-like macros ; Function-like macros
(macro_invocation (macro_invocation
macro: (identifier) @function.macro) macro: (identifier) @function.macro)
(macro_invocation (macro_invocation
macro: (scoped_identifier macro: (scoped_identifier
(identifier) @function.macro .)) (identifier) @function.macro .))
; Literals
;;; Literals
[ [
(line_comment) (line_comment)
@ -165,34 +198,41 @@
((line_comment) @comment.documentation ((line_comment) @comment.documentation
(#lua-match? @comment.documentation "^///[^/]")) (#lua-match? @comment.documentation "^///[^/]"))
((line_comment) @comment.documentation ((line_comment) @comment.documentation
(#lua-match? @comment.documentation "^///$")) (#lua-match? @comment.documentation "^///$"))
((line_comment) @comment.documentation ((line_comment) @comment.documentation
(#lua-match? @comment.documentation "^//!")) (#lua-match? @comment.documentation "^//!"))
((block_comment) @comment.documentation ((block_comment) @comment.documentation
(#lua-match? @comment.documentation "^/[*][*][^*].*[*]/$")) (#lua-match? @comment.documentation "^/[*][*][^*].*[*]/$"))
((block_comment) @comment.documentation ((block_comment) @comment.documentation
(#lua-match? @comment.documentation "^/[*][!]")) (#lua-match? @comment.documentation "^/[*][!]"))
(boolean_literal) @boolean (boolean_literal) @boolean
(integer_literal) @number (integer_literal) @number
(float_literal) @float (float_literal) @float
[ [
(raw_string_literal) (raw_string_literal)
(string_literal) (string_literal)
] @string ] @string
(escape_sequence) @string.escape (escape_sequence) @string.escape
(char_literal) @character (char_literal) @character
; Keywords
;;; Keywords
[ [
"use" "use"
"mod" "mod"
] @include ] @include
(use_as_clause "as" @include) (use_as_clause "as" @include)
[ [
@ -237,11 +277,15 @@
] @keyword.return ] @keyword.return
(type_cast_expression "as" @keyword.operator) (type_cast_expression "as" @keyword.operator)
(qualified_type "as" @keyword.operator) (qualified_type "as" @keyword.operator)
(use_list (self) @namespace) (use_list (self) @namespace)
(scoped_use_list (self) @namespace) (scoped_use_list (self) @namespace)
(scoped_identifier [(crate) (super) (self)] @namespace) (scoped_identifier [(crate) (super) (self)] @namespace)
(visibility_modifier [(crate) (super) (self)] @namespace) (visibility_modifier [(crate) (super) (self)] @namespace)
[ [
@ -261,7 +305,7 @@
"for" @keyword "for" @keyword
(for_expression "for" @repeat) (for_expression "for" @repeat)
;;; Operators & Punctuation ; Operators
[ [
"!" "!"
@ -300,23 +344,41 @@
"||" "||"
] @operator ] @operator
["(" ")" "[" "]" "{" "}"] @punctuation.bracket ; Punctuation
(closure_parameters "|" @punctuation.bracket)
["(" ")" "[" "]" "{" "}"] @punctuation.bracket
(closure_parameters "|" @punctuation.bracket)
(type_arguments ["<" ">"] @punctuation.bracket) (type_arguments ["<" ">"] @punctuation.bracket)
(type_parameters ["<" ">"] @punctuation.bracket) (type_parameters ["<" ">"] @punctuation.bracket)
(bracketed_type ["<" ">"] @punctuation.bracket) (bracketed_type ["<" ">"] @punctuation.bracket)
(for_lifetimes ["<" ">"] @punctuation.bracket) (for_lifetimes ["<" ">"] @punctuation.bracket)
["," "." ":" "::" ";" "->" "=>"] @punctuation.delimiter ["," "." ":" "::" ";" "->" "=>"] @punctuation.delimiter
(attribute_item "#" @punctuation.special) (attribute_item "#" @punctuation.special)
(inner_attribute_item ["!" "#"] @punctuation.special) (inner_attribute_item ["!" "#"] @punctuation.special)
(macro_invocation "!" @function.macro) (macro_invocation "!" @function.macro)
(empty_type "!" @type.builtin) (empty_type "!" @type.builtin)
(macro_invocation macro: (identifier) @_ident @exception "!" @exception (macro_invocation
(#eq? @_ident "panic")) macro: (identifier) @exception
(macro_invocation macro: (identifier) @_ident @exception "!" @exception "!" @exception
(#contains? @_ident "assert")) (#eq? @exception "panic"))
(macro_invocation macro: (identifier) @_ident @debug "!" @debug
(#eq? @_ident "dbg")) (macro_invocation
macro: (identifier) @exception
"!" @exception
(#contains? @exception "assert"))
(macro_invocation
macro: (identifier) @debug
"!" @debug
(#eq? @debug "dbg"))