Create valid disjoint selection sets in selections_in_ranges

This commit is contained in:
Max Brunsfeld 2021-10-28 16:04:16 -07:00
parent efc85d1b75
commit f3cd710f21
2 changed files with 21 additions and 18 deletions

View File

@ -1487,25 +1487,28 @@ impl Buffer {
let mut ranges = ranges.into_iter().collect::<Vec<_>>();
ranges.sort_unstable_by_key(|range| range.start);
let mut selections = Vec::with_capacity(ranges.len());
for range in ranges {
let mut selections = Vec::<Selection<usize>>::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)
}

View File

@ -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";