1use std::{
2 cell::Ref,
3 cmp, iter, mem,
4 ops::{Deref, Range, Sub},
5 sync::Arc,
6};
7
8use collections::HashMap;
9use gpui::{AppContext, ModelHandle, MutableAppContext};
10use itertools::Itertools;
11use language::{Bias, Point, Selection, SelectionGoal, TextDimension, ToPoint};
12use util::post_inc;
13
14use crate::{
15 display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
16 Anchor, DisplayPoint, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode, ToOffset,
17};
18
19#[derive(Clone)]
20pub struct PendingSelection {
21 pub selection: Selection<Anchor>,
22 pub mode: SelectMode,
23}
24
25#[derive(Clone)]
26pub struct SelectionsCollection {
27 display_map: ModelHandle<DisplayMap>,
28 buffer: ModelHandle<MultiBuffer>,
29 pub next_selection_id: usize,
30 pub line_mode: bool,
31 disjoint: Arc<[Selection<Anchor>]>,
32 pending: Option<PendingSelection>,
33}
34
35impl SelectionsCollection {
36 pub fn new(display_map: ModelHandle<DisplayMap>, buffer: ModelHandle<MultiBuffer>) -> Self {
37 Self {
38 display_map,
39 buffer,
40 next_selection_id: 1,
41 line_mode: false,
42 disjoint: Arc::from([]),
43 pending: Some(PendingSelection {
44 selection: Selection {
45 id: 0,
46 start: Anchor::min(),
47 end: Anchor::min(),
48 reversed: false,
49 goal: SelectionGoal::None,
50 },
51 mode: SelectMode::Character,
52 }),
53 }
54 }
55
56 fn display_map(&self, cx: &mut MutableAppContext) -> DisplaySnapshot {
57 self.display_map.update(cx, |map, cx| map.snapshot(cx))
58 }
59
60 fn buffer<'a>(&self, cx: &'a AppContext) -> Ref<'a, MultiBufferSnapshot> {
61 self.buffer.read(cx).read(cx)
62 }
63
64 pub fn set_state(&mut self, other: &SelectionsCollection) {
65 self.next_selection_id = other.next_selection_id;
66 self.line_mode = other.line_mode;
67 self.disjoint = other.disjoint.clone();
68 self.pending = other.pending.clone();
69 }
70
71 pub fn count(&self) -> usize {
72 let mut count = self.disjoint.len();
73 if self.pending.is_some() {
74 count += 1;
75 }
76 count
77 }
78
79 pub fn disjoint_anchors(&self) -> Arc<[Selection<Anchor>]> {
80 self.disjoint.clone()
81 }
82
83 pub fn pending_anchor(&self) -> Option<Selection<Anchor>> {
84 self.pending
85 .as_ref()
86 .map(|pending| pending.selection.clone())
87 }
88
89 pub fn pending<D: TextDimension + Ord + Sub<D, Output = D>>(
90 &self,
91 cx: &AppContext,
92 ) -> Option<Selection<D>> {
93 self.pending_anchor()
94 .as_ref()
95 .map(|pending| pending.map(|p| p.summary::<D>(&self.buffer(cx))))
96 }
97
98 pub fn pending_mode(&self) -> Option<SelectMode> {
99 self.pending.as_ref().map(|pending| pending.mode.clone())
100 }
101
102 pub fn all<'a, D>(&self, cx: &AppContext) -> Vec<Selection<D>>
103 where
104 D: 'a + TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug,
105 {
106 let disjoint_anchors = &self.disjoint;
107 let mut disjoint =
108 resolve_multiple::<D, _>(disjoint_anchors.iter(), &self.buffer(cx)).peekable();
109
110 let mut pending_opt = self.pending::<D>(cx);
111
112 iter::from_fn(move || {
113 if let Some(pending) = pending_opt.as_mut() {
114 while let Some(next_selection) = disjoint.peek() {
115 if pending.start <= next_selection.end && pending.end >= next_selection.start {
116 let next_selection = disjoint.next().unwrap();
117 if next_selection.start < pending.start {
118 pending.start = next_selection.start;
119 }
120 if next_selection.end > pending.end {
121 pending.end = next_selection.end;
122 }
123 } else if next_selection.end < pending.start {
124 return disjoint.next();
125 } else {
126 break;
127 }
128 }
129
130 pending_opt.take()
131 } else {
132 disjoint.next()
133 }
134 })
135 .collect()
136 }
137
138 // Returns all of the selections, adjusted to take into account the selection line_mode
139 pub fn all_adjusted(&self, cx: &mut MutableAppContext) -> Vec<Selection<Point>> {
140 let mut selections = self.all::<Point>(cx);
141 if self.line_mode {
142 let map = self.display_map(cx);
143 for selection in &mut selections {
144 let new_range = map.expand_to_line(selection.range());
145 selection.start = new_range.start;
146 selection.end = new_range.end;
147 }
148 }
149 selections
150 }
151
152 pub fn all_adjusted_display(
153 &self,
154 cx: &mut MutableAppContext,
155 ) -> (DisplaySnapshot, Vec<Selection<DisplayPoint>>) {
156 if self.line_mode {
157 let selections = self.all::<Point>(cx);
158 let map = self.display_map(cx);
159 let result = selections
160 .into_iter()
161 .map(|mut selection| {
162 let new_range = map.expand_to_line(selection.range());
163 selection.start = new_range.start;
164 selection.end = new_range.end;
165 selection.map(|point| point.to_display_point(&map))
166 })
167 .collect();
168 (map, result)
169 } else {
170 self.all_display(cx)
171 }
172 }
173
174 pub fn disjoint_in_range<'a, D>(
175 &self,
176 range: Range<Anchor>,
177 cx: &AppContext,
178 ) -> Vec<Selection<D>>
179 where
180 D: 'a + TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug,
181 {
182 let buffer = self.buffer(cx);
183 let start_ix = match self
184 .disjoint
185 .binary_search_by(|probe| probe.end.cmp(&range.start, &buffer))
186 {
187 Ok(ix) | Err(ix) => ix,
188 };
189 let end_ix = match self
190 .disjoint
191 .binary_search_by(|probe| probe.start.cmp(&range.end, &buffer))
192 {
193 Ok(ix) => ix + 1,
194 Err(ix) => ix,
195 };
196 resolve_multiple(&self.disjoint[start_ix..end_ix], &buffer).collect()
197 }
198
199 pub fn all_display(
200 &self,
201 cx: &mut MutableAppContext,
202 ) -> (DisplaySnapshot, Vec<Selection<DisplayPoint>>) {
203 let display_map = self.display_map(cx);
204 let selections = self
205 .all::<Point>(cx)
206 .into_iter()
207 .map(|selection| selection.map(|point| point.to_display_point(&display_map)))
208 .collect();
209 (display_map, selections)
210 }
211
212 pub fn newest_anchor(&self) -> &Selection<Anchor> {
213 self.pending
214 .as_ref()
215 .map(|s| &s.selection)
216 .or_else(|| self.disjoint.iter().max_by_key(|s| s.id))
217 .unwrap()
218 }
219
220 pub fn newest<D: TextDimension + Ord + Sub<D, Output = D>>(
221 &self,
222 cx: &AppContext,
223 ) -> Selection<D> {
224 resolve(self.newest_anchor(), &self.buffer(cx))
225 }
226
227 pub fn newest_display(&self, cx: &mut MutableAppContext) -> Selection<DisplayPoint> {
228 let display_map = self.display_map(cx);
229 let selection = self
230 .newest_anchor()
231 .map(|point| point.to_display_point(&display_map));
232 selection
233 }
234
235 pub fn oldest_anchor(&self) -> &Selection<Anchor> {
236 self.disjoint
237 .iter()
238 .min_by_key(|s| s.id)
239 .or_else(|| self.pending.as_ref().map(|p| &p.selection))
240 .unwrap()
241 }
242
243 pub fn oldest<D: TextDimension + Ord + Sub<D, Output = D>>(
244 &self,
245 cx: &AppContext,
246 ) -> Selection<D> {
247 resolve(self.oldest_anchor(), &self.buffer(cx))
248 }
249
250 pub fn first<D: TextDimension + Ord + Sub<D, Output = D>>(
251 &self,
252 cx: &AppContext,
253 ) -> Selection<D> {
254 self.all(cx).first().unwrap().clone()
255 }
256
257 pub fn last<D: TextDimension + Ord + Sub<D, Output = D>>(
258 &self,
259 cx: &AppContext,
260 ) -> Selection<D> {
261 self.all(cx).last().unwrap().clone()
262 }
263
264 #[cfg(any(test, feature = "test-support"))]
265 pub fn ranges<D: TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug>(
266 &self,
267 cx: &AppContext,
268 ) -> Vec<Range<D>> {
269 self.all::<D>(cx)
270 .iter()
271 .map(|s| {
272 if s.reversed {
273 s.end.clone()..s.start.clone()
274 } else {
275 s.start.clone()..s.end.clone()
276 }
277 })
278 .collect()
279 }
280
281 #[cfg(any(test, feature = "test-support"))]
282 pub fn display_ranges(&self, cx: &mut MutableAppContext) -> Vec<Range<DisplayPoint>> {
283 let display_map = self.display_map(cx);
284 self.disjoint_anchors()
285 .iter()
286 .chain(self.pending_anchor().as_ref())
287 .map(|s| {
288 if s.reversed {
289 s.end.to_display_point(&display_map)..s.start.to_display_point(&display_map)
290 } else {
291 s.start.to_display_point(&display_map)..s.end.to_display_point(&display_map)
292 }
293 })
294 .collect()
295 }
296
297 pub fn build_columnar_selection(
298 &mut self,
299 display_map: &DisplaySnapshot,
300 row: u32,
301 columns: &Range<u32>,
302 reversed: bool,
303 ) -> Option<Selection<Point>> {
304 let is_empty = columns.start == columns.end;
305 let line_len = display_map.line_len(row);
306 if columns.start < line_len || (is_empty && columns.start == line_len) {
307 let start = DisplayPoint::new(row, columns.start);
308 let end = DisplayPoint::new(row, cmp::min(columns.end, line_len));
309
310 Some(Selection {
311 id: post_inc(&mut self.next_selection_id),
312 start: start.to_point(display_map),
313 end: end.to_point(display_map),
314 reversed,
315 goal: SelectionGoal::ColumnRange {
316 start: columns.start,
317 end: columns.end,
318 },
319 })
320 } else {
321 None
322 }
323 }
324
325 pub(crate) fn change_with<R>(
326 &mut self,
327 cx: &mut MutableAppContext,
328 change: impl FnOnce(&mut MutableSelectionsCollection) -> R,
329 ) -> (bool, R) {
330 let mut mutable_collection = MutableSelectionsCollection {
331 collection: self,
332 selections_changed: false,
333 cx,
334 };
335
336 let result = change(&mut mutable_collection);
337 assert!(
338 !mutable_collection.disjoint.is_empty() || mutable_collection.pending.is_some(),
339 "There must be at least one selection"
340 );
341 (mutable_collection.selections_changed, result)
342 }
343}
344
345pub struct MutableSelectionsCollection<'a> {
346 collection: &'a mut SelectionsCollection,
347 selections_changed: bool,
348 cx: &'a mut MutableAppContext,
349}
350
351impl<'a> MutableSelectionsCollection<'a> {
352 fn display_map(&mut self) -> DisplaySnapshot {
353 self.collection.display_map(self.cx)
354 }
355
356 fn buffer(&self) -> Ref<MultiBufferSnapshot> {
357 self.collection.buffer(self.cx)
358 }
359
360 pub fn clear_disjoint(&mut self) {
361 self.collection.disjoint = Arc::from([]);
362 }
363
364 pub fn delete(&mut self, selection_id: usize) {
365 let mut changed = false;
366 self.collection.disjoint = self
367 .disjoint
368 .iter()
369 .filter(|selection| {
370 let found = selection.id == selection_id;
371 changed |= found;
372 !found
373 })
374 .cloned()
375 .collect();
376
377 self.selections_changed |= changed;
378 }
379
380 pub fn clear_pending(&mut self) {
381 if self.collection.pending.is_some() {
382 self.collection.pending = None;
383 self.selections_changed = true;
384 }
385 }
386
387 pub fn set_pending_anchor_range(&mut self, range: Range<Anchor>, mode: SelectMode) {
388 self.collection.pending = Some(PendingSelection {
389 selection: Selection {
390 id: post_inc(&mut self.collection.next_selection_id),
391 start: range.start,
392 end: range.end,
393 reversed: false,
394 goal: SelectionGoal::None,
395 },
396 mode,
397 });
398 self.selections_changed = true;
399 }
400
401 pub fn set_pending_display_range(&mut self, range: Range<DisplayPoint>, mode: SelectMode) {
402 let (start, end, reversed) = {
403 let display_map = self.display_map();
404 let buffer = self.buffer();
405 let mut start = range.start;
406 let mut end = range.end;
407 let reversed = if start > end {
408 mem::swap(&mut start, &mut end);
409 true
410 } else {
411 false
412 };
413
414 let end_bias = if end > start { Bias::Left } else { Bias::Right };
415 (
416 buffer.anchor_before(start.to_point(&display_map)),
417 buffer.anchor_at(end.to_point(&display_map), end_bias),
418 reversed,
419 )
420 };
421
422 let new_pending = PendingSelection {
423 selection: Selection {
424 id: post_inc(&mut self.collection.next_selection_id),
425 start,
426 end,
427 reversed,
428 goal: SelectionGoal::None,
429 },
430 mode,
431 };
432
433 self.collection.pending = Some(new_pending);
434 self.selections_changed = true;
435 }
436
437 pub fn set_pending(&mut self, selection: Selection<Anchor>, mode: SelectMode) {
438 self.collection.pending = Some(PendingSelection { selection, mode });
439 self.selections_changed = true;
440 }
441
442 pub fn try_cancel(&mut self) -> bool {
443 if let Some(pending) = self.collection.pending.take() {
444 if self.disjoint.is_empty() {
445 self.collection.disjoint = Arc::from([pending.selection]);
446 }
447 self.selections_changed = true;
448 return true;
449 }
450
451 let mut oldest = self.oldest_anchor().clone();
452 if self.count() > 1 {
453 self.collection.disjoint = Arc::from([oldest]);
454 self.selections_changed = true;
455 return true;
456 }
457
458 if !oldest.start.cmp(&oldest.end, &self.buffer()).is_eq() {
459 let head = oldest.head();
460 oldest.start = head.clone();
461 oldest.end = head;
462 self.collection.disjoint = Arc::from([oldest]);
463 self.selections_changed = true;
464 return true;
465 }
466
467 false
468 }
469
470 pub fn insert_range<T>(&mut self, range: Range<T>)
471 where
472 T: 'a + ToOffset + ToPoint + TextDimension + Ord + Sub<T, Output = T> + std::marker::Copy,
473 {
474 let mut selections = self.all(self.cx);
475 let mut start = range.start.to_offset(&self.buffer());
476 let mut end = range.end.to_offset(&self.buffer());
477 let reversed = if start > end {
478 mem::swap(&mut start, &mut end);
479 true
480 } else {
481 false
482 };
483 selections.push(Selection {
484 id: post_inc(&mut self.collection.next_selection_id),
485 start,
486 end,
487 reversed,
488 goal: SelectionGoal::None,
489 });
490 self.select(selections);
491 }
492
493 pub fn select<T>(&mut self, mut selections: Vec<Selection<T>>)
494 where
495 T: ToOffset + ToPoint + Ord + std::marker::Copy + std::fmt::Debug,
496 {
497 let buffer = self.buffer.read(self.cx).snapshot(self.cx);
498 selections.sort_unstable_by_key(|s| s.start);
499 // Merge overlapping selections.
500 let mut i = 1;
501 while i < selections.len() {
502 if selections[i - 1].end >= selections[i].start {
503 let removed = selections.remove(i);
504 if removed.start < selections[i - 1].start {
505 selections[i - 1].start = removed.start;
506 }
507 if removed.end > selections[i - 1].end {
508 selections[i - 1].end = removed.end;
509 }
510 } else {
511 i += 1;
512 }
513 }
514
515 self.collection.disjoint = Arc::from_iter(selections.into_iter().map(|selection| {
516 let end_bias = if selection.end > selection.start {
517 Bias::Left
518 } else {
519 Bias::Right
520 };
521 Selection {
522 id: selection.id,
523 start: buffer.anchor_after(selection.start),
524 end: buffer.anchor_at(selection.end, end_bias),
525 reversed: selection.reversed,
526 goal: selection.goal,
527 }
528 }));
529
530 self.collection.pending = None;
531 self.selections_changed = true;
532 }
533
534 pub fn select_anchors(&mut self, selections: Vec<Selection<Anchor>>) {
535 let buffer = self.buffer.read(self.cx).snapshot(self.cx);
536 let resolved_selections =
537 resolve_multiple::<usize, _>(&selections, &buffer).collect::<Vec<_>>();
538 self.select(resolved_selections);
539 }
540
541 pub fn select_ranges<I, T>(&mut self, ranges: I)
542 where
543 I: IntoIterator<Item = Range<T>>,
544 T: ToOffset,
545 {
546 let buffer = self.buffer.read(self.cx).snapshot(self.cx);
547 let selections = ranges
548 .into_iter()
549 .map(|range| {
550 let mut start = range.start.to_offset(&buffer);
551 let mut end = range.end.to_offset(&buffer);
552 let reversed = if start > end {
553 mem::swap(&mut start, &mut end);
554 true
555 } else {
556 false
557 };
558 Selection {
559 id: post_inc(&mut self.collection.next_selection_id),
560 start,
561 end,
562 reversed,
563 goal: SelectionGoal::None,
564 }
565 })
566 .collect::<Vec<_>>();
567
568 self.select(selections)
569 }
570
571 pub fn select_anchor_ranges<I: IntoIterator<Item = Range<Anchor>>>(&mut self, ranges: I) {
572 let buffer = self.buffer.read(self.cx).snapshot(self.cx);
573 let selections = ranges
574 .into_iter()
575 .map(|range| {
576 let mut start = range.start;
577 let mut end = range.end;
578 let reversed = if start.cmp(&end, &buffer).is_gt() {
579 mem::swap(&mut start, &mut end);
580 true
581 } else {
582 false
583 };
584 Selection {
585 id: post_inc(&mut self.collection.next_selection_id),
586 start,
587 end,
588 reversed,
589 goal: SelectionGoal::None,
590 }
591 })
592 .collect::<Vec<_>>();
593
594 self.select_anchors(selections)
595 }
596
597 #[cfg(any(test, feature = "test-support"))]
598 pub fn select_display_ranges<T>(&mut self, ranges: T)
599 where
600 T: IntoIterator<Item = Range<DisplayPoint>>,
601 {
602 let display_map = self.display_map();
603 let selections = ranges
604 .into_iter()
605 .map(|range| {
606 let mut start = range.start;
607 let mut end = range.end;
608 let reversed = if start > end {
609 mem::swap(&mut start, &mut end);
610 true
611 } else {
612 false
613 };
614 Selection {
615 id: post_inc(&mut self.collection.next_selection_id),
616 start: start.to_point(&display_map),
617 end: end.to_point(&display_map),
618 reversed,
619 goal: SelectionGoal::None,
620 }
621 })
622 .collect();
623 self.select(selections);
624 }
625
626 pub fn move_with(
627 &mut self,
628 mut move_selection: impl FnMut(&DisplaySnapshot, &mut Selection<DisplayPoint>),
629 ) {
630 let mut changed = false;
631 let display_map = self.display_map();
632 let selections = self
633 .all::<Point>(self.cx)
634 .into_iter()
635 .map(|selection| {
636 let mut moved_selection =
637 selection.map(|point| point.to_display_point(&display_map));
638 move_selection(&display_map, &mut moved_selection);
639 let moved_selection =
640 moved_selection.map(|display_point| display_point.to_point(&display_map));
641 if selection != moved_selection {
642 changed = true;
643 }
644 moved_selection
645 })
646 .collect();
647
648 if changed {
649 self.select(selections)
650 }
651 }
652
653 pub fn move_heads_with(
654 &mut self,
655 mut update_head: impl FnMut(
656 &DisplaySnapshot,
657 DisplayPoint,
658 SelectionGoal,
659 ) -> (DisplayPoint, SelectionGoal),
660 ) {
661 self.move_with(|map, selection| {
662 let (new_head, new_goal) = update_head(map, selection.head(), selection.goal);
663 selection.set_head(new_head, new_goal);
664 });
665 }
666
667 pub fn move_cursors_with(
668 &mut self,
669 mut update_cursor_position: impl FnMut(
670 &DisplaySnapshot,
671 DisplayPoint,
672 SelectionGoal,
673 ) -> (DisplayPoint, SelectionGoal),
674 ) {
675 self.move_with(|map, selection| {
676 let (cursor, new_goal) = update_cursor_position(map, selection.head(), selection.goal);
677 selection.collapse_to(cursor, new_goal)
678 });
679 }
680
681 pub fn replace_cursors_with(
682 &mut self,
683 mut find_replacement_cursors: impl FnMut(&DisplaySnapshot) -> Vec<DisplayPoint>,
684 ) {
685 let display_map = self.display_map();
686 let new_selections = find_replacement_cursors(&display_map)
687 .into_iter()
688 .map(|cursor| {
689 let cursor_point = cursor.to_point(&display_map);
690 Selection {
691 id: post_inc(&mut self.collection.next_selection_id),
692 start: cursor_point,
693 end: cursor_point,
694 reversed: false,
695 goal: SelectionGoal::None,
696 }
697 })
698 .collect();
699 self.select(new_selections);
700 }
701
702 /// Compute new ranges for any selections that were located in excerpts that have
703 /// since been removed.
704 ///
705 /// Returns a `HashMap` indicating which selections whose former head position
706 /// was no longer present. The keys of the map are selection ids. The values are
707 /// the id of the new excerpt where the head of the selection has been moved.
708 pub fn refresh(&mut self) -> HashMap<usize, ExcerptId> {
709 let mut pending = self.collection.pending.take();
710 let mut selections_with_lost_position = HashMap::default();
711
712 let anchors_with_status = {
713 let buffer = self.buffer();
714 let disjoint_anchors = self
715 .disjoint
716 .iter()
717 .flat_map(|selection| [&selection.start, &selection.end]);
718 buffer.refresh_anchors(disjoint_anchors)
719 };
720 let adjusted_disjoint: Vec<_> = anchors_with_status
721 .chunks(2)
722 .map(|selection_anchors| {
723 let (anchor_ix, start, kept_start) = selection_anchors[0].clone();
724 let (_, end, kept_end) = selection_anchors[1].clone();
725 let selection = &self.disjoint[anchor_ix / 2];
726 let kept_head = if selection.reversed {
727 kept_start
728 } else {
729 kept_end
730 };
731 if !kept_head {
732 selections_with_lost_position.insert(selection.id, selection.head().excerpt_id);
733 }
734
735 Selection {
736 id: selection.id,
737 start,
738 end,
739 reversed: selection.reversed,
740 goal: selection.goal,
741 }
742 })
743 .collect();
744
745 if !adjusted_disjoint.is_empty() {
746 let resolved_selections =
747 resolve_multiple(adjusted_disjoint.iter(), &self.buffer()).collect();
748 self.select::<usize>(resolved_selections);
749 }
750
751 if let Some(pending) = pending.as_mut() {
752 let buffer = self.buffer();
753 let anchors =
754 buffer.refresh_anchors([&pending.selection.start, &pending.selection.end]);
755 let (_, start, kept_start) = anchors[0].clone();
756 let (_, end, kept_end) = anchors[1].clone();
757 let kept_head = if pending.selection.reversed {
758 kept_start
759 } else {
760 kept_end
761 };
762 if !kept_head {
763 selections_with_lost_position
764 .insert(pending.selection.id, pending.selection.head().excerpt_id);
765 }
766
767 pending.selection.start = start;
768 pending.selection.end = end;
769 }
770 self.collection.pending = pending;
771 self.selections_changed = true;
772
773 selections_with_lost_position
774 }
775}
776
777impl<'a> Deref for MutableSelectionsCollection<'a> {
778 type Target = SelectionsCollection;
779 fn deref(&self) -> &Self::Target {
780 self.collection
781 }
782}
783
784// Panics if passed selections are not in order
785pub fn resolve_multiple<'a, D, I>(
786 selections: I,
787 snapshot: &MultiBufferSnapshot,
788) -> impl 'a + Iterator<Item = Selection<D>>
789where
790 D: TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug,
791 I: 'a + IntoIterator<Item = &'a Selection<Anchor>>,
792{
793 let (to_summarize, selections) = selections.into_iter().tee();
794 let mut summaries = snapshot
795 .summaries_for_anchors::<D, _>(
796 to_summarize
797 .flat_map(|s| [&s.start, &s.end])
798 .collect::<Vec<_>>(),
799 )
800 .into_iter();
801 selections.map(move |s| Selection {
802 id: s.id,
803 start: summaries.next().unwrap(),
804 end: summaries.next().unwrap(),
805 reversed: s.reversed,
806 goal: s.goal,
807 })
808}
809
810fn resolve<D: TextDimension + Ord + Sub<D, Output = D>>(
811 selection: &Selection<Anchor>,
812 buffer: &MultiBufferSnapshot,
813) -> Selection<D> {
814 selection.map(|p| p.summary::<D>(buffer))
815}