diff --git a/crates/buffer/src/anchor.rs b/crates/buffer/src/anchor.rs index d1577230cc..da47d8924b 100644 --- a/crates/buffer/src/anchor.rs +++ b/crates/buffer/src/anchor.rs @@ -1,3 +1,5 @@ +use crate::rope::TextDimension; + use super::{Buffer, Content, FromAnchor, FullOffset, Point, ToOffset}; use anyhow::Result; use std::{ @@ -183,31 +185,22 @@ impl AnchorRangeMap { Self { version, entries } } + pub fn ranges<'a, D>( + &'a self, + content: impl Into> + 'a, + ) -> impl Iterator, &'a T)> + 'a + where + D: 'a + TextDimension<'a>, + { + let content = content.into(); + content.summaries_for_anchor_ranges(self) + } + pub fn full_offset_ranges(&self) -> impl Iterator, &T)> { self.entries .iter() .map(|(range, value)| (range.start.0..range.end.0, value)) } - - pub fn point_ranges<'a>( - &'a self, - content: impl Into> + 'a, - ) -> impl Iterator, &'a T)> + 'a { - let content = content.into(); - content - .summaries_for_anchor_ranges(self) - .map(move |(range, value)| ((range.start.lines..range.end.lines), value)) - } - - pub fn offset_ranges<'a>( - &'a self, - content: impl Into> + 'a, - ) -> impl Iterator, &'a T)> + 'a { - let content = content.into(); - content - .summaries_for_anchor_ranges(self) - .map(move |(range, value)| ((range.start.bytes..range.end.bytes), value)) - } } impl PartialEq for AnchorRangeMap { @@ -248,18 +241,12 @@ impl AnchorRangeSet { self.0.version() } - pub fn offset_ranges<'a>( - &'a self, - content: impl Into> + 'a, - ) -> impl Iterator> + 'a { - self.0.offset_ranges(content).map(|(range, _)| range) - } - - pub fn point_ranges<'a>( - &'a self, - content: impl Into> + 'a, - ) -> impl Iterator> + 'a { - self.0.point_ranges(content).map(|(range, _)| range) + pub fn ranges<'a, D, C>(&'a self, content: C) -> impl 'a + Iterator> + where + D: 'a + TextDimension<'a>, + C: 'a + Into>, + { + self.0.ranges(content).map(|(range, _)| range) } } diff --git a/crates/buffer/src/lib.rs b/crates/buffer/src/lib.rs index bf97574d34..85847468c8 100644 --- a/crates/buffer/src/lib.rs +++ b/crates/buffer/src/lib.rs @@ -1486,10 +1486,14 @@ impl Buffer { Ok(selections) } - pub fn selection_ranges<'a>(&'a self, set_id: SelectionSetId) -> Result>> { + #[cfg(test)] + pub fn selection_ranges<'a, D>(&'a self, set_id: SelectionSetId) -> Result>> + where + D: 'a + TextDimension<'a>, + { Ok(self .selection_set(set_id)? - .offset_selections(self) + .selections(self) .map(move |selection| { if selection.reversed { selection.end..selection.start @@ -1500,9 +1504,13 @@ impl Buffer { .collect()) } - pub fn all_selection_ranges<'a>( + #[cfg(test)] + pub fn all_selection_ranges<'a, D>( &'a self, - ) -> impl 'a + Iterator>)> { + ) -> impl 'a + Iterator>)> + where + D: 'a + TextDimension<'a>, + { self.selections .keys() .map(move |set_id| (*set_id, self.selection_ranges(*set_id).unwrap())) @@ -1751,12 +1759,15 @@ impl<'a> Content<'a> { }) } - fn summaries_for_anchor_ranges( + fn summaries_for_anchor_ranges( &self, map: &'a AnchorRangeMap, - ) -> impl Iterator, &'a T)> { + ) -> impl Iterator, &'a T)> + where + D: TextDimension<'a>, + { let cx = Some(map.version.clone()); - let mut summary = TextSummary::default(); + let mut summary = D::default(); let mut rope_cursor = self.visible_text.cursor(0); let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>(); map.entries.iter().map(move |(range, value)| { @@ -1775,7 +1786,7 @@ impl<'a> Content<'a> { } else { 0 }; - summary += rope_cursor.summary::(cursor.start().1 + overshoot); + summary.add_assign(&rope_cursor.summary::(cursor.start().1 + overshoot)); let start_summary = summary.clone(); cursor.seek_forward(&VersionedFullOffset::Offset(*end_offset), *end_bias, &cx); @@ -1784,7 +1795,7 @@ impl<'a> Content<'a> { } else { 0 }; - summary += rope_cursor.summary::(cursor.start().1 + overshoot); + summary.add_assign(&rope_cursor.summary::(cursor.start().1 + overshoot)); let end_summary = summary.clone(); (start_summary..end_summary, value) diff --git a/crates/buffer/src/selection.rs b/crates/buffer/src/selection.rs index c55a5f423b..8d804c522d 100644 --- a/crates/buffer/src/selection.rs +++ b/crates/buffer/src/selection.rs @@ -1,3 +1,5 @@ +use crate::rope::TextDimension; + use super::{AnchorRangeMap, Buffer, Content, Point, ToOffset, ToPoint}; use std::{cmp::Ordering, ops::Range, sync::Arc}; @@ -97,27 +99,13 @@ impl SelectionSet { self.selections.len() } - pub fn offset_selections<'a>( - &'a self, - content: impl Into> + 'a, - ) -> impl 'a + Iterator> { + pub fn selections<'a, D, C>(&'a self, content: C) -> impl 'a + Iterator> + where + D: 'a + TextDimension<'a>, + C: 'a + Into>, + { self.selections - .offset_ranges(content) - .map(|(range, state)| Selection { - id: state.id, - start: range.start, - end: range.end, - reversed: state.reversed, - goal: state.goal, - }) - } - - pub fn point_selections<'a>( - &'a self, - content: impl Into> + 'a, - ) -> impl 'a + Iterator> { - self.selections - .point_ranges(content) + .ranges(content) .map(|(range, state)| Selection { id: state.id, start: range.start, diff --git a/crates/buffer/src/tests.rs b/crates/buffer/src/tests.rs index 68d6e6aa35..2a8b938114 100644 --- a/crates/buffer/src/tests.rs +++ b/crates/buffer/src/tests.rs @@ -576,9 +576,11 @@ fn test_random_concurrent_edits(mut rng: StdRng) { first_buffer.selection_sets().collect::>() ); assert_eq!( - buffer.all_selection_ranges().collect::>(), + buffer + .all_selection_ranges::() + .collect::>(), first_buffer - .all_selection_ranges() + .all_selection_ranges::() .collect::>() ); } diff --git a/crates/editor/src/lib.rs b/crates/editor/src/lib.rs index 1f03133c84..275fa804b1 100644 --- a/crates/editor/src/lib.rs +++ b/crates/editor/src/lib.rs @@ -5,6 +5,7 @@ pub mod movement; #[cfg(test)] mod test; +use buffer::rope::TextDimension; use clock::ReplicaId; pub use display_map::DisplayPoint; use display_map::*; @@ -512,7 +513,7 @@ impl Editor { return false; } - let mut selections = self.point_selections(cx).peekable(); + let mut selections = self.selections::(cx).peekable(); let first_cursor_top = selections .peek() .unwrap() @@ -564,7 +565,7 @@ impl Editor { cx: &mut ViewContext, ) -> bool { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let selections = self.point_selections(cx); + let selections = self.selections::(cx); let mut target_left = std::f32::INFINITY; let mut target_right = 0.0_f32; for selection in selections { @@ -655,7 +656,7 @@ impl Editor { fn end_selection(&mut self, cx: &mut ViewContext) { if let Some(selection) = self.pending_selection.take() { - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let ix = self.selection_insertion_index(&selections, selection.start); selections.insert(ix, selection); self.update_selections(selections, false, cx); @@ -668,12 +669,12 @@ impl Editor { pub fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext) { if let Some(pending_selection) = self.pending_selection.take() { - if self.point_selections(cx).next().is_none() { + if self.selections::(cx).next().is_none() { self.update_selections(vec![pending_selection], true, cx); } } else { let selection_count = self.selection_count(cx); - let selections = self.point_selections(cx); + let selections = self.selections::(cx); let mut oldest_selection = selections.min_by_key(|s| s.id).unwrap().clone(); if selection_count == 1 { oldest_selection.start = oldest_selection.head().clone(); @@ -760,7 +761,7 @@ impl Editor { self.start_transaction(cx); let mut old_selections = SmallVec::<[_; 32]>::new(); { - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let buffer = self.buffer.read(cx); for selection in selections.iter() { let start_point = selection.start; @@ -882,7 +883,7 @@ impl Editor { fn insert(&mut self, text: &str, cx: &mut ViewContext) { self.start_transaction(cx); - let old_selections = self.offset_selections(cx).collect::>(); + let old_selections = self.selections::(cx).collect::>(); let mut new_selections = Vec::new(); self.buffer.update(cx, |buffer, cx| { let edit_ranges = old_selections.iter().map(|s| s.start..s.end); @@ -913,7 +914,7 @@ impl Editor { } fn autoclose_pairs(&mut self, cx: &mut ViewContext) { - let selections = self.offset_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let new_autoclose_pair_state = self.buffer.update(cx, |buffer, cx| { let autoclose_pair = buffer.language().and_then(|language| { let first_selection_start = selections.first().unwrap().start; @@ -970,7 +971,7 @@ impl Editor { fn skip_autoclose_end(&mut self, text: &str, cx: &mut ViewContext) -> bool { let old_selection_count = self.selection_count(cx); - let old_selections = self.offset_selections(cx).collect::>(); + let old_selections = self.selections::(cx).collect::>(); let autoclose_pair_state = if let Some(autoclose_pair_state) = self.autoclose_stack.last() { autoclose_pair_state } else { @@ -985,7 +986,7 @@ impl Editor { let buffer = self.buffer.read(cx); if old_selections .iter() - .zip(autoclose_pair_state.ranges.offset_ranges(buffer)) + .zip(autoclose_pair_state.ranges.ranges::(buffer)) .all(|(selection, autoclose_range)| { let autoclose_range_end = autoclose_range.end.to_offset(buffer); selection.is_empty() && selection.start == autoclose_range_end @@ -1021,7 +1022,7 @@ impl Editor { pub fn backspace(&mut self, _: &Backspace, cx: &mut ViewContext) { self.start_transaction(cx); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); for selection in &mut selections { if selection.is_empty() { @@ -1041,7 +1042,7 @@ impl Editor { pub fn delete(&mut self, _: &Delete, cx: &mut ViewContext) { self.start_transaction(cx); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { if selection.is_empty() { let head = selection.head().to_display_point(&display_map, Bias::Left); @@ -1060,7 +1061,7 @@ impl Editor { pub fn tab(&mut self, _: &Tab, cx: &mut ViewContext) { self.start_transaction(cx); let tab_size = self.build_settings.borrow()(cx).tab_size; - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); self.buffer.update(cx, |buffer, cx| { let mut last_indented_row = None; for selection in &mut selections { @@ -1101,7 +1102,7 @@ impl Editor { pub fn delete_line(&mut self, _: &DeleteLine, cx: &mut ViewContext) { self.start_transaction(cx); - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = self.buffer.read(cx); @@ -1176,7 +1177,7 @@ impl Editor { pub fn duplicate_line(&mut self, _: &DuplicateLine, cx: &mut ViewContext) { self.start_transaction(cx); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = self.buffer.read(cx); @@ -1234,7 +1235,7 @@ impl Editor { pub fn move_line_up(&mut self, _: &MoveLineUp, cx: &mut ViewContext) { self.start_transaction(cx); - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = self.buffer.read(cx); @@ -1324,7 +1325,7 @@ impl Editor { pub fn move_line_down(&mut self, _: &MoveLineDown, cx: &mut ViewContext) { self.start_transaction(cx); - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = self.buffer.read(cx); @@ -1411,7 +1412,7 @@ impl Editor { pub fn cut(&mut self, _: &Cut, cx: &mut ViewContext) { self.start_transaction(cx); let mut text = String::new(); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let mut clipboard_selections = Vec::with_capacity(selections.len()); { let buffer = self.buffer.read(cx); @@ -1442,7 +1443,7 @@ impl Editor { } pub fn copy(&mut self, _: &Copy, cx: &mut ViewContext) { - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let buffer = self.buffer.read(cx); let max_point = buffer.max_point(); let mut text = String::new(); @@ -1474,7 +1475,7 @@ impl Editor { if let Some(item) = cx.as_mut().read_from_clipboard() { let clipboard_text = item.text(); if let Some(mut clipboard_selections) = item.metadata::>() { - let mut selections = self.offset_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let all_selections_were_entire_line = clipboard_selections.iter().all(|s| s.is_entire_line); if clipboard_selections.len() != selections.len() { @@ -1537,7 +1538,7 @@ impl Editor { pub fn move_left(&mut self, _: &MoveLeft, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let start = selection.start.to_display_point(&display_map, Bias::Left); let end = selection.end.to_display_point(&display_map, Bias::Left); @@ -1559,7 +1560,7 @@ impl Editor { pub fn select_left(&mut self, _: &SelectLeft, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let cursor = movement::left(&display_map, head) @@ -1573,7 +1574,7 @@ impl Editor { pub fn move_right(&mut self, _: &MoveRight, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let start = selection.start.to_display_point(&display_map, Bias::Left); let end = selection.end.to_display_point(&display_map, Bias::Left); @@ -1595,7 +1596,7 @@ impl Editor { pub fn select_right(&mut self, _: &SelectRight, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let cursor = movement::right(&display_map, head) @@ -1614,7 +1615,7 @@ impl Editor { } let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let start = selection.start.to_display_point(&display_map, Bias::Left); let end = selection.end.to_display_point(&display_map, Bias::Left); @@ -1634,7 +1635,7 @@ impl Editor { pub fn select_up(&mut self, _: &SelectUp, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let (head, goal) = movement::up(&display_map, head, selection.goal).unwrap(); @@ -1652,7 +1653,7 @@ impl Editor { } let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let start = selection.start.to_display_point(&display_map, Bias::Left); let end = selection.end.to_display_point(&display_map, Bias::Left); @@ -1672,7 +1673,7 @@ impl Editor { pub fn select_down(&mut self, _: &SelectDown, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let (head, goal) = movement::down(&display_map, head, selection.goal).unwrap(); @@ -1689,7 +1690,7 @@ impl Editor { cx: &mut ViewContext, ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let new_head = movement::prev_word_boundary(&display_map, head).unwrap(); @@ -1708,7 +1709,7 @@ impl Editor { cx: &mut ViewContext, ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let new_head = movement::prev_word_boundary(&display_map, head).unwrap(); @@ -1726,7 +1727,7 @@ impl Editor { ) { self.start_transaction(cx); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { if selection.is_empty() { let head = selection.head().to_display_point(&display_map, Bias::Left); @@ -1747,7 +1748,7 @@ impl Editor { cx: &mut ViewContext, ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let new_head = movement::next_word_boundary(&display_map, head).unwrap(); @@ -1766,7 +1767,7 @@ impl Editor { cx: &mut ViewContext, ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let new_head = movement::next_word_boundary(&display_map, head).unwrap(); @@ -1784,7 +1785,7 @@ impl Editor { ) { self.start_transaction(cx); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { if selection.is_empty() { let head = selection.head().to_display_point(&display_map, Bias::Left); @@ -1805,7 +1806,7 @@ impl Editor { cx: &mut ViewContext, ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let new_head = movement::line_beginning(&display_map, head, true).unwrap(); @@ -1824,7 +1825,7 @@ impl Editor { cx: &mut ViewContext, ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let new_head = movement::line_beginning(&display_map, head, *toggle_indent).unwrap(); @@ -1847,7 +1848,7 @@ impl Editor { pub fn move_to_end_of_line(&mut self, _: &MoveToEndOfLine, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); { for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); @@ -1864,7 +1865,7 @@ impl Editor { pub fn select_to_end_of_line(&mut self, _: &SelectToEndOfLine, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); for selection in &mut selections { let head = selection.head().to_display_point(&display_map, Bias::Left); let new_head = movement::line_end(&display_map, head).unwrap(); @@ -1900,7 +1901,7 @@ impl Editor { } pub fn select_to_beginning(&mut self, _: &SelectToBeginning, cx: &mut ViewContext) { - let mut selection = self.point_selections(cx).last().unwrap().clone(); + let mut selection = self.selections::(cx).last().unwrap().clone(); selection.set_head(Point::zero()); self.update_selections(vec![selection], true, cx); } @@ -1919,7 +1920,7 @@ impl Editor { } pub fn select_to_end(&mut self, _: &SelectToEnd, cx: &mut ViewContext) { - let mut selection = self.offset_selections(cx).last().unwrap().clone(); + let mut selection = self.selections::(cx).last().unwrap().clone(); selection.set_head(self.buffer.read(cx).len()); self.update_selections(vec![selection], true, cx); } @@ -1937,7 +1938,7 @@ impl Editor { pub fn select_line(&mut self, _: &SelectLine, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let buffer = self.buffer.read(cx); let max_point = buffer.max_point(); for selection in &mut selections { @@ -1954,7 +1955,7 @@ impl Editor { _: &SplitSelectionIntoLines, cx: &mut ViewContext, ) { - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let buffer = self.buffer.read(cx); let mut to_unfold = Vec::new(); @@ -2002,7 +2003,7 @@ impl Editor { fn add_selection(&mut self, above: bool, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); - let mut selections = self.point_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let mut state = self.add_selections_state.take().unwrap_or_else(|| { let oldest_selection = selections.iter().min_by_key(|s| s.id).unwrap().clone(); let range = oldest_selection.display_range(&display_map).sorted(); @@ -2098,7 +2099,7 @@ impl Editor { _: &SelectLargerSyntaxNode, cx: &mut ViewContext, ) { - let old_selections = self.offset_selections(cx).collect::>(); + let old_selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = self.buffer.read(cx); @@ -2156,7 +2157,7 @@ impl Editor { _: &MoveToEnclosingBracket, cx: &mut ViewContext, ) { - let mut selections = self.offset_selections(cx).collect::>(); + let mut selections = self.selections::(cx).collect::>(); let buffer = self.buffer.read(cx.as_ref()); for selection in &mut selections { if let Some((open_range, close_range)) = @@ -2227,7 +2228,7 @@ impl Editor { let last_selection = buffer .selection_set(self.selection_set_id) .unwrap() - .point_selections(buffer) + .selections::(buffer) .max_by_key(|s| s.id) .unwrap(); last_selection @@ -2245,7 +2246,7 @@ impl Editor { let selections = buffer .selection_set(set_id) .unwrap() - .point_selections(buffer) + .selections::(buffer) .collect::>(); let start = range.start.to_buffer_point(&display_map, Bias::Left); let start_index = self.selection_insertion_index(&selections, start); @@ -2282,28 +2283,19 @@ impl Editor { } } - fn point_selections<'a>( + fn selections<'a, D>( &mut self, cx: &'a mut ViewContext, - ) -> impl 'a + Iterator> { + ) -> impl 'a + Iterator> + where + D: 'a + TextDimension<'a>, + { self.end_selection(cx); let buffer = self.buffer.read(cx); buffer .selection_set(self.selection_set_id) .unwrap() - .point_selections(buffer) - } - - fn offset_selections<'a>( - &mut self, - cx: &'a mut ViewContext, - ) -> impl 'a + Iterator> { - self.end_selection(cx); - let buffer = self.buffer.read(cx); - buffer - .selection_set(self.selection_set_id) - .unwrap() - .offset_selections(buffer) + .selections::(buffer) } fn selection_count(&mut self, cx: &mut ViewContext) -> usize { @@ -2344,7 +2336,7 @@ impl Editor { if selections.len() == autoclose_pair_state.ranges.len() { selections .iter() - .zip(autoclose_pair_state.ranges.point_ranges(buffer)) + .zip(autoclose_pair_state.ranges.ranges::(buffer)) .all(|(selection, autoclose_range)| { let head = selection.head().to_point(&*buffer); autoclose_range.start <= head && autoclose_range.end >= head @@ -2404,7 +2396,7 @@ impl Editor { pub fn fold(&mut self, _: &Fold, cx: &mut ViewContext) { let mut fold_ranges = Vec::new(); - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); for selection in selections { let range = selection.display_range(&display_map).sorted(); @@ -2427,7 +2419,7 @@ impl Editor { } pub fn unfold(&mut self, _: &Unfold, cx: &mut ViewContext) { - let selections = self.point_selections(cx).collect::>(); + let selections = self.selections::(cx).collect::>(); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = self.buffer.read(cx); let ranges = selections @@ -2488,7 +2480,7 @@ impl Editor { } pub fn fold_selected_ranges(&mut self, _: &FoldSelectedRanges, cx: &mut ViewContext) { - let selections = self.point_selections(cx); + let selections = self.selections::(cx); let ranges = selections.map(|s| s.start..s.end).collect(); self.fold_ranges(ranges, cx); } diff --git a/crates/language/src/lib.rs b/crates/language/src/lib.rs index 893dc164a6..fb259a704d 100644 --- a/crates/language/src/lib.rs +++ b/crates/language/src/lib.rs @@ -893,7 +893,7 @@ impl Buffer { if let Some(inserted) = request.inserted.as_ref() { let inserted_row_ranges = contiguous_ranges( inserted - .point_ranges(&snapshot) + .ranges::(&snapshot) .flat_map(|range| range.start.row..range.end.row + 1), max_rows_between_yields, ); @@ -941,7 +941,7 @@ impl Buffer { for selection_set_id in &selection_set_ids { if let Ok(set) = self.selection_set(*selection_set_id) { let new_selections = set - .point_selections(&*self) + .selections::(&*self) .map(|selection| { if selection.start.column == 0 { let delta = Point::new( diff --git a/crates/language/src/tests.rs b/crates/language/src/tests.rs index 0bc81b0808..b8dfbdfd50 100644 --- a/crates/language/src/tests.rs +++ b/crates/language/src/tests.rs @@ -311,7 +311,7 @@ fn test_autoindent_moves_selections(cx: &mut MutableAppContext) { let selection_ranges = buffer .selection_set(selection_set_id) .unwrap() - .point_selections(&buffer) + .selections::(&buffer) .map(|selection| selection.point_range(&buffer)) .collect::>(); diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index 13f33bfdb1..d7336b7823 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -3538,7 +3538,7 @@ mod tests { let cursor_positions = buffer .selection_set(selection_set_id) .unwrap() - .point_selections(&*buffer) + .selections::(&*buffer) .map(|selection| { assert_eq!(selection.start, selection.end); selection.start