From 36a59e4482120e66980b7f8f260a174b82b9415d Mon Sep 17 00:00:00 2001 From: Daniel Ebert Date: Fri, 11 Aug 2023 12:24:37 +0200 Subject: [PATCH] Improve C, Rust & Python indent queries & add @align captures. --- runtime/queries/c/indents.scm | 6 ++- runtime/queries/python/indents.scm | 9 ++++ runtime/queries/rust/indents.scm | 71 +++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/runtime/queries/c/indents.scm b/runtime/queries/c/indents.scm index 812867854..058d80316 100644 --- a/runtime/queries/c/indents.scm +++ b/runtime/queries/c/indents.scm @@ -5,7 +5,6 @@ (enumerator_list) (parameter_list) (init_declarator) - (case_statement) (expression_statement) ] @indent @@ -13,6 +12,7 @@ "case" "}" "]" + ")" ] @outdent (if_statement @@ -32,3 +32,7 @@ (_) @indent (#not-kind-eq? @indent "compound_statement") (#set! "scope" "all")) + +(parameter_list + . (parameter_declaration) @anchor + (#set! "scope" "tail")) @align diff --git a/runtime/queries/python/indents.scm b/runtime/queries/python/indents.scm index 835b40d39..67bc76ad5 100644 --- a/runtime/queries/python/indents.scm +++ b/runtime/queries/python/indents.scm @@ -73,3 +73,12 @@ (else_clause "else" @outdent) +(parameters + . + (identifier) @anchor + (#set! "scope" "tail")) @align +(argument_list + . + (_) @anchor + (#set! "scope" "tail")) @align + diff --git a/runtime/queries/rust/indents.scm b/runtime/queries/rust/indents.scm index 5c2c70d0c..af2e05e3b 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") ) +