aboutsummaryrefslogtreecommitdiff
path: root/helix-core
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-03-19 02:14:13 +0000
committerBlaž Hrastnik2021-03-19 02:14:13 +0000
commitf29f01858d1b8c9e54b3293879796a4650823f60 (patch)
tree6aeb9434e110adcdbe533c0519d7dd0e2993c88b /helix-core
parente9bd9e72c3adf822ae569644dd87f4a5e04df18e (diff)
Implement iter() and len() directly on Selection.
Diffstat (limited to 'helix-core')
-rw-r--r--helix-core/src/comment.rs2
-rw-r--r--helix-core/src/selection.rs28
-rw-r--r--helix-core/src/transaction.rs2
3 files changed, 25 insertions, 7 deletions
diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs
index 35aabda9..5bcf3767 100644
--- a/helix-core/src/comment.rs
+++ b/helix-core/src/comment.rs
@@ -44,7 +44,7 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction {
let token = "//";
let comment = Tendril::from(format!("{} ", token));
- for selection in selection.ranges() {
+ for selection in selection {
let start = text.char_to_line(selection.from());
let end = text.char_to_line(selection.to());
let lines = start..end + 1;
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index c34dff31..91edcf81 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -269,6 +269,25 @@ impl Selection {
pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
self.ranges.iter().map(move |range| range.fragment(text))
}
+
+ #[inline(always)]
+ pub fn iter(&self) -> std::slice::Iter<'_, Range> {
+ self.ranges.iter()
+ }
+
+ #[inline(always)]
+ pub fn len(&self) -> usize {
+ self.ranges.len()
+ }
+}
+
+impl<'a> IntoIterator for &'a Selection {
+ type Item = &'a Range;
+ type IntoIter = std::slice::Iter<'a, Range>;
+
+ fn into_iter(self) -> std::slice::Iter<'a, Range> {
+ self.ranges().iter()
+ }
}
// TODO: checkSelection -> check if valid for doc length
@@ -279,7 +298,6 @@ pub fn keep_matches(
regex: &crate::regex::Regex,
) -> Option<Selection> {
let result: SmallVec<_> = selection
- .ranges()
.iter()
.filter(|range| regex.is_match(&range.fragment(text)))
.copied()
@@ -297,9 +315,9 @@ pub fn select_on_matches(
selection: &Selection,
regex: &crate::regex::Regex,
) -> Option<Selection> {
- let mut result = SmallVec::with_capacity(selection.ranges().len());
+ let mut result = SmallVec::with_capacity(selection.len());
- for sel in selection.ranges() {
+ for sel in selection {
// TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
let fragment = sel.fragment(text);
@@ -331,9 +349,9 @@ pub fn split_on_matches(
selection: &Selection,
regex: &crate::regex::Regex,
) -> Selection {
- let mut result = SmallVec::with_capacity(selection.ranges().len());
+ let mut result = SmallVec::with_capacity(selection.len());
- for sel in selection.ranges() {
+ for sel in selection {
// TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
let fragment = sel.fragment(text);
diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs
index 2bd100e8..1f9e63aa 100644
--- a/helix-core/src/transaction.rs
+++ b/helix-core/src/transaction.rs
@@ -494,7 +494,7 @@ impl Transaction {
where
F: FnMut(&Range) -> Change,
{
- Self::change(doc, selection.ranges().iter().map(f))
+ Self::change(doc, selection.iter().map(f))
}
/// Insert text at each selection head.