Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Fix Shift-click selection after cursor movement (#61045)
saberoueslati2118a7c53a84
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 516797a740d..8f5cda4b65d 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -856,6 +856,107 @@ fn test_extending_selection(cx: &mut TestAppContext) {});}+#[gpui::test]+fn test_extend_selection_after_collapsed_word_selection(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let editor = cx.add_window(|window, cx| {+ let buffer = MultiBuffer::build_simple("aaa bbb ccc ddd eee", cx);+ build_editor(buffer, window, cx)+ });++ _ = editor.update(cx, |editor, window, cx| {+ // Double-click "bbb" to select it word-wise.+ editor.begin_selection(DisplayPoint::new(DisplayRow(0), 5), false, 2, window, cx);+ editor.end_selection(window, cx);+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(0), 4)..DisplayPoint::new(DisplayRow(0), 7)]+ );++ // Move the cursor with the keyboard, collapsing the selection.+ editor.move_right(&MoveRight, window, cx);+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(0), 7)..DisplayPoint::new(DisplayRow(0), 7)]+ );++ // Shift+click further along the line should extend from the cursor,+ // not from the stale double-clicked word.+ editor.extend_selection(DisplayPoint::new(DisplayRow(0), 10), 1, window, cx);+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(0), 7)..DisplayPoint::new(DisplayRow(0), 10)]+ );+ });+}++#[gpui::test]+fn test_extend_selection_after_collapsed_word_selection_moved_via_offsets(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let editor = cx.add_window(|window, cx| {+ let buffer = MultiBuffer::build_simple("aaa bbb ccc ddd eee", cx);+ build_editor(buffer, window, cx)+ });++ _ = editor.update(cx, |editor, window, cx| {+ // Double-click "bbb" to select it word-wise.+ editor.begin_selection(DisplayPoint::new(DisplayRow(0), 5), false, 2, window, cx);+ editor.end_selection(window, cx);+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(0), 4)..DisplayPoint::new(DisplayRow(0), 7)]+ );++ // Collapse the selection through `move_offsets_with`, the code path+ // used by non-keyboard-movement callers like+ // `move_to_enclosing_bracket` and `select_delimiters_impl`.+ editor.change_selections(SelectionEffects::default(), window, cx, |s| {+ s.move_offsets_with(&mut |_, selection| {+ selection.collapse_to(MultiBufferOffset(7), SelectionGoal::None);+ });+ });+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(0), 7)..DisplayPoint::new(DisplayRow(0), 7)]+ );++ // Shift+click further along the line should extend from the cursor,+ // not from the stale double-clicked word.+ editor.extend_selection(DisplayPoint::new(DisplayRow(0), 10), 1, window, cx);+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(0), 7)..DisplayPoint::new(DisplayRow(0), 10)]+ );+ });+}++#[gpui::test]+fn test_extend_selection_from_empty_line_selection(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let editor = cx.add_window(|window, cx| {+ let buffer = MultiBuffer::build_simple("aaa\nbbb\n", cx);+ build_editor(buffer, window, cx)+ });++ _ = editor.update(cx, |editor, window, cx| {+ editor.begin_selection(DisplayPoint::new(DisplayRow(2), 0), false, 3, window, cx);+ editor.end_selection(window, cx);+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 0)]+ );++ editor.extend_selection(DisplayPoint::new(DisplayRow(0), 1), 1, window, cx);+ assert_eq!(+ display_ranges(editor, cx),+ [DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(0), 0)]+ );+ });+}+#[gpui::test]fn test_clone(cx: &mut TestAppContext) {init_test(cx, |_| {});diff --git a/crates/editor/src/selection.rs b/crates/editor/src/selection.rsindex 1d5ba4b5261..afb261305ec 100644--- a/crates/editor/src/selection.rs+++ b/crates/editor/src/selection.rs@@ -1442,14 +1442,20 @@ impl Editor {let selections = self.selections.all::<MultiBufferOffset>(&self.display_snapshot(cx));+ // `select` below resets the selections' granularity to `Character`, since it's the+ // funnel every wholesale selection replacement goes through. When extending, the+ // granularity established by the selection gesture that started the extension must+ // survive that reset instead of being overwritten by `pending_mode`.+ let select_mode = if self.selections.is_extending() {+ self.selections.select_mode().clone()+ } else {+ pending_mode+ };self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {s.select(selections);s.clear_pending();- if s.is_extending() {- s.set_is_extending(false);- } else {- s.set_select_mode(pending_mode);- }+ s.set_is_extending(false);+ s.set_select_mode(select_mode);});}}diff --git a/crates/editor/src/selections_collection.rs b/crates/editor/src/selections_collection.rsindex 33ffdfca815..53303db81fc 100644--- a/crates/editor/src/selections_collection.rs+++ b/crates/editor/src/selections_collection.rs@@ -843,6 +843,7 @@ impl<'snap, 'a> MutableSelectionsCollection<'snap, 'a> {.map(|selection| selection_to_anchor_selection(selection, self.snapshot)),);self.collection.pending = None;+ self.collection.select_mode = SelectMode::Character;self.selections_changed = true;}