on master help sign in
Fix overlap calculation.
95dd55bBlaž Hrastnik
helix-core/src/selection.rs+10−7
@@ -59,11 +59,12 @@ impl Range {
59 #[must_use]
59 #[must_use]
60 pub fn overlaps(&self, other: &Self) -> bool {
60 pub fn overlaps(&self, other: &Self) -> bool {
61 // cursor overlap is checked differently
61 // cursor overlap is checked differently
62− // if self.is_empty() {
62+ if self.is_empty() {
63+ let pos = self.head;
63− // self.from() <= other.to()
64+ pos >= other.from() && other.to() >= pos
64− // } else {
65+ } else {
65− self.to() >= other.from() && other.to() >= self.from()
66+ self.to() > other.from() && other.to() > self.from()
66− // }
67+ }
67 }
68 }
68
69
69 pub fn contains(&self, pos: usize) -> bool {
70 pub fn contains(&self, pos: usize) -> bool {
@@ -159,13 +160,12 @@ impl Selection {
159 }
160 }
160 }
161 }
161
162
162− pub fn add(mut self, range: Range) -> Self {
163+ pub fn push(mut self, range: Range) -> Self {
163 let index = self.ranges.len();
164 let index = self.ranges.len();
164 self.ranges.push(range);
165 self.ranges.push(range);
165
166
166 Self::normalize(self.ranges, index)
167 Self::normalize(self.ranges, index)
167 }
168 }
168− // add_range // push
169 // replace_range
169 // replace_range
170
170
171 /// Map selections over a set of changes. Useful for adjusting the selection position after
171 /// Map selections over a set of changes. Useful for adjusting the selection position after
@@ -223,6 +223,9 @@ impl Selection {
223 // if previous value exists
223 // if previous value exists
224 if let Some(prev) = result.last_mut() {
224 if let Some(prev) = result.last_mut() {
225 // and we overlap it
225 // and we overlap it
226+
227+ // TODO: we used to simply check range.from() <(=) prev.to()
228+ // avoiding two comparisons
226 if range.overlaps(prev) {
229 if range.overlaps(prev) {
227 let from = prev.from();
230 let from = prev.from();
228 let to = std::cmp::max(range.to(), prev.to());
231 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
622 let end = text.byte_to_char(mat.end());
622 let end = text.byte_to_char(mat.end());
623
623
624 let selection = if extend {
624 let selection = if extend {
625− selection.clone().add(Range::new(start, end - 1))
625+ selection.clone().push(Range::new(start, end - 1))
626 } else {
626 } else {
627 Selection::single(start, end - 1)
627 Selection::single(start, end - 1)
628 };
628 };