From f16a77583047d6d3ea684092c679bbe88398cb01 Mon Sep 17 00:00:00 2001 From: Grace Petryk Date: Tue, 16 Jul 2024 04:13:41 -0700 Subject: 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 --- queries/python/indents.scm | 39 ++++++++++++++++++++++--- tests/indent/python/aligned_indent.py | 11 +++++++ tests/indent/python/parenthesized_conditions.py | 35 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 tests/indent/python/parenthesized_conditions.py diff --git a/queries/python/indents.scm b/queries/python/indents.scm index dbda25b5c..2414812a7 100644 --- a/queries/python/indents.scm +++ b/queries/python/indents.scm @@ -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) diff --git a/tests/indent/python/aligned_indent.py b/tests/indent/python/aligned_indent.py index b92356740..bdba8467a 100644 --- a/tests/indent/python/aligned_indent.py +++ b/tests/indent/python/aligned_indent.py @@ -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 diff --git a/tests/indent/python/parenthesized_conditions.py b/tests/indent/python/parenthesized_conditions.py new file mode 100644 index 000000000..d30f2b1af --- /dev/null +++ b/tests/indent/python/parenthesized_conditions.py @@ -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 -- cgit v1.2.3-70-g09d2