1use super::{anchor::AnchorRangeMap, MultiBufferSnapshot, ToOffset};
2use std::{ops::Range, sync::Arc};
3use sum_tree::Bias;
4use text::{rope::TextDimension, Selection, SelectionSetId, SelectionState};
5
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct SelectionSet {
8 pub id: SelectionSetId,
9 pub active: bool,
10 pub selections: Arc<AnchorRangeMap<SelectionState>>,
11}
12
13impl SelectionSet {
14 pub fn len(&self) -> usize {
15 self.selections.len()
16 }
17
18 pub fn selections<'a, D>(
19 &'a self,
20 content: &'a MultiBufferSnapshot,
21 ) -> impl 'a + Iterator<Item = Selection<D>>
22 where
23 D: TextDimension,
24 {
25 self.selections
26 .ranges(content)
27 .map(|(range, state)| Selection {
28 id: state.id,
29 start: range.start,
30 end: range.end,
31 reversed: state.reversed,
32 goal: state.goal,
33 })
34 }
35
36 pub fn intersecting_selections<'a, D, I>(
37 &'a self,
38 range: Range<(I, Bias)>,
39 content: &'a MultiBufferSnapshot,
40 ) -> impl 'a + Iterator<Item = Selection<D>>
41 where
42 D: TextDimension,
43 I: 'a + ToOffset,
44 {
45 self.selections
46 .intersecting_ranges(range, content)
47 .map(|(range, state)| Selection {
48 id: state.id,
49 start: range.start,
50 end: range.end,
51 reversed: state.reversed,
52 goal: state.goal,
53 })
54 }
55
56 pub fn oldest_selection<'a, D>(
57 &'a self,
58 content: &'a MultiBufferSnapshot,
59 ) -> Option<Selection<D>>
60 where
61 D: TextDimension,
62 {
63 self.selections
64 .min_by_key(content, |selection| selection.id)
65 .map(|(range, state)| Selection {
66 id: state.id,
67 start: range.start,
68 end: range.end,
69 reversed: state.reversed,
70 goal: state.goal,
71 })
72 }
73
74 pub fn newest_selection<'a, D>(
75 &'a self,
76 content: &'a MultiBufferSnapshot,
77 ) -> Option<Selection<D>>
78 where
79 D: TextDimension,
80 {
81 self.selections
82 .max_by_key(content, |selection| selection.id)
83 .map(|(range, state)| Selection {
84 id: state.id,
85 start: range.start,
86 end: range.end,
87 reversed: state.reversed,
88 goal: state.goal,
89 })
90 }
91}