Make resolving selections generic

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-11-03 17:51:57 +01:00
parent a26b066788
commit 9dc3c74260
8 changed files with 113 additions and 133 deletions

View file

@ -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<T> AnchorRangeMap<T> {
Self { version, entries }
}
pub fn ranges<'a, D>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = (Range<D>, &'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<Item = (Range<FullOffset>, &T)> {
self.entries
.iter()
.map(|(range, value)| (range.start.0..range.end.0, value))
}
pub fn point_ranges<'a>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = (Range<Point>, &'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<Content<'a>> + 'a,
) -> impl Iterator<Item = (Range<usize>, &'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<T: PartialEq> PartialEq for AnchorRangeMap<T> {
@ -248,18 +241,12 @@ impl AnchorRangeSet {
self.0.version()
}
pub fn offset_ranges<'a>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = Range<usize>> + 'a {
self.0.offset_ranges(content).map(|(range, _)| range)
}
pub fn point_ranges<'a>(
&'a self,
content: impl Into<Content<'a>> + 'a,
) -> impl Iterator<Item = Range<Point>> + 'a {
self.0.point_ranges(content).map(|(range, _)| range)
pub fn ranges<'a, D, C>(&'a self, content: C) -> impl 'a + Iterator<Item = Range<Point>>
where
D: 'a + TextDimension<'a>,
C: 'a + Into<Content<'a>>,
{
self.0.ranges(content).map(|(range, _)| range)
}
}

View file

@ -1486,10 +1486,14 @@ impl Buffer {
Ok(selections)
}
pub fn selection_ranges<'a>(&'a self, set_id: SelectionSetId) -> Result<Vec<Range<usize>>> {
#[cfg(test)]
pub fn selection_ranges<'a, D>(&'a self, set_id: SelectionSetId) -> Result<Vec<Range<D>>>
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<Item = (SelectionSetId, Vec<Range<usize>>)> {
) -> impl 'a + Iterator<Item = (SelectionSetId, Vec<Range<usize>>)>
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<T>(
fn summaries_for_anchor_ranges<D, T>(
&self,
map: &'a AnchorRangeMap<T>,
) -> impl Iterator<Item = (Range<TextSummary>, &'a T)> {
) -> impl Iterator<Item = (Range<D>, &'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::<TextSummary>(cursor.start().1 + overshoot);
summary.add_assign(&rope_cursor.summary::<D>(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::<TextSummary>(cursor.start().1 + overshoot);
summary.add_assign(&rope_cursor.summary::<D>(cursor.start().1 + overshoot));
let end_summary = summary.clone();
(start_summary..end_summary, value)

View file

@ -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<Content<'a>> + 'a,
) -> impl 'a + Iterator<Item = Selection<usize>> {
pub fn selections<'a, D, C>(&'a self, content: C) -> impl 'a + Iterator<Item = Selection<D>>
where
D: 'a + TextDimension<'a>,
C: 'a + Into<Content<'a>>,
{
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<Content<'a>> + 'a,
) -> impl 'a + Iterator<Item = Selection<Point>> {
self.selections
.point_ranges(content)
.ranges(content)
.map(|(range, state)| Selection {
id: state.id,
start: range.start,

View file

@ -576,9 +576,11 @@ fn test_random_concurrent_edits(mut rng: StdRng) {
first_buffer.selection_sets().collect::<HashMap<_, _>>()
);
assert_eq!(
buffer.all_selection_ranges().collect::<HashMap<_, _>>(),
buffer
.all_selection_ranges::<usize>()
.collect::<HashMap<_, _>>(),
first_buffer
.all_selection_ranges()
.all_selection_ranges::<usize>()
.collect::<HashMap<_, _>>()
);
}

View file

@ -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::<Point>(cx).peekable();
let first_cursor_top = selections
.peek()
.unwrap()
@ -564,7 +565,7 @@ impl Editor {
cx: &mut ViewContext<Self>,
) -> bool {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let selections = self.point_selections(cx);
let selections = self.selections::<Point>(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<Self>) {
if let Some(selection) = self.pending_selection.take() {
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
if let Some(pending_selection) = self.pending_selection.take() {
if self.point_selections(cx).next().is_none() {
if self.selections::<Point>(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::<Point>(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::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
self.start_transaction(cx);
let old_selections = self.offset_selections(cx).collect::<SmallVec<[_; 32]>>();
let old_selections = self.selections::<usize>(cx).collect::<SmallVec<[_; 32]>>();
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<Self>) {
let selections = self.offset_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<usize>(cx).collect::<Vec<_>>();
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<Self>) -> bool {
let old_selection_count = self.selection_count(cx);
let old_selections = self.offset_selections(cx).collect::<Vec<_>>();
let old_selections = self.selections::<usize>(cx).collect::<Vec<_>>();
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::<usize, _>(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>) {
self.start_transaction(cx);
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
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::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
self.start_transaction(cx);
let tab_size = self.build_settings.borrow()(cx).tab_size;
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
self.start_transaction(cx);
let selections = self.point_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
self.start_transaction(cx);
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
self.start_transaction(cx);
let selections = self.point_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
self.start_transaction(cx);
let selections = self.point_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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>) {
self.start_transaction(cx);
let mut text = String::new();
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let selections = self.point_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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::<Vec<ClipboardSelection>>() {
let mut selections = self.offset_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<usize>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
{
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let mut selection = self.point_selections(cx).last().unwrap().clone();
let mut selection = self.selections::<Point>(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<Self>) {
let mut selection = self.offset_selections(cx).last().unwrap().clone();
let mut selection = self.selections::<usize>(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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let selections = self.point_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.point_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>,
) {
let old_selections = self.offset_selections(cx).collect::<Box<_>>();
let old_selections = self.selections::<usize>(cx).collect::<Box<_>>();
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<Self>,
) {
let mut selections = self.offset_selections(cx).collect::<Vec<_>>();
let mut selections = self.selections::<usize>(cx).collect::<Vec<_>>();
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::<Point, _>(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::<Point, _>(buffer)
.collect::<Vec<_>>();
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<Self>,
) -> impl 'a + Iterator<Item = Selection<Point>> {
) -> impl 'a + Iterator<Item = Selection<D>>
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<Self>,
) -> impl 'a + Iterator<Item = Selection<usize>> {
self.end_selection(cx);
let buffer = self.buffer.read(cx);
buffer
.selection_set(self.selection_set_id)
.unwrap()
.offset_selections(buffer)
.selections::<D, _>(buffer)
}
fn selection_count(&mut self, cx: &mut ViewContext<Self>) -> 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::<Point, _>(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<Self>) {
let mut fold_ranges = Vec::new();
let selections = self.point_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let selections = self.point_selections(cx).collect::<Vec<_>>();
let selections = self.selections::<Point>(cx).collect::<Vec<_>>();
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<Self>) {
let selections = self.point_selections(cx);
let selections = self.selections::<Point>(cx);
let ranges = selections.map(|s| s.start..s.end).collect();
self.fold_ranges(ranges, cx);
}

View file

@ -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::<Point, _>(&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::<Point, _>(&*self)
.map(|selection| {
if selection.start.column == 0 {
let delta = Point::new(

View file

@ -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::<Point, _>(&buffer)
.map(|selection| selection.point_range(&buffer))
.collect::<Vec<_>>();

View file

@ -3538,7 +3538,7 @@ mod tests {
let cursor_positions = buffer
.selection_set(selection_set_id)
.unwrap()
.point_selections(&*buffer)
.selections::<Point, _>(&*buffer)
.map(|selection| {
assert_eq!(selection.start, selection.end);
selection.start