on master help sign in
Fix overlap calculation.
commit95dd55ba943f3667bac3acecb2c65ee7f69d42cd authorBlaž Hrastnik <blaz@mxxn.io> dateSat 10 Apr 2021, 12:01 (6 years ago) parent7493d19
2 files changed+11−8
helix-core/src/selection.rs+10−7 helix-term/src/commands.rs+1−1
helix-core/src/selection.rs+10−7
@@ -59,11 +59,12 @@ impl Range {
5959 #[must_use]
6060 pub fn overlaps(&self, other: &Self) -> bool {
6161 // cursor overlap is checked differently
62− // if self.is_empty() {
63− // self.from() <= other.to()
64− // } else {
65− self.to() >= other.from() && other.to() >= self.from()
66− // }
62+ if self.is_empty() {
63+ let pos = self.head;
64+ pos >= other.from() && other.to() >= pos
65+ } else {
66+ self.to() > other.from() && other.to() > self.from()
67+ }
6768 }
6869
6970 pub fn contains(&self, pos: usize) -> bool {
@@ -159,13 +160,12 @@ impl Selection {
159160 }
160161 }
161162
162− pub fn add(mut self, range: Range) -> Self {
163+ pub fn push(mut self, range: Range) -> Self {
163164 let index = self.ranges.len();
164165 self.ranges.push(range);
165166
166167 Self::normalize(self.ranges, index)
167168 }
168− // add_range // push
169169 // replace_range
170170
171171 /// Map selections over a set of changes. Useful for adjusting the selection position after
@@ -223,6 +223,9 @@ impl Selection {
223223 // if previous value exists
224224 if let Some(prev) = result.last_mut() {
225225 // and we overlap it
226+
227+ // TODO: we used to simply check range.from() <(=) prev.to()
228+ // avoiding two comparisons
226229 if range.overlaps(prev) {
227230 let from = prev.from();
228231 let to = std::cmp::max(range.to(), prev.to());
helix-term/src/commands.rs+1−1
@@ -622,7 +622,7 @@ fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex, e
622622 let end = text.byte_to_char(mat.end());
623623
624624 let selection = if extend {
625− selection.clone().add(Range::new(start, end - 1))
625+ selection.clone().push(Range::new(start, end - 1))
626626 } else {
627627 Selection::single(start, end - 1)
628628 };