aboutsummaryrefslogtreecommitdiff
path: root/helix-core
diff options
context:
space:
mode:
Diffstat (limited to 'helix-core')
-rw-r--r--helix-core/src/selection.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index 83bab5e3..59bd736e 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -222,9 +222,23 @@ impl Range {
// groupAt
+ /// Returns the text inside this range given the text of the whole buffer.
+ ///
+ /// The returned `Cow` is a reference if the range of text is inside a single
+ /// chunk of the rope. Otherwise a copy of the text is returned. Consider
+ /// using `slice` instead if you do not need a `Cow` or `String` to avoid copying.
#[inline]
pub fn fragment<'a, 'b: 'a>(&'a self, text: RopeSlice<'b>) -> Cow<'b, str> {
- text.slice(self.from()..self.to()).into()
+ self.slice(text).into()
+ }
+
+ /// Returns the text inside this range given the text of the whole buffer.
+ ///
+ /// The returned value is a reference to the passed slice. This method never
+ /// copies any contents.
+ #[inline]
+ pub fn slice<'a, 'b: 'a>(&'a self, text: RopeSlice<'b>) -> RopeSlice<'b> {
+ text.slice(self.from()..self.to())
}
//--------------------------------
@@ -548,6 +562,10 @@ impl Selection {
self.ranges.iter().map(move |range| range.fragment(text))
}
+ pub fn slices<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = RopeSlice> + 'a {
+ self.ranges.iter().map(move |range| range.slice(text))
+ }
+
#[inline(always)]
pub fn iter(&self) -> std::slice::Iter<'_, Range> {
self.ranges.iter()