Expand empty selections to cover full word and fix bugs

Joseph T. Lyons created

Change summary

crates/editor/src/editor.rs       | 21 +++++++++++----------
crates/editor/src/editor_tests.rs | 23 +++++++++++++++++++++--
2 files changed, 32 insertions(+), 12 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -4366,6 +4366,8 @@ impl Editor {
 
         let mut new_selections = Vec::new();
         let mut edits = Vec::new();
+        let mut selection_adjustment = 0i32;
+
         for selection in self.selections.all::<usize>(cx) {
             let selection_is_empty = selection.is_empty();
 
@@ -4382,18 +4384,17 @@ impl Editor {
             };
 
             let text = buffer.text_for_range(start..end).collect::<String>();
+            let old_length = text.len() as i32;
             let text = callback(&text);
 
-            if selection_is_empty {
-                new_selections.push(selection);
-            } else {
-                new_selections.push(Selection {
-                    start,
-                    end: start + text.len(),
-                    goal: SelectionGoal::None,
-                    ..selection
-                });
-            }
+            new_selections.push(Selection {
+                start: (start as i32 - selection_adjustment) as usize,
+                end: ((start + text.len()) as i32 - selection_adjustment) as usize,
+                goal: SelectionGoal::None,
+                ..selection
+            });
+
+            selection_adjustment += old_length - text.len() as i32;
 
             edits.push((start..end, text));
         }

crates/editor/src/editor_tests.rs 🔗

@@ -2719,7 +2719,7 @@ async fn test_manipulate_text(cx: &mut TestAppContext) {
         «hello worldˇ»
     "});
 
-    // From here on out, test more complex cases of manipulate_text() with a single driver method: convert_to_upper_case()
+    // From here on out, test more complex cases of manipulate_text()
 
     // Test no selection case - should affect words cursors are in
     // Cursor at beginning, middle, and end of word
@@ -2728,7 +2728,7 @@ async fn test_manipulate_text(cx: &mut TestAppContext) {
     "});
     cx.update_editor(|e, cx| e.convert_to_upper_case(&ConvertToUpperCase, cx));
     cx.assert_editor_state(indoc! {"
-        ˇHELLO big BEAUˇTIFUL WORLDˇ
+        «HELLOˇ» big «BEAUTIFULˇ» «WORLDˇ»
     "});
 
     // Test multiple selections on a single line and across multiple lines
@@ -2752,6 +2752,25 @@ async fn test_manipulate_text(cx: &mut TestAppContext) {
     cx.assert_editor_state(indoc! {"
         «TSCHÜSSˇ»
     "});
+
+    // Test to make sure we don't crash when text shrinks
+    cx.set_state(indoc! {"
+        aaa_bbbˇ
+    "});
+    cx.update_editor(|e, cx| e.convert_to_lower_camel_case(&ConvertToLowerCamelCase, cx));
+    cx.assert_editor_state(indoc! {"
+        «aaaBbbˇ»
+    "});
+
+    // Test to make sure we all aware of the fact that each word can grow and shrink
+    // Final selections should be aware of this fact
+    cx.set_state(indoc! {"
+        aaa_bˇbb bbˇb_ccc ˇccc_ddd
+    "});
+    cx.update_editor(|e, cx| e.convert_to_lower_camel_case(&ConvertToLowerCamelCase, cx));
+    cx.assert_editor_state(indoc! {"
+        «aaaBbbˇ» «bbbCccˇ» «cccDddˇ»
+    "});
 }
 
 #[gpui::test]