aboutsummaryrefslogtreecommitdiff
path: root/runtime/queries/rust/indents.scm
diff options
context:
space:
mode:
authorDaniel Ebert2023-08-11 10:24:37 +0000
committerBlaž Hrastnik2023-08-11 14:44:02 +0000
commit36a59e4482120e66980b7f8f260a174b82b9415d (patch)
treedfc184a5c86d8cefdbc7e4313320c3230331db3f /runtime/queries/rust/indents.scm
parenteab0d4fa4b5c3a98d94b90acf54fd22692148ce3 (diff)
Improve C, Rust & Python indent queries & add @align captures.
Diffstat (limited to 'runtime/queries/rust/indents.scm')
-rw-r--r--runtime/queries/rust/indents.scm71
1 files changed, 69 insertions, 2 deletions
diff --git a/runtime/queries/rust/indents.scm b/runtime/queries/rust/indents.scm
index 5c2c70d0..af2e05e3 100644
--- a/runtime/queries/rust/indents.scm
+++ b/runtime/queries/rust/indents.scm
@@ -14,9 +14,11 @@
(call_expression)
(binary_expression)
(field_expression)
+ (await_expression)
(tuple_expression)
(array_expression)
(where_clause)
+ (type_cast_expression)
(token_tree)
(macro_definition)
@@ -48,10 +50,16 @@
(#set! "scope" "all")
)
(let_declaration
+ "let" @expr-start
+ value: (_) @indent
+ alternative: (_)? @indent
+ (#not-same-line? @indent @expr-start)
+ (#set! "scope" "all")
+)
+(let_condition
.
(_) @expr-start
value: (_) @indent
- alternative: (_)? @indent
(#not-same-line? @indent @expr-start)
(#set! "scope" "all")
)
@@ -69,6 +77,22 @@
(#not-same-line? @indent @expr-start)
(#set! "scope" "all")
)
+(field_pattern
+ .
+ (_) @expr-start
+ pattern: (_) @indent
+ (#not-same-line? @indent @expr-start)
+ (#set! "scope" "all")
+)
+; Indent type aliases that span multiple lines, similar to
+; regular assignment expressions
+(type_item
+ .
+ (_) @expr-start
+ type: (_) @indent
+ (#not-same-line? @indent @expr-start)
+ (#set! "scope" "all")
+)
; Some field expressions where the left part is a multiline expression are not
; indented by cargo fmt.
@@ -77,5 +101,48 @@
(field_expression
value: (_) @val
"." @outdent
- (#match? @val "(\\A[^\\n\\r]+\\([\\t ]*(\\n|\\r).*)|(\\A[^\\n\\r]*\\{[\\t ]*(\\n|\\r))")
+ ; Check whether the first line ends with `(`, `{` or `[` (up to whitespace).
+ (#match? @val "(\\A[^\\n\\r]+(\\(|\\{|\\[)[\\t ]*(\\n|\\r))")
+)
+; Same as above, but with an additional `call_expression`. This is required since otherwise
+; the arguments of the function call won't be outdented.
+(call_expression
+ function: (field_expression
+ value: (_) @val
+ "." @outdent
+ (#match? @val "(\\A[^\\n\\r]+(\\(|\\{|\\[)[\\t ]*(\\n|\\r))")
+ )
+ arguments: (_) @outdent
+)
+
+
+; Indent if guards in patterns.
+; Since the tree-sitter grammar doesn't create a node for the if expression,
+; it's not possible to do this correctly in all cases. Indenting the tail of the
+; whole pattern whenever it contains an `if` only fails if the `if` appears after
+; the second line of the pattern (which should only rarely be the case)
+(match_pattern
+ .
+ (_) @expr-start
+ "if" @pattern-guard
+ (#not-same-line? @expr-start @pattern-guard)
+) @indent
+
+; Align closure parameters if they span more than one line
+(closure_parameters
+ "|"
+ .
+ (_) @anchor
+ (_) @expr-end
+ .
+ (#not-same-line? @anchor @expr-end)
+) @align
+
+(for_expression
+ "in" @in
+ .
+ (_) @indent
+ (#not-same-line? @in @indent)
+ (#set! "scope" "all")
)
+