Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Expand empty selections to cover full word and fix bugs

Joseph T. Lyons1abb6a0176fe
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index c70a1a8e048..cd5e86b9109 100644
--- a/crates/editor/src/editor.rs
+++ b/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));
}
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 11d64f085cd..7e39647ac63 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/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]