Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/selection.rs')
-rw-r--r--helix-core/src/selection.rs41
1 files changed, 16 insertions, 25 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index e29fcc94..76de6362 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -11,6 +11,7 @@ use crate::{
movement::Direction,
Assoc, ChangeSet, RopeGraphemes, RopeSlice,
};
+use helix_stdx::range::is_subset;
use helix_stdx::rope::{self, RopeSliceExt};
use smallvec::{smallvec, SmallVec};
use std::{borrow::Cow, iter, slice};
@@ -401,6 +402,15 @@ impl From<(usize, usize)> for Range {
}
}
+impl From<Range> for helix_stdx::Range {
+ fn from(range: Range) -> Self {
+ Self {
+ start: range.from(),
+ end: range.to(),
+ }
+ }
+}
+
/// A selection consists of one or more selection ranges.
/// invariant: A selection can never be empty (always contains at least primary range).
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -513,6 +523,10 @@ impl Selection {
}
}
+ pub fn range_bounds(&self) -> impl Iterator<Item = helix_stdx::Range> + '_ {
+ self.ranges.iter().map(|&range| range.into())
+ }
+
pub fn primary_index(&self) -> usize {
self.primary_index
}
@@ -683,32 +697,9 @@ impl Selection {
self.ranges.len()
}
- // returns true if self ⊇ other
+ /// returns true if self ⊇ other
pub fn contains(&self, other: &Selection) -> bool {
- let (mut iter_self, mut iter_other) = (self.iter(), other.iter());
- let (mut ele_self, mut ele_other) = (iter_self.next(), iter_other.next());
-
- loop {
- match (ele_self, ele_other) {
- (Some(ra), Some(rb)) => {
- if !ra.contains_range(rb) {
- // `self` doesn't contain next element from `other`, advance `self`, we need to match all from `other`
- ele_self = iter_self.next();
- } else {
- // matched element from `other`, advance `other`
- ele_other = iter_other.next();
- };
- }
- (None, Some(_)) => {
- // exhausted `self`, we can't match the reminder of `other`
- return false;
- }
- (_, None) => {
- // no elements from `other` left to match, `self` contains `other`
- return true;
- }
- }
- }
+ is_subset::<true>(self.range_bounds(), other.range_bounds())
}
}