selection.rs

 1use super::{Anchor, MultiBufferSnapshot};
 2use std::ops::Sub;
 3use text::{rope::TextDimension, Selection};
 4
 5fn resolve_selection<'a, D>(
 6    selection: &'a Selection<Anchor>,
 7    snapshot: &'a MultiBufferSnapshot,
 8) -> Selection<D>
 9where
10    D: TextDimension + Ord + Sub<D, Output = D>,
11{
12    Selection {
13        id: selection.id,
14        start: selection.start.summary::<D>(snapshot),
15        end: selection.end.summary::<D>(snapshot),
16        reversed: selection.reversed,
17        goal: selection.goal,
18    }
19}
20
21fn resolve_selections<'a, D>(
22    selections: &'a [Selection<Anchor>],
23    snapshot: &'a MultiBufferSnapshot,
24) -> impl 'a + Iterator<Item = Selection<D>>
25where
26    D: TextDimension + Ord + Sub<D, Output = D>,
27{
28    let mut summaries = snapshot
29        .summaries_for_anchors::<D, _>(
30            selections
31                .iter()
32                .flat_map(|selection| [&selection.start, &selection.end]),
33        )
34        .into_iter();
35    selections.iter().map(move |selection| Selection {
36        id: selection.id,
37        start: summaries.next().unwrap(),
38        end: summaries.next().unwrap(),
39        reversed: selection.reversed,
40        goal: selection.goal,
41    })
42}