aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-04-10 03:01:49 +0000
committerBlaž Hrastnik2021-04-10 03:02:23 +0000
commit95dd55ba943f3667bac3acecb2c65ee7f69d42cd (patch)
tree28074b4756f8e686a79383fd443f3037354af29c
parent7493d19098a8cea44badf4c127fbe754e9733439 (diff)
Fix overlap calculation.
-rw-r--r--helix-core/src/selection.rs17
-rw-r--r--helix-term/src/commands.rs2
2 files changed, 11 insertions, 8 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index ca1a6d32..dfd95bdd 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -59,11 +59,12 @@ impl Range {
#[must_use]
pub fn overlaps(&self, other: &Self) -> bool {
// cursor overlap is checked differently
- // if self.is_empty() {
- // self.from() <= other.to()
- // } else {
- self.to() >= other.from() && other.to() >= self.from()
- // }
+ if self.is_empty() {
+ let pos = self.head;
+ pos >= other.from() && other.to() >= pos
+ } else {
+ self.to() > other.from() && other.to() > self.from()
+ }
}
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();
self.ranges.push(range);
Self::normalize(self.ranges, index)
}
- // add_range // push
// replace_range
/// 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 let Some(prev) = result.last_mut() {
// and we overlap it
+
+ // TODO: we used to simply check range.from() <(=) prev.to()
+ // avoiding two comparisons
if range.overlaps(prev) {
let from = prev.from();
let to = std::cmp::max(range.to(), prev.to());
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index b6ae0138..d75cb6ff 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -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 selection = if extend {
- selection.clone().add(Range::new(start, end - 1))
+ selection.clone().push(Range::new(start, end - 1))
} else {
Selection::single(start, end - 1)
};