on master help sign in
Improve C, Rust & Python indent queries & add @align captures.
commit36a59e4482120e66980b7f8f260a174b82b9415d authorDaniel Ebert <daniel.ebert@protonmail.com> dateFri 11 Aug 2023, 12:24 (3 years ago) parenteab0d4f
3 files changed+83−3
runtime/queries/c/indents.scm+5−1 runtime/queries/python/indents.scm+9 runtime/queries/rust/indents.scm+69−2
runtime/queries/c/indents.scm+5−1
@@ -5,7 +5,6 @@
55 (enumerator_list)
66 (parameter_list)
77 (init_declarator)
8− (case_statement)
98 (expression_statement)
109] @indent
1110
@@ -13,6 +12,7 @@
1312 "case"
1413 "}"
1514 "]"
15+ ")"
1616] @outdent
1717
1818(if_statement
@@ -32,3 +32,7 @@
3232 (_) @indent
3333 (#not-kind-eq? @indent "compound_statement")
3434 (#set! "scope" "all"))
35+
36+(parameter_list
37+ . (parameter_declaration) @anchor
38+ (#set! "scope" "tail")) @align
runtime/queries/python/indents.scm+9
@@ -73,3 +73,12 @@
7373(else_clause
7474 "else" @outdent)
7575
76+(parameters
77+ .
78+ (identifier) @anchor
79+ (#set! "scope" "tail")) @align
80+(argument_list
81+ .
82+ (_) @anchor
83+ (#set! "scope" "tail")) @align
84+
runtime/queries/rust/indents.scm+69−2
@@ -14,9 +14,11 @@
1414 (call_expression)
1515 (binary_expression)
1616 (field_expression)
17+ (await_expression)
1718 (tuple_expression)
1819 (array_expression)
1920 (where_clause)
21+ (type_cast_expression)
2022
2123 (token_tree)
2224 (macro_definition)
@@ -48,10 +50,16 @@
4850 (#set! "scope" "all")
4951)
5052(let_declaration
53+ "let" @expr-start
54+ value: (_) @indent
55+ alternative: (_)? @indent
56+ (#not-same-line? @indent @expr-start)
57+ (#set! "scope" "all")
58+)
59+(let_condition
5160 .
5261 (_) @expr-start
5362 value: (_) @indent
54− alternative: (_)? @indent
5563 (#not-same-line? @indent @expr-start)
5664 (#set! "scope" "all")
5765)
@@ -69,6 +77,22 @@
6977 (#not-same-line? @indent @expr-start)
7078 (#set! "scope" "all")
7179)
80+(field_pattern
81+ .
82+ (_) @expr-start
83+ pattern: (_) @indent
84+ (#not-same-line? @indent @expr-start)
85+ (#set! "scope" "all")
86+)
87+; Indent type aliases that span multiple lines, similar to
88+; regular assignment expressions
89+(type_item
90+ .
91+ (_) @expr-start
92+ type: (_) @indent
93+ (#not-same-line? @indent @expr-start)
94+ (#set! "scope" "all")
95+)
7296
7397; Some field expressions where the left part is a multiline expression are not
7498; indented by cargo fmt.
@@ -77,5 +101,48 @@
77101(field_expression
78102 value: (_) @val
79103 "." @outdent
80− (#match? @val "(\\A[^\\n\\r]+\\([\\t ]*(\\n|\\r).*)|(\\A[^\\n\\r]*\\{[\\t ]*(\\n|\\r))")
104+ ; Check whether the first line ends with `(`, `{` or `[` (up to whitespace).
105+ (#match? @val "(\\A[^\\n\\r]+(\\(|\\{|\\[)[\\t ]*(\\n|\\r))")
106+)
107+; Same as above, but with an additional `call_expression`. This is required since otherwise
108+; the arguments of the function call won't be outdented.
109+(call_expression
110+ function: (field_expression
111+ value: (_) @val
112+ "." @outdent
113+ (#match? @val "(\\A[^\\n\\r]+(\\(|\\{|\\[)[\\t ]*(\\n|\\r))")
114+ )
115+ arguments: (_) @outdent
116+)
117+
118+
119+; Indent if guards in patterns.
120+; Since the tree-sitter grammar doesn't create a node for the if expression,
121+; it's not possible to do this correctly in all cases. Indenting the tail of the
122+; whole pattern whenever it contains an `if` only fails if the `if` appears after
123+; the second line of the pattern (which should only rarely be the case)
124+(match_pattern
125+ .
126+ (_) @expr-start
127+ "if" @pattern-guard
128+ (#not-same-line? @expr-start @pattern-guard)
129+) @indent
130+
131+; Align closure parameters if they span more than one line
132+(closure_parameters
133+ "|"
134+ .
135+ (_) @anchor
136+ (_) @expr-end
137+ .
138+ (#not-same-line? @anchor @expr-end)
139+) @align
140+
141+(for_expression
142+ "in" @in
143+ .
144+ (_) @indent
145+ (#not-same-line? @in @indent)
146+ (#set! "scope" "all")
81147)
148+