refactor(indent)!: Rework indent, aligned indent

indents now use @indent.X style captures, and indent.PROP for properties to set on those captures, as documented in the help.

Captures are:

indent.auto
indent.begin
indent.end
indent.dedent
indent.branch
indent.ignore
indent.align
indent.zero

Properties are:

indent.immediate
indent.start_at_same_line
indent.open_delimiter
indent.close_delimiter
indent.increment
indent.avoid_last_matching_next

Multiple opening delims on one line and multiple closing on a line are collapsed so as not to over indent,

The final line of @indent.align blocks which must in some cases be treated specially to avoid clashing with the next line is treated the same regardless of whether the @indent.align capture actually uses aligned indentation or just normal indentation. The indent.avoid_last_matching_next property controls this.

Adjust python to use these.

List, set, dict and tuple all use @indent.align which permits both hanging and aligned styles.

Finally, try: on it’s own will indent when typing live but make no guaranteeds about whole-file formatting.

Includes lucario387:fix-align-indent
This commit is contained in:
George Harker 2023-03-19 18:09:18 -07:00 committed by Amaan Qureshi
parent 90ead4ed58
commit d1333dd7e5
14 changed files with 298 additions and 53 deletions

View file

@ -222,7 +222,112 @@ Supported options:
enable = true
},
}
`@indent` *nvim-treesitter-indentation-queries*
Queries can use the following captures: `@indent` and `@dedent`,
`@branch`, `@indent_end` or `@aligned_indent`. An `@ignore` capture tells
treesitter to ignore indentation and a `@zero_indent` capture sets
the indentation to 0.
`@indent` *nvim-treesitter-indentation-indent*
The `@indent` specifies that the next line should be indented. Multiple
indents on the same line get collapsed. Eg.
>
(
(if_statement)
(ERROR "else") @indent
)
<
Indent can also have `immediate_indent` set using a `#set!` directive, which
permits the next line to indent even when the block intended to be indented
has no content yet, improving interactive typing.
eg for python:
>
((if_statement) @indent
(#set! "immediate_indent" 1))
<
Will allow:
>
if True:<CR>
# Auto indent to here
`@indent_end` *nvim-treesitter-indentation-indent_end*
An `@indent_end` capture is used to specify that the indented region ends and
any text subsequent to the capture should be dedented.
`@branch` *nvim-treesitter-indentation-branch*
An `@branch` capture is used to specify that a dedented region starts
at the line including the captured nodes.
`@dedent` *nvim-treesitter-indentation-dedent*
A `@dedent` capture specifies dedenting starting on the next line.
>
`@aligned_indent` *nvim-treesitter-indentation-aligned_indent*
Aligned indent blocks may be specified with the `@aligned_indent` capture.
This permits
>
foo(a,
b,
c)
<
As well as
>
foo(
a,
b,
c)
<
and finally
>
foo(
a,
b,
c
)
<
To specify the delimiters to use `open_delimiter` and `close_delimiter`
should be used. Eg.
>
((argument_list) @aligned_indent
(#set! "open_delimiter" "(")
(#set! "close_delimiter" ")"))
<
For some languages the last line of an `aligned_indent` block must not be
the same indent as the natural next line.
For example in python:
>
if (a > b and
c < d):
pass
Is not correct, whereas
>
if (a > b and
c < d):
pass
Would be correctly indented. This behavior may be chosen using
`avoid_last_matching_next`. Eg.
>
(if_statement
condition: (parenthesized_expression) @aligned_indent
(#set! "open_delimiter" "(")
(#set! "close_delimiter" ")")
(#set! "avoid_last_matching_next" 1)
)
<
Could be used to specify that the last line of an `@aligned_indent` capture
should be additionally indented to avoid clashing with the indent of the first
line of the block inside an if.
==============================================================================
COMMANDS *nvim-treesitter-commands*