Fix overlap calculation.

pull/11/head
Blaž Hrastnik 4 years ago
parent 7493d19098
commit 95dd55ba94

@ -59,11 +59,12 @@ impl Range {
#[must_use] #[must_use]
pub fn overlaps(&self, other: &Self) -> bool { pub fn overlaps(&self, other: &Self) -> bool {
// cursor overlap is checked differently // cursor overlap is checked differently
// if self.is_empty() { if self.is_empty() {
// self.from() <= other.to() let pos = self.head;
// } else { pos >= other.from() && other.to() >= pos
self.to() >= other.from() && other.to() >= self.from() } else {
// } self.to() > other.from() && other.to() > self.from()
}
} }
pub fn contains(&self, pos: usize) -> bool { pub fn contains(&self, pos: usize) -> bool {
@ -159,13 +160,12 @@ impl Selection {
} }
} }
pub fn add(mut self, range: Range) -> Self { pub fn push(mut self, range: Range) -> Self {
let index = self.ranges.len(); let index = self.ranges.len();
self.ranges.push(range); self.ranges.push(range);
Self::normalize(self.ranges, index) Self::normalize(self.ranges, index)
} }
// add_range // push
// replace_range // replace_range
/// Map selections over a set of changes. Useful for adjusting the selection position after /// Map selections over a set of changes. Useful for adjusting the selection position after
@ -223,6 +223,9 @@ impl Selection {
// if previous value exists // if previous value exists
if let Some(prev) = result.last_mut() { if let Some(prev) = result.last_mut() {
// and we overlap it // and we overlap it
// TODO: we used to simply check range.from() <(=) prev.to()
// avoiding two comparisons
if range.overlaps(prev) { if range.overlaps(prev) {
let from = prev.from(); let from = prev.from();
let to = std::cmp::max(range.to(), prev.to()); let to = std::cmp::max(range.to(), prev.to());

@ -622,7 +622,7 @@ fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex, e
let end = text.byte_to_char(mat.end()); let end = text.byte_to_char(mat.end());
let selection = if extend { let selection = if extend {
selection.clone().add(Range::new(start, end - 1)) selection.clone().push(Range::new(start, end - 1))
} else { } else {
Selection::single(start, end - 1) Selection::single(start, end - 1)
}; };

Loading…
Cancel
Save