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.

Fix panic when doing various cursor movements with a pending mouse selection (#13016)

Max Brunsfeld066cdc229784
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 4771c8bc017..a767f9eabb1 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -481,6 +481,42 @@ fn test_canceling_pending_selection(cx: &mut TestAppContext) {
});
}
+#[gpui::test]
+fn test_movement_actions_with_pending_selection(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let view = cx.add_window(|cx| {
+ let buffer = MultiBuffer::build_simple("aaaaaa\nbbbbbb\ncccccc\ndddddd\n", cx);
+ build_editor(buffer, cx)
+ });
+
+ _ = view.update(cx, |view, cx| {
+ view.begin_selection(DisplayPoint::new(DisplayRow(2), 2), false, 1, cx);
+ assert_eq!(
+ view.selections.display_ranges(cx),
+ [DisplayPoint::new(DisplayRow(2), 2)..DisplayPoint::new(DisplayRow(2), 2)]
+ );
+
+ view.move_down(&Default::default(), cx);
+ assert_eq!(
+ view.selections.display_ranges(cx),
+ [DisplayPoint::new(DisplayRow(3), 2)..DisplayPoint::new(DisplayRow(3), 2)]
+ );
+
+ view.begin_selection(DisplayPoint::new(DisplayRow(2), 2), false, 1, cx);
+ assert_eq!(
+ view.selections.display_ranges(cx),
+ [DisplayPoint::new(DisplayRow(2), 2)..DisplayPoint::new(DisplayRow(2), 2)]
+ );
+
+ view.move_up(&Default::default(), cx);
+ assert_eq!(
+ view.selections.display_ranges(cx),
+ [DisplayPoint::new(DisplayRow(1), 2)..DisplayPoint::new(DisplayRow(1), 2)]
+ );
+ });
+}
+
#[gpui::test]
fn test_clone(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/editor/src/selections_collection.rs b/crates/editor/src/selections_collection.rs
index 70af8b94893..7a448f0b6cc 100644
--- a/crates/editor/src/selections_collection.rs
+++ b/crates/editor/src/selections_collection.rs
@@ -256,7 +256,10 @@ impl SelectionsCollection {
}
pub fn first_anchor(&self) -> Selection<Anchor> {
- self.disjoint[0].clone()
+ self.pending
+ .as_ref()
+ .map(|pending| pending.selection.clone())
+ .unwrap_or_else(|| self.disjoint.first().cloned().unwrap())
}
pub fn first<D: TextDimension + Ord + Sub<D, Output = D>>(