1use crate::Diagnostic;
2use collections::HashMap;
3use std::{
4 cmp::{Ordering, Reverse},
5 iter,
6 ops::Range,
7};
8use sum_tree::{self, Bias, SumTree};
9use text::{Anchor, FromAnchor, PointUtf16, ToOffset};
10
11#[derive(Clone, Default)]
12pub struct DiagnosticSet {
13 diagnostics: SumTree<DiagnosticEntry<Anchor>>,
14}
15
16#[derive(Clone, Debug, PartialEq, Eq)]
17pub struct DiagnosticEntry<T> {
18 pub range: Range<T>,
19 pub diagnostic: Diagnostic,
20}
21
22pub struct DiagnosticGroup<T> {
23 pub entries: Vec<DiagnosticEntry<T>>,
24 pub primary_ix: usize,
25}
26
27#[derive(Clone, Debug)]
28pub struct Summary {
29 start: Anchor,
30 end: Anchor,
31 min_start: Anchor,
32 max_end: Anchor,
33 count: usize,
34}
35
36impl DiagnosticSet {
37 pub fn from_sorted_entries<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
38 where
39 I: IntoIterator<Item = DiagnosticEntry<Anchor>>,
40 {
41 Self {
42 diagnostics: SumTree::from_iter(iter, buffer),
43 }
44 }
45
46 pub fn new<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
47 where
48 I: IntoIterator<Item = DiagnosticEntry<PointUtf16>>,
49 {
50 let mut entries = iter.into_iter().collect::<Vec<_>>();
51 entries.sort_unstable_by_key(|entry| (entry.range.start, Reverse(entry.range.end)));
52 Self {
53 diagnostics: SumTree::from_iter(
54 entries.into_iter().map(|entry| DiagnosticEntry {
55 range: buffer.anchor_before(entry.range.start)
56 ..buffer.anchor_after(entry.range.end),
57 diagnostic: entry.diagnostic,
58 }),
59 buffer,
60 ),
61 }
62 }
63
64 pub fn iter(&self) -> impl Iterator<Item = &DiagnosticEntry<Anchor>> {
65 self.diagnostics.iter()
66 }
67
68 pub fn range<'a, T, O>(
69 &'a self,
70 range: Range<T>,
71 buffer: &'a text::BufferSnapshot,
72 inclusive: bool,
73 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
74 where
75 T: 'a + ToOffset,
76 O: FromAnchor,
77 {
78 let end_bias = if inclusive { Bias::Right } else { Bias::Left };
79 let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias);
80 let mut cursor = self.diagnostics.filter::<_, ()>(
81 {
82 move |summary: &Summary| {
83 let start_cmp = range.start.cmp(&summary.max_end, buffer).unwrap();
84 let end_cmp = range.end.cmp(&summary.min_start, buffer).unwrap();
85 if inclusive {
86 start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal
87 } else {
88 start_cmp == Ordering::Less && end_cmp == Ordering::Greater
89 }
90 }
91 },
92 buffer,
93 );
94
95 iter::from_fn({
96 move || {
97 if let Some(diagnostic) = cursor.item() {
98 cursor.next(buffer);
99 Some(diagnostic.resolve(buffer))
100 } else {
101 None
102 }
103 }
104 })
105 }
106
107 pub fn groups(&self, buffer: &text::BufferSnapshot) -> Vec<DiagnosticGroup<Anchor>> {
108 let mut groups = HashMap::default();
109 for entry in self.diagnostics.iter() {
110 groups
111 .entry(entry.diagnostic.group_id)
112 .or_insert(Vec::new())
113 .push(entry.clone());
114 }
115
116 let mut groups = groups
117 .into_values()
118 .filter_map(|mut entries| {
119 entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer).unwrap());
120 entries
121 .iter()
122 .position(|entry| entry.diagnostic.is_primary)
123 .map(|primary_ix| DiagnosticGroup {
124 entries,
125 primary_ix,
126 })
127 })
128 .collect::<Vec<_>>();
129 groups.sort_unstable_by(|a, b| {
130 a.entries[a.primary_ix]
131 .range
132 .start
133 .cmp(&b.entries[b.primary_ix].range.start, buffer)
134 .unwrap()
135 });
136 groups
137 }
138
139 pub fn group<'a, O: FromAnchor>(
140 &'a self,
141 group_id: usize,
142 buffer: &'a text::BufferSnapshot,
143 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>> {
144 self.iter()
145 .filter(move |entry| entry.diagnostic.group_id == group_id)
146 .map(|entry| entry.resolve(buffer))
147 }
148}
149
150impl sum_tree::Item for DiagnosticEntry<Anchor> {
151 type Summary = Summary;
152
153 fn summary(&self) -> Self::Summary {
154 Summary {
155 start: self.range.start.clone(),
156 end: self.range.end.clone(),
157 min_start: self.range.start.clone(),
158 max_end: self.range.end.clone(),
159 count: 1,
160 }
161 }
162}
163
164impl DiagnosticEntry<Anchor> {
165 pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
166 DiagnosticEntry {
167 range: O::from_anchor(&self.range.start, buffer)
168 ..O::from_anchor(&self.range.end, buffer),
169 diagnostic: self.diagnostic.clone(),
170 }
171 }
172}
173
174impl Default for Summary {
175 fn default() -> Self {
176 Self {
177 start: Anchor::min(),
178 end: Anchor::max(),
179 min_start: Anchor::max(),
180 max_end: Anchor::min(),
181 count: 0,
182 }
183 }
184}
185
186impl sum_tree::Summary for Summary {
187 type Context = text::BufferSnapshot;
188
189 fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
190 if other
191 .min_start
192 .cmp(&self.min_start, buffer)
193 .unwrap()
194 .is_lt()
195 {
196 self.min_start = other.min_start.clone();
197 }
198 if other.max_end.cmp(&self.max_end, buffer).unwrap().is_gt() {
199 self.max_end = other.max_end.clone();
200 }
201 self.start = other.start.clone();
202 self.end = other.end.clone();
203 self.count += other.count;
204 }
205}