on master help sign in
Implement iter() and len() directly on Selection.
f29f018Blaž Hrastnik
helix-core/src/comment.rs+1−1
@@ -44,7 +44,7 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction {
44 let token = "//";
44 let token = "//";
45 let comment = Tendril::from(format!("{} ", token));
45 let comment = Tendril::from(format!("{} ", token));
46
46
47− for selection in selection.ranges() {
47+ for selection in selection {
48 let start = text.char_to_line(selection.from());
48 let start = text.char_to_line(selection.from());
49 let end = text.char_to_line(selection.to());
49 let end = text.char_to_line(selection.to());
50 let lines = start..end + 1;
50 let lines = start..end + 1;
helix-core/src/selection.rs+23−5
@@ -269,6 +269,25 @@ impl Selection {
269 pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
269 pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
270 self.ranges.iter().map(move |range| range.fragment(text))
270 self.ranges.iter().map(move |range| range.fragment(text))
271 }
271 }
272+
273+ #[inline(always)]
274+ pub fn iter(&self) -> std::slice::Iter<'_, Range> {
275+ self.ranges.iter()
276+ }
277+
278+ #[inline(always)]
279+ pub fn len(&self) -> usize {
280+ self.ranges.len()
281+ }
282+}
283+
284+impl<'a> IntoIterator for &'a Selection {
285+ type Item = &'a Range;
286+ type IntoIter = std::slice::Iter<'a, Range>;
287+
288+ fn into_iter(self) -> std::slice::Iter<'a, Range> {
289+ self.ranges().iter()
290+ }
272}
291}
273
292
274// TODO: checkSelection -> check if valid for doc length
293// TODO: checkSelection -> check if valid for doc length
@@ -279,7 +298,6 @@ pub fn keep_matches(
279 regex: &crate::regex::Regex,
298 regex: &crate::regex::Regex,
280) -> Option<Selection> {
299) -> Option<Selection> {
281 let result: SmallVec<_> = selection
300 let result: SmallVec<_> = selection
282− .ranges()
283 .iter()
301 .iter()
284 .filter(|range| regex.is_match(&range.fragment(text)))
302 .filter(|range| regex.is_match(&range.fragment(text)))
285 .copied()
303 .copied()
@@ -297,9 +315,9 @@ pub fn select_on_matches(
297 selection: &Selection,
315 selection: &Selection,
298 regex: &crate::regex::Regex,
316 regex: &crate::regex::Regex,
299) -> Option<Selection> {
317) -> Option<Selection> {
300− let mut result = SmallVec::with_capacity(selection.ranges().len());
318+ let mut result = SmallVec::with_capacity(selection.len());
301
319
302− for sel in selection.ranges() {
320+ for sel in selection {
303 // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
321 // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
304 let fragment = sel.fragment(text);
322 let fragment = sel.fragment(text);
305
323
@@ -331,9 +349,9 @@ pub fn split_on_matches(
331 selection: &Selection,
349 selection: &Selection,
332 regex: &crate::regex::Regex,
350 regex: &crate::regex::Regex,
333) -> Selection {
351) -> Selection {
334− let mut result = SmallVec::with_capacity(selection.ranges().len());
352+ let mut result = SmallVec::with_capacity(selection.len());
335
353
336− for sel in selection.ranges() {
354+ for sel in selection {
337 // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
355 // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
338 let fragment = sel.fragment(text);
356 let fragment = sel.fragment(text);
339
357
helix-core/src/transaction.rs+1−1
@@ -494,7 +494,7 @@ impl Transaction {
494 where
494 where
495 F: FnMut(&Range) -> Change,
495 F: FnMut(&Range) -> Change,
496 {
496 {
497− Self::change(doc, selection.ranges().iter().map(f))
497+ Self::change(doc, selection.iter().map(f))
498 }
498 }
499
499
500 /// Insert text at each selection head.
500 /// Insert text at each selection head.
helix-term/src/commands.rs+2−4
@@ -781,7 +781,6 @@ pub fn buffer_picker(cx: &mut Context) {
781// calculate line numbers for each selection range
781// calculate line numbers for each selection range
782fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
782fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
783 let mut lines = selection
783 let mut lines = selection
784− .ranges()
785 .iter()
784 .iter()
786 .map(|range| doc.char_to_line(range.head))
785 .map(|range| doc.char_to_line(range.head))
787 .collect::<Vec<_>>();
786 .collect::<Vec<_>>();
@@ -1145,7 +1144,7 @@ fn get_lines(doc: &Document) -> Vec<usize> {
1145 let mut lines = Vec::new();
1144 let mut lines = Vec::new();
1146
1145
1147 // Get all line numbers
1146 // Get all line numbers
1148− for range in doc.selection().ranges() {
1147+ for range in doc.selection() {
1149 let start = doc.text().char_to_line(range.from());
1148 let start = doc.text().char_to_line(range.from());
1150 let end = doc.text().char_to_line(range.to());
1149 let end = doc.text().char_to_line(range.to());
1151
1150
@@ -1220,7 +1219,6 @@ pub fn format_selections(cx: &mut Context) {
1220
1219
1221 let ranges: Vec<lsp::Range> = doc
1220 let ranges: Vec<lsp::Range> = doc
1222 .selection()
1221 .selection()
1223− .ranges()
1224 .iter()
1222 .iter()
1225 .map(|range| helix_lsp::util::range_to_lsp_range(doc.text(), *range))
1223 .map(|range| helix_lsp::util::range_to_lsp_range(doc.text(), *range))
1226 .collect();
1224 .collect();
@@ -1257,7 +1255,7 @@ pub fn join_selections(cx: &mut Context) {
1257 let mut changes = Vec::new();
1255 let mut changes = Vec::new();
1258 let fragment = Tendril::from(" ");
1256 let fragment = Tendril::from(" ");
1259
1257
1260− for selection in doc.selection().ranges() {
1258+ for selection in doc.selection() {
1261 let start = text.char_to_line(selection.from());
1259 let start = text.char_to_line(selection.from());
1262 let mut end = text.char_to_line(selection.to());
1260 let mut end = text.char_to_line(selection.to());
1263 if start == end {
1261 if start == end {
helix-term/src/ui/editor.rs−1
@@ -211,7 +211,6 @@ impl EditorView {
211 for selection in view
211 for selection in view
212 .doc
212 .doc
213 .selection()
213 .selection()
214− .ranges()
215 .iter()
214 .iter()
216 .filter(|range| range.overlaps(&screen))
215 .filter(|range| range.overlaps(&screen))
217 {
216 {