diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index b5442122fcfedbe9bc369f830852067fdb7c6ae1..72f8641d15667c89c7848520b3800a6a40cfaf65 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -20741,7 +20741,7 @@ async fn test_indent_on_newline_for_python(cx: &mut TestAppContext) { ˇ "}); - // test correct indent after newline in curly brackets + // test correct indent after newline in brackets cx.set_state(indoc! {" {ˇ} "}); @@ -20754,6 +20754,32 @@ async fn test_indent_on_newline_for_python(cx: &mut TestAppContext) { ˇ } "}); + + cx.set_state(indoc! {" + (ˇ) + "}); + cx.update_editor(|editor, window, cx| { + editor.newline(&Newline, window, cx); + }); + cx.run_until_parked(); + cx.assert_editor_state(indoc! {" + ( + ˇ + ) + "}); + + // do not indent after empty lists or dictionaries + cx.set_state(indoc! {" + a = []ˇ + "}); + cx.update_editor(|editor, window, cx| { + editor.newline(&Newline, window, cx); + }); + cx.run_until_parked(); + cx.assert_editor_state(indoc! {" + a = [] + ˇ + "}); } fn empty_range(row: usize, column: usize) -> Range { diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index b8799e79328fdfee51882c90bf06591b2e8bead4..2d298ff24bd5a28201e6d2b83855ca74d1c6f039 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -2907,7 +2907,7 @@ impl BufferSnapshot { end }; if let Some((start, end)) = start.zip(end) { - if start.row == end.row && !significant_indentation { + if start.row == end.row && (!significant_indentation || start.column < end.column) { continue; } let range = start..end; diff --git a/crates/languages/src/python.rs b/crates/languages/src/python.rs index da7ab9cb6ee26abe384482dc4985db9016ccdd4f..e68c43d805edfe33dcfb051df1cc7cb3925476a9 100644 --- a/crates/languages/src/python.rs +++ b/crates/languages/src/python.rs @@ -1236,12 +1236,12 @@ mod tests { "def a():\n \n if a:\n b()\n else:\n " ); - // indent after an open paren. the closing paren is not indented + // indent after an open paren. the closing paren is not indented // because there is another token before it on the same line. 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 diff --git a/crates/languages/src/python/indents.scm b/crates/languages/src/python/indents.scm index 34557f3b2a6079f62633c3fb0b1cd52d2e0a0235..f306d814350091994af4ab322432045a4adf35c7 100644 --- a/crates/languages/src/python/indents.scm +++ b/crates/languages/src/python/indents.scm @@ -1,3 +1,4 @@ +(_ "(" ")" @end) @indent (_ "[" "]" @end) @indent (_ "{" "}" @end) @indent