on master help sign in
Implement iter() and len() directly on Selection.
commitf29f01858d1b8c9e54b3293879796a4650823f60 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 19 Mar 2021, 11:14 (6 years ago) parente9bd9e7
5 files changed+27−12
helix-core/src/comment.rs+1−1 helix-core/src/selection.rs+23−5 helix-core/src/transaction.rs+1−1 helix-term/src/commands.rs+2−4 helix-term/src/ui/editor.rs−1
helix-core/src/comment.rs+1−1
@@ -44,7 +44,7 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction {
4444 let token = "//";
4545 let comment = Tendril::from(format!("{} ", token));
4646
47− for selection in selection.ranges() {
47+ for selection in selection {
4848 let start = text.char_to_line(selection.from());
4949 let end = text.char_to_line(selection.to());
5050 let lines = start..end + 1;
helix-core/src/selection.rs+23−5
@@ -269,6 +269,25 @@ impl Selection {
269269 pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
270270 self.ranges.iter().map(move |range| range.fragment(text))
271271 }
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+ }
272291}
273292
274293// TODO: checkSelection -> check if valid for doc length
@@ -279,7 +298,6 @@ pub fn keep_matches(
279298 regex: &crate::regex::Regex,
280299) -> Option<Selection> {
281300 let result: SmallVec<_> = selection
282− .ranges()
283301 .iter()
284302 .filter(|range| regex.is_match(&range.fragment(text)))
285303 .copied()
@@ -297,9 +315,9 @@ pub fn select_on_matches(
297315 selection: &Selection,
298316 regex: &crate::regex::Regex,
299317) -> Option<Selection> {
300− let mut result = SmallVec::with_capacity(selection.ranges().len());
318+ let mut result = SmallVec::with_capacity(selection.len());
301319
302− for sel in selection.ranges() {
320+ for sel in selection {
303321 // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
304322 let fragment = sel.fragment(text);
305323
@@ -331,9 +349,9 @@ pub fn split_on_matches(
331349 selection: &Selection,
332350 regex: &crate::regex::Regex,
333351) -> Selection {
334− let mut result = SmallVec::with_capacity(selection.ranges().len());
352+ let mut result = SmallVec::with_capacity(selection.len());
335353
336− for sel in selection.ranges() {
354+ for sel in selection {
337355 // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
338356 let fragment = sel.fragment(text);
339357
helix-core/src/transaction.rs+1−1
@@ -494,7 +494,7 @@ impl Transaction {
494494 where
495495 F: FnMut(&Range) -> Change,
496496 {
497− Self::change(doc, selection.ranges().iter().map(f))
497+ Self::change(doc, selection.iter().map(f))
498498 }
499499
500500 /// Insert text at each selection head.
helix-term/src/commands.rs+2−4
@@ -781,7 +781,6 @@ pub fn buffer_picker(cx: &mut Context) {
781781// calculate line numbers for each selection range
782782fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
783783 let mut lines = selection
784− .ranges()
785784 .iter()
786785 .map(|range| doc.char_to_line(range.head))
787786 .collect::<Vec<_>>();
@@ -1145,7 +1144,7 @@ fn get_lines(doc: &Document) -> Vec<usize> {
11451144 let mut lines = Vec::new();
11461145
11471146 // Get all line numbers
1148− for range in doc.selection().ranges() {
1147+ for range in doc.selection() {
11491148 let start = doc.text().char_to_line(range.from());
11501149 let end = doc.text().char_to_line(range.to());
11511150
@@ -1220,7 +1219,6 @@ pub fn format_selections(cx: &mut Context) {
12201219
12211220 let ranges: Vec<lsp::Range> = doc
12221221 .selection()
1223− .ranges()
12241222 .iter()
12251223 .map(|range| helix_lsp::util::range_to_lsp_range(doc.text(), *range))
12261224 .collect();
@@ -1257,7 +1255,7 @@ pub fn join_selections(cx: &mut Context) {
12571255 let mut changes = Vec::new();
12581256 let fragment = Tendril::from(" ");
12591257
1260− for selection in doc.selection().ranges() {
1258+ for selection in doc.selection() {
12611259 let start = text.char_to_line(selection.from());
12621260 let mut end = text.char_to_line(selection.to());
12631261 if start == end {
helix-term/src/ui/editor.rs−1
@@ -211,7 +211,6 @@ impl EditorView {
211211 for selection in view
212212 .doc
213213 .selection()
214− .ranges()
215214 .iter()
216215 .filter(|range| range.overlaps(&screen))
217216 {