diff --git a/crates/buffer/src/lib.rs b/crates/buffer/src/lib.rs index ffee9d5736..47e40800ed 100644 --- a/crates/buffer/src/lib.rs +++ b/crates/buffer/src/lib.rs @@ -1487,25 +1487,28 @@ impl Buffer { let mut ranges = ranges.into_iter().collect::>(); ranges.sort_unstable_by_key(|range| range.start); - let mut selections = Vec::with_capacity(ranges.len()); - for range in ranges { + let mut selections = Vec::>::with_capacity(ranges.len()); + for mut range in ranges { + let mut reversed = false; if range.start > range.end { - selections.push(Selection { - id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst), - start: range.end, - end: range.start, - reversed: true, - goal: SelectionGoal::None, - }); - } else { - selections.push(Selection { - id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst), - start: range.start, - end: range.end, - reversed: false, - goal: SelectionGoal::None, - }); + reversed = true; + std::mem::swap(&mut range.start, &mut range.end); } + + if let Some(selection) = selections.last_mut() { + if selection.end >= range.start { + selection.end = range.end; + continue; + } + } + + selections.push(Selection { + id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst), + start: range.start, + end: range.end, + reversed, + goal: SelectionGoal::None, + }); } Ok(selections) } diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index f763a57699..61e4dfc9a4 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -3323,7 +3323,7 @@ mod tests { #[gpui::test] async fn test_buffer_file_changes_on_disk(mut cx: gpui::TestAppContext) { - use buffer::{Point, Selection, SelectionGoal, ToPoint}; + use buffer::{Point, Selection, SelectionGoal}; use std::fs; let initial_contents = "aaa\nbbbbb\nc\n";