diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 237c68491a..809a286fb8 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -2918,6 +2918,17 @@ impl Editor { .next() .map_or(true, |c| scope.should_autoclose_before(c)); + let preceding_text_allows_autoclose = selection.start.column == 0 + || snapshot.reversed_chars_at(selection.start).next().map_or( + true, + |c| { + bracket_pair.start != bracket_pair.end + || !snapshot + .char_classifier_at(selection.start) + .is_word(c) + }, + ); + let is_closing_quote = if bracket_pair.end == bracket_pair.start && bracket_pair.start.len() == 1 { @@ -2935,6 +2946,7 @@ impl Editor { if autoclose && bracket_pair.close && following_text_allows_autoclose + && preceding_text_allows_autoclose && !is_closing_quote { let anchor = snapshot.anchor_before(selection.end); diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index c2cad6528d..a37e0efc3b 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -6357,12 +6357,29 @@ async fn test_autoclose_and_auto_surround_pairs(cx: &mut TestAppContext) { cx.update_editor(|editor, window, cx| editor.handle_input("{", window, cx)); cx.assert_editor_state("{«aˇ»} b"); - // Autclose pair where the start and end characters are the same + // Autoclose when not immediately after a word character + cx.set_state("a ˇ"); + cx.update_editor(|editor, window, cx| editor.handle_input("\"", window, cx)); + cx.assert_editor_state("a \"ˇ\""); + + // Autoclose pair where the start and end characters are the same + cx.update_editor(|editor, window, cx| editor.handle_input("\"", window, cx)); + cx.assert_editor_state("a \"\"ˇ"); + + // Don't autoclose when immediately after a word character cx.set_state("aˇ"); cx.update_editor(|editor, window, cx| editor.handle_input("\"", window, cx)); - cx.assert_editor_state("a\"ˇ\""); + cx.assert_editor_state("a\"ˇ"); + + // Do autoclose when after a non-word character + cx.set_state("{ˇ"); cx.update_editor(|editor, window, cx| editor.handle_input("\"", window, cx)); - cx.assert_editor_state("a\"\"ˇ"); + cx.assert_editor_state("{\"ˇ\""); + + // Non identical pairs autoclose regardless of preceding character + cx.set_state("aˇ"); + cx.update_editor(|editor, window, cx| editor.handle_input("{", window, cx)); + cx.assert_editor_state("a{ˇ}"); // Don't autoclose pair if autoclose is disabled cx.set_state("ˇ");