on master help sign in
Improve C, Rust & Python indent queries & add @align captures.
36a59e4Daniel Ebert
runtime/queries/c/indents.scm+5−1
@@ -5,7 +5,6 @@
5 (enumerator_list)
5 (enumerator_list)
6 (parameter_list)
6 (parameter_list)
7 (init_declarator)
7 (init_declarator)
8− (case_statement)
9 (expression_statement)
8 (expression_statement)
10] @indent
9] @indent
11
10
@@ -13,6 +12,7 @@
13 "case"
12 "case"
14 "}"
13 "}"
15 "]"
14 "]"
15+ ")"
16] @outdent
16] @outdent
17
17
18(if_statement
18(if_statement
@@ -32,3 +32,7 @@
32 (_) @indent
32 (_) @indent
33 (#not-kind-eq? @indent "compound_statement")
33 (#not-kind-eq? @indent "compound_statement")
34 (#set! "scope" "all"))
34 (#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 @@
73(else_clause
73(else_clause
74 "else" @outdent)
74 "else" @outdent)
75
75
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 @@
14 (call_expression)
14 (call_expression)
15 (binary_expression)
15 (binary_expression)
16 (field_expression)
16 (field_expression)
17+ (await_expression)
17 (tuple_expression)
18 (tuple_expression)
18 (array_expression)
19 (array_expression)
19 (where_clause)
20 (where_clause)
21+ (type_cast_expression)
20
22
21 (token_tree)
23 (token_tree)
22 (macro_definition)
24 (macro_definition)
@@ -48,10 +50,16 @@
48 (#set! "scope" "all")
50 (#set! "scope" "all")
49)
51)
50(let_declaration
52(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
51 .
60 .
52 (_) @expr-start
61 (_) @expr-start
53 value: (_) @indent
62 value: (_) @indent
54− alternative: (_)? @indent
55 (#not-same-line? @indent @expr-start)
63 (#not-same-line? @indent @expr-start)
56 (#set! "scope" "all")
64 (#set! "scope" "all")
57)
65)
@@ -69,6 +77,22 @@
69 (#not-same-line? @indent @expr-start)
77 (#not-same-line? @indent @expr-start)
70 (#set! "scope" "all")
78 (#set! "scope" "all")
71)
79)
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+)
72
96
73; Some field expressions where the left part is a multiline expression are not
97; Some field expressions where the left part is a multiline expression are not
74; indented by cargo fmt.
98; indented by cargo fmt.
@@ -77,5 +101,48 @@
77(field_expression
101(field_expression
78 value: (_) @val
102 value: (_) @val
79 "." @outdent
103 "." @outdent
104+ ; Check whether the first line ends with `(`, `{` or `[` (up to whitespace).
80− (#match? @val "(\\A[^\\n\\r]+\\([\\t ]*(\\n|\\r).*)|(\\A[^\\n\\r]*\\{[\\t ]*(\\n|\\r))")
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")
81)
147)
148+