@@ -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);
@@ -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("ห");