From dd749bb2846584aa20078cfa2aea4fe18678c12c Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Mon, 7 Sep 2020 17:08:28 +0900 Subject: Expand transaction API. --- helix-core/src/selection.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'helix-core/src/selection.rs') diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 1c0b6b74..a196417a 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -113,6 +113,11 @@ impl Selection { self.ranges[self.primary_index] } + #[must_use] + pub fn cursor(&self) -> usize { + self.primary().head + } + /// Ensure selection containing only the primary selection. pub fn into_single(self) -> Self { if self.ranges.len() == 1 { @@ -144,6 +149,10 @@ impl Selection { ) } + pub fn ranges(&self) -> &[Range] { + &self.ranges + } + #[must_use] /// Constructs a selection holding a single range. pub fn single(anchor: usize, head: usize) -> Self { @@ -200,6 +209,14 @@ impl Selection { // TODO: only normalize if needed (any ranges out of order) normalize(ranges, primary_index) } + + /// Takes a closure and maps each selection over the closure. + pub fn transform(self, f: F) -> Self + where + F: Fn(Range) -> Range, + { + Self::new(self.ranges.into_iter().map(f).collect(), self.primary_index) + } } // TODO: checkSelection -> check if valid for doc length -- cgit v1.2.3-70-g09d2