mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 11:06:54 -04:00
feat(python): Improved python indentation (#6859)
* feat: improve python indentation * use lua-match * revert concatenated_string changes, handle function def hanging indents * lint * add delimiters to param query * add test cases
This commit is contained in:
parent
a8c1f36d2c
commit
f16a775830
3 changed files with 81 additions and 4 deletions
|
|
@ -1,6 +1,5 @@
|
|||
[
|
||||
(import_from_statement)
|
||||
(parenthesized_expression)
|
||||
(generator_expression)
|
||||
(list_comprehension)
|
||||
(set_comprehension)
|
||||
|
|
@ -24,6 +23,10 @@
|
|||
(#set! indent.open_delimiter "{")
|
||||
(#set! indent.close_delimiter "}"))
|
||||
|
||||
((parenthesized_expression) @indent.align
|
||||
(#set! indent.open_delimiter "(")
|
||||
(#set! indent.close_delimiter ")"))
|
||||
|
||||
((for_statement) @indent.begin
|
||||
(#set! indent.immediate 1))
|
||||
|
||||
|
|
@ -67,14 +70,40 @@
|
|||
((case_clause) @indent.begin
|
||||
(#set! indent.immediate 1))
|
||||
|
||||
; if (cond1
|
||||
; or cond2
|
||||
; or cond3):
|
||||
; pass
|
||||
;
|
||||
(if_statement
|
||||
condition: (parenthesized_expression) @indent.align
|
||||
(#lua-match? @indent.align "^%([^\n]")
|
||||
(#set! indent.open_delimiter "(")
|
||||
(#set! indent.close_delimiter ")")
|
||||
(#set! indent.avoid_last_matching_next 1))
|
||||
|
||||
; while (
|
||||
; cond1
|
||||
; or cond2
|
||||
; or cond3):
|
||||
; pass
|
||||
;
|
||||
(while_statement
|
||||
condition: (parenthesized_expression) @indent.align
|
||||
(#lua-match? @indent.align "[^\n ]%)$")
|
||||
(#set! indent.open_delimiter "(")
|
||||
(#set! indent.close_delimiter ")")
|
||||
(#set! indent.avoid_last_matching_next 1))
|
||||
|
||||
; if (
|
||||
; cond1
|
||||
; or cond2
|
||||
; or cond3):
|
||||
; pass
|
||||
;
|
||||
(if_statement
|
||||
condition: (parenthesized_expression) @indent.align
|
||||
(#lua-match? @indent.align "[^\n ]%)$")
|
||||
(#set! indent.open_delimiter "(")
|
||||
(#set! indent.close_delimiter ")")
|
||||
(#set! indent.avoid_last_matching_next 1))
|
||||
|
|
@ -91,6 +120,11 @@
|
|||
(#set! indent.close_delimiter ")"))
|
||||
|
||||
((parameters) @indent.align
|
||||
(#set! indent.open_delimiter "(")
|
||||
(#set! indent.close_delimiter ")"))
|
||||
|
||||
((parameters) @indent.align
|
||||
(#lua-match? @indent.align "[^\n ]%)$")
|
||||
(#set! indent.open_delimiter "(")
|
||||
(#set! indent.close_delimiter ")")
|
||||
(#set! indent.avoid_last_matching_next 1))
|
||||
|
|
@ -130,9 +164,6 @@
|
|||
.
|
||||
(#lua-match? @indent.branch "^elif"))
|
||||
|
||||
(parenthesized_expression
|
||||
")" @indent.end)
|
||||
|
||||
(generator_expression
|
||||
")" @indent.end)
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,12 @@ def aligned_indent(arg1,
|
|||
arg2):
|
||||
pass
|
||||
|
||||
def aligned_indent2(
|
||||
arg1,
|
||||
arg2
|
||||
):
|
||||
pass
|
||||
|
||||
aligned_indent(1,
|
||||
2)
|
||||
|
||||
|
|
@ -10,5 +16,10 @@ aligned_indent(1,
|
|||
2
|
||||
)
|
||||
|
||||
aligned_indent(
|
||||
1,
|
||||
2
|
||||
)
|
||||
|
||||
foodsadsa(sdada,
|
||||
2
|
||||
|
|
|
|||
35
tests/indent/python/parenthesized_conditions.py
Normal file
35
tests/indent/python/parenthesized_conditions.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
if (
|
||||
True
|
||||
or 1
|
||||
or False
|
||||
):
|
||||
pass
|
||||
|
||||
if (
|
||||
True
|
||||
or 1
|
||||
or False):
|
||||
pass
|
||||
|
||||
if (True
|
||||
or 1
|
||||
or False):
|
||||
pass
|
||||
|
||||
while (
|
||||
False
|
||||
or 1
|
||||
or False
|
||||
):
|
||||
pass
|
||||
|
||||
while (
|
||||
False
|
||||
or 1
|
||||
or False):
|
||||
pass
|
||||
|
||||
while (False
|
||||
or 1
|
||||
or False):
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue