Skip to content

Commit 4c28d2c

Browse files
authored
language: Improve auto-indentation when using round brackets in Python (#31260)
Follow-up to #29625 and #30902 This PR reintroduces auto-intents for brackets in Python and fixes some cases where an indentation would be triggered if it should not. For example, upon typing ```python a = [] ``` and inserting a newline after, the next line would be indented although it shoud not be. Bracket auto-indentation was tested prior to #29625 but removed there and the test updated accordingly. #30902 reintroduced this for all brackets but `()`. I reintroduced this here, reverted the changes to the test so that indents also happen after typing `()`. This is frequently used for tuples and multiline statements in Python. Release Notes: - Improved auto-indentation when using round brackets in Python.
1 parent a204510 commit 4c28d2c

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

crates/editor/src/editor_tests.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20860,7 +20860,7 @@ async fn test_indent_on_newline_for_python(cx: &mut TestAppContext) {
2086020860
ˇ
2086120861
"});
2086220862

20863-
// test correct indent after newline in curly brackets
20863+
// test correct indent after newline in brackets
2086420864
cx.set_state(indoc! {"
2086520865
{ˇ}
2086620866
"});
@@ -20873,6 +20873,32 @@ async fn test_indent_on_newline_for_python(cx: &mut TestAppContext) {
2087320873
ˇ
2087420874
}
2087520875
"});
20876+
20877+
cx.set_state(indoc! {"
20878+
(ˇ)
20879+
"});
20880+
cx.update_editor(|editor, window, cx| {
20881+
editor.newline(&Newline, window, cx);
20882+
});
20883+
cx.run_until_parked();
20884+
cx.assert_editor_state(indoc! {"
20885+
(
20886+
ˇ
20887+
)
20888+
"});
20889+
20890+
// do not indent after empty lists or dictionaries
20891+
cx.set_state(indoc! {"
20892+
a = []ˇ
20893+
"});
20894+
cx.update_editor(|editor, window, cx| {
20895+
editor.newline(&Newline, window, cx);
20896+
});
20897+
cx.run_until_parked();
20898+
cx.assert_editor_state(indoc! {"
20899+
a = []
20900+
ˇ
20901+
"});
2087620902
}
2087720903

2087820904
fn empty_range(row: usize, column: usize) -> Range<DisplayPoint> {

crates/language/src/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2901,7 +2901,7 @@ impl BufferSnapshot {
29012901
end
29022902
};
29032903
if let Some((start, end)) = start.zip(end) {
2904-
if start.row == end.row && !significant_indentation {
2904+
if start.row == end.row && (!significant_indentation || start.column < end.column) {
29052905
continue;
29062906
}
29072907
let range = start..end;

crates/languages/src/python.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,12 +1236,12 @@ mod tests {
12361236
"def a():\n \n if a:\n b()\n else:\n "
12371237
);
12381238

1239-
// indent after an open paren. the closing paren is not indented
1239+
// indent after an open paren. the closing paren is not indented
12401240
// because there is another token before it on the same line.
12411241
append(&mut buffer, "foo(\n1)", cx);
12421242
assert_eq!(
12431243
buffer.text(),
1244-
"def a():\n \n if a:\n b()\n else:\n foo(\n 1)"
1244+
"def a():\n \n if a:\n b()\n else:\n foo(\n 1)"
12451245
);
12461246

12471247
// dedent the closing paren if it is shifted to the beginning of the line

crates/languages/src/python/indents.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
(_ "(" ")" @end) @indent
12
(_ "[" "]" @end) @indent
23
(_ "{" "}" @end) @indent
34

0 commit comments

Comments
 (0)