Change summary
crates/editor/src/editor.rs | 71 +++++++++++++++++++-------------
crates/editor/src/multi_buffer.rs | 4
2 files changed, 45 insertions(+), 30 deletions(-)
Detailed changes
@@ -3049,35 +3049,50 @@ impl Editor {
where
D: 'a + TextDimension + Ord + Sub<D, Output = D>,
{
- // let buffer = self.buffer.read(cx).snapshot(cx);
- // let mut selections = self.selection_set(cx).selections::<D>(&buffer).peekable();
- // let mut pending_selection = self.pending_selection(cx);
-
- // iter::from_fn(move || {
- // if let Some(pending) = pending_selection.as_mut() {
- // while let Some(next_selection) = selections.peek() {
- // if pending.start <= next_selection.end && pending.end >= next_selection.start {
- // let next_selection = selections.next().unwrap();
- // if next_selection.start < pending.start {
- // pending.start = next_selection.start;
- // }
- // if next_selection.end > pending.end {
- // pending.end = next_selection.end;
- // }
- // } else if next_selection.end < pending.start {
- // return selections.next();
- // } else {
- // break;
- // }
- // }
+ let buffer = self.buffer.read(cx).snapshot(cx);
- // pending_selection.take()
- // } else {
- // selections.next()
- // }
- // })
- // .collect()
- todo!()
+ let mut summaries = buffer
+ .summaries_for_anchors::<D, _>(self.selections.iter().flat_map(|s| [&s.start, &s.end]))
+ .into_iter();
+
+ let mut selections = self
+ .selections
+ .iter()
+ .map(|s| Selection {
+ id: s.id,
+ start: summaries.next().unwrap(),
+ end: summaries.next().unwrap(),
+ reversed: s.reversed,
+ goal: s.goal,
+ })
+ .peekable();
+
+ let mut pending_selection = self.pending_selection::<D>(&buffer, cx);
+
+ iter::from_fn(move || {
+ if let Some(pending) = pending_selection.as_mut() {
+ while let Some(next_selection) = selections.peek() {
+ if pending.start <= next_selection.end && pending.end >= next_selection.start {
+ let next_selection = selections.next().unwrap();
+ if next_selection.start < pending.start {
+ pending.start = next_selection.start;
+ }
+ if next_selection.end > pending.end {
+ pending.end = next_selection.end;
+ }
+ } else if next_selection.end < pending.start {
+ return selections.next();
+ } else {
+ break;
+ }
+ }
+
+ pending_selection.take()
+ } else {
+ selections.next()
+ }
+ })
+ .collect()
}
fn pending_selection<D: TextDimension + Ord + Sub<D, Output = D>>(
@@ -777,7 +777,7 @@ impl MultiBufferSnapshot {
summary
}
- fn summary_for_anchor<D>(&self, anchor: &Anchor) -> D
+ pub fn summary_for_anchor<D>(&self, anchor: &Anchor) -> D
where
D: TextDimension + Ord + Sub<D, Output = D>,
{
@@ -798,7 +798,7 @@ impl MultiBufferSnapshot {
D::from_text_summary(&cursor.start().text)
}
- fn summaries_for_anchors<'a, D, I>(&'a self, anchors: I) -> Vec<D>
+ pub fn summaries_for_anchors<'a, D, I>(&'a self, anchors: I) -> Vec<D>
where
D: TextDimension + Ord + Sub<D, Output = D>,
I: 'a + IntoIterator<Item = &'a Anchor>,