Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Fix line selection disappears when the cursor goes off screen (#60054)
Finn Eitreimbc61a30181aa
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 71e9a50087a..6def466e844 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -8353,7 +8353,7 @@ impl Element for EditorElement {selected_buffer_ids};- let mut selections = editor.selections.disjoint_in_range(+ let mut selections = editor.selections.disjoint_in_row_range(start_anchor..end_anchor,&snapshot.display_snapshot,);diff --git a/crates/editor/src/selections_collection.rs b/crates/editor/src/selections_collection.rsindex 53303db81fc..0e2a607a445 100644--- a/crates/editor/src/selections_collection.rs+++ b/crates/editor/src/selections_collection.rs@@ -7,7 +7,7 @@ use std::{use gpui::Pixels;use itertools::{Either, Itertools as _};use language::{Bias, Point, PointUtf16, Selection, SelectionGoal};-use multi_buffer::{MultiBufferDimension, MultiBufferOffset};+use multi_buffer::{MultiBufferDimension, MultiBufferOffset, ToPoint};use util::post_inc;use crate::{@@ -212,6 +212,26 @@ impl SelectionsCollection {}}+ pub fn disjoint_in_row_range<D>(+ &self,+ range: Range<Anchor>,+ snapshot: &DisplaySnapshot,+ ) -> Vec<Selection<D>>+ where+ D: MultiBufferDimension + Sub + AddAssign<<D as Sub>::Output> + Ord + std::fmt::Debug,+ {+ let buffer = snapshot.buffer_snapshot();+ let start_row = range.start.to_point(buffer).row;+ let end_row = range.end.to_point(buffer).row;+ let start_ix = self+ .disjoint+ .partition_point(|probe| probe.end.to_point(buffer).row < start_row);+ let end_ix = self+ .disjoint+ .partition_point(|probe| probe.start.to_point(buffer).row <= end_row);+ resolve_selections_wrapping_blocks(&self.disjoint[start_ix..end_ix], snapshot).collect()+ }+pub fn disjoint_in_range<D>(&self,range: Range<Anchor>,@@ -220,19 +240,13 @@ impl SelectionsCollection {whereD: MultiBufferDimension + Sub + AddAssign<<D as Sub>::Output> + Ord + std::fmt::Debug,{- let start_ix = match self+ let buffer = snapshot.buffer_snapshot();+ let start_ix = self.disjoint- .binary_search_by(|probe| probe.end.cmp(&range.start, snapshot.buffer_snapshot()))- {- Ok(ix) | Err(ix) => ix,- };- let end_ix = match self+ .partition_point(|probe| probe.end.cmp(&range.start, buffer).is_lt());+ let end_ix = self.disjoint- .binary_search_by(|probe| probe.start.cmp(&range.end, snapshot.buffer_snapshot()))- {- Ok(ix) => ix + 1,- Err(ix) => ix,- };+ .partition_point(|probe| probe.start.cmp(&range.end, buffer).is_le());resolve_selections_wrapping_blocks(&self.disjoint[start_ix..end_ix], snapshot).collect()}@@ -1352,6 +1366,125 @@ mod tests {use settings::SettingsStore;use std::sync::Arc;+ fn row_range_snapshot(cx: &mut gpui::TestAppContext, text: &str) -> DisplaySnapshot {+ cx.update(|cx| {+ let settings = SettingsStore::test(cx);+ cx.set_global(settings);+ crate::init(cx);+ });+ let buffer = cx.update(|cx| MultiBuffer::build_simple(text, cx));+ let display_map = cx.new(|cx| {+ DisplayMap::new(+ buffer,+ test_font(),+ px(14.),+ None,+ 1,+ 1,+ FoldPlaceholder::test(),+ DiagnosticSeverity::Warning,+ cx,+ )+ });+ display_map.update(cx, |map, cx| map.snapshot(cx))+ }++ fn row_range_collection(+ offset_ranges: impl IntoIterator<Item = Range<usize>>,+ buffer_snapshot: &MultiBufferSnapshot,+ ) -> SelectionsCollection {+ let selections = offset_ranges+ .into_iter()+ .enumerate()+ .map(|(id, range)| {+ selection_to_anchor_selection(+ Selection {+ id,+ start: MultiBufferOffset(range.start),+ end: MultiBufferOffset(range.end),+ reversed: false,+ goal: SelectionGoal::None,+ },+ buffer_snapshot,+ )+ })+ .collect::<Vec<_>>();+ let mut collection = SelectionsCollection::new();+ collection.disjoint = Arc::from(selections);+ collection.pending = None;+ collection+ }++ /// `disjoint_in_row_range` selects by whole rows, so a selection sharing a queried row must be+ /// returned even when its columns don't overlap the queried range. `disjoint_in_range` compares+ /// exact offsets and would miss this case.+ #[gpui::test]+ fn disjoint_in_row_range_matches_whole_row(cx: &mut gpui::TestAppContext) {+ let snapshot = row_range_snapshot(cx, "aaaa\nbbbbbbbb\ncccc");+ let buffer_snapshot = snapshot.buffer_snapshot();++ // A selection on row 1 spanning columns 3..5 (buffer offsets 8..10).+ let collection = row_range_collection([8..10], buffer_snapshot);++ // Query row 1 at columns 0..1, entirely before the selection's columns.+ let range = buffer_snapshot.anchor_before(MultiBufferOffset(5))+ ..buffer_snapshot.anchor_before(MultiBufferOffset(6));+ let result = collection.disjoint_in_row_range::<Point>(range, &snapshot);++ assert_eq!(+ result.len(),+ 1,+ "row range should include the selection sharing the queried row"+ );+ assert_eq!(result[0].start, Point::new(1, 3));+ assert_eq!(result[0].end, Point::new(1, 5));+ }++ /// A selection on a row outside the queried row range must not be returned. This guards the+ /// row-overlap boundary against becoming over-inclusive.+ #[gpui::test]+ fn disjoint_in_row_range_excludes_other_rows(cx: &mut gpui::TestAppContext) {+ let snapshot = row_range_snapshot(cx, "aaaa\nbbbbbbbb\ncccc");+ let buffer_snapshot = snapshot.buffer_snapshot();++ // A selection on row 2 (buffer offsets 14..16).+ let collection = row_range_collection([14..16], buffer_snapshot);++ // Query only row 0, two rows away from the selection.+ let range = buffer_snapshot.anchor_before(MultiBufferOffset(0))+ ..buffer_snapshot.anchor_before(MultiBufferOffset(1));+ let result = collection.disjoint_in_row_range::<Point>(range, &snapshot);++ assert!(+ result.is_empty(),+ "row range should exclude a selection on a non-queried row"+ );+ }++ /// A selection spanning multiple rows must be returned when the query touches any of those rows,+ /// not just when the query shares the selection's start or end row.+ #[gpui::test]+ fn disjoint_in_row_range_matches_interior_row(cx: &mut gpui::TestAppContext) {+ let snapshot = row_range_snapshot(cx, "aaaa\nbbbb\ncccc\ndddd");+ let buffer_snapshot = snapshot.buffer_snapshot();++ // A selection spanning row 1 column 1 through row 3 column 2 (buffer offsets 6..17).+ let collection = row_range_collection([6..17], buffer_snapshot);++ // Query only row 2, an interior row of the selection.+ let range = buffer_snapshot.anchor_before(MultiBufferOffset(11))+ ..buffer_snapshot.anchor_before(MultiBufferOffset(12));+ let result = collection.disjoint_in_row_range::<Point>(range, &snapshot);++ assert_eq!(+ result.len(),+ 1,+ "row range should include a selection whose interior row is queried"+ );+ assert_eq!(result[0].start, Point::new(1, 1));+ assert_eq!(result[0].end, Point::new(3, 2));+ }+#[gpui::test(iterations = 20)]fn fast_and_slow_selection_resolution_match_without_collapsed_content(cx: &mut gpui::TestAppContext,