aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/selection.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-core/src/selection.rs')
-rw-r--r--helix-core/src/selection.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index 80bb6a0c..f934f74d 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -60,6 +60,18 @@ impl Range {
}
}
+ pub fn contains(&self, pos: usize) -> bool {
+ if self.is_empty() {
+ return false;
+ }
+
+ if self.anchor < self.head {
+ self.anchor <= pos && pos < self.head
+ } else {
+ self.head < pos && pos <= self.anchor
+ }
+ }
+
/// Map a range through a set of changes. Returns a new range representing the same position
/// after the changes are applied.
pub fn map(self, changes: &ChangeSet) -> Self {
@@ -283,4 +295,21 @@ mod test {
assert_eq!(res, "8/10,10/12");
}
+
+ #[test]
+ fn test_contains() {
+ let range = Range::new(10, 12);
+
+ assert_eq!(range.contains(9), false);
+ assert_eq!(range.contains(10), true);
+ assert_eq!(range.contains(11), true);
+ assert_eq!(range.contains(12), false);
+ assert_eq!(range.contains(13), false);
+
+ let range = Range::new(9, 6);
+ assert_eq!(range.contains(9), true);
+ assert_eq!(range.contains(7), true);
+ assert_eq!(range.contains(6), false);
+ }
+
}