language: Fix indent suggestions for significant indented languages like Python (#29625)

Closes #26157

This fixes multiple cases where Python indentation breaks:
- [x] Adding a new line after `if`, `try`, etc. correctly indents in
that scope
- [x] Multi-cursor tabs correctly preserve relative indents
- [x] Adding a new line after `else`, `finally`, etc. correctly outdents
them
- [x] Existing Tests

Future Todo: I need to add new tests for all the above cases.

Before/After:

1. Multi-cursor tabs correctly preserve relative indents


https://github.com/user-attachments/assets/08a46ddf-5371-4e26-ae7d-f8aa0b31c4a2

2. Adding a new line after `if`, `try`, etc. correctly indents in that
scope


https://github.com/user-attachments/assets/9affae97-1a50-43c9-9e9f-c1ea3a747813

Release Notes:

- Fixes indentation-related issues involving tab, newline, etc for
Python.
This commit is contained in:
Smit Barmase 2025-05-07 23:05:42 +05:30 committed by GitHub
parent 22ad207baf
commit 7c76cee16d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 84 additions and 34 deletions

View file

@ -1209,7 +1209,7 @@ mod tests {
append(&mut buffer, "foo(\n1)", cx);
assert_eq!(
buffer.text(),
"def a():\n \n if a:\n b()\n else:\n foo(\n 1)"
"def a():\n \n if a:\n b()\n else:\n foo(\n 1)"
);
// dedent the closing paren if it is shifted to the beginning of the line
@ -1255,7 +1255,7 @@ mod tests {
// dedent "else" on the line after a closing paren
append(&mut buffer, "\n else:\n", cx);
assert_eq!(buffer.text(), "if a:\n b(\n )\nelse:\n ");
assert_eq!(buffer.text(), "if a:\n b(\n )\nelse:\n");
buffer
});

View file

@ -27,6 +27,7 @@ brackets = [
]
auto_indent_using_last_non_empty_line = false
increase_indent_pattern = "^[^#].*:\\s*$"
decrease_indent_pattern = "^\\s*(else|elif|except|finally)\\b.*:"
debuggers = ["Debugpy"]
significant_indentation = true
increase_indent_pattern = "^\\s*(try)\\b.*:"
decrease_indent_pattern = "^\\s*(else|elif|except|finally)\\b.*:"

View file

@ -1,18 +1,43 @@
(_ "[" "]" @end) @indent
(_ "{" "}" @end) @indent
(_ "(" ")" @end) @indent
(try_statement
body: (_) @start
[(except_clause) (finally_clause)] @end
) @indent
(function_definition
":" @start
body: (block) @indent
)
(if_statement
consequence: (_) @start
alternative: (_) @end
) @indent
":" @start
consequence: (block) @indent
alternative: (_)? @outdent
)
(_
alternative: (elif_clause) @start
alternative: (_) @end
) @indent
(else_clause
":" @start
body: (block) @indent
)
(elif_clause
":" @start
consequence: (block) @indent
)
(for_statement
":" @start
body: (block) @indent
)
(try_statement
":" @start
body: (block) @indent
(except_clause)? @outdent
(else_clause)? @outdent
(finally_clause)? @outdent
)
(except_clause
":" @start
(block) @indent
)
(finally_clause
":" @start
(block) @indent
)