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 primary: DiagnosticEntry<T>,
24 pub supporting: Vec<DiagnosticEntry<T>>,
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<O>(&self, buffer: &text::BufferSnapshot) -> Vec<DiagnosticGroup<O>>
108 where
109 O: FromAnchor + Ord + Copy,
110 {
111 let mut groups =
112 HashMap::<usize, (Option<DiagnosticEntry<O>>, Vec<DiagnosticEntry<O>>)>::default();
113
114 for entry in self.diagnostics.iter() {
115 let entry = entry.resolve(buffer);
116 let (ref mut primary, ref mut supporting) = groups
117 .entry(entry.diagnostic.group_id)
118 .or_insert((None, Vec::new()));
119 if entry.diagnostic.is_primary {
120 *primary = Some(entry);
121 } else {
122 supporting.push(entry);
123 }
124 }
125
126 let mut groups = groups
127 .into_values()
128 .map(|(primary, mut supporting)| {
129 supporting.sort_unstable_by_key(|entry| entry.range.start);
130 DiagnosticGroup {
131 primary: primary.unwrap(),
132 supporting,
133 }
134 })
135 .collect::<Vec<_>>();
136 groups.sort_unstable_by_key(|group| group.primary.range.start);
137
138 groups
139 }
140
141 pub fn group<'a, O: FromAnchor>(
142 &'a self,
143 group_id: usize,
144 buffer: &'a text::BufferSnapshot,
145 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>> {
146 self.iter()
147 .filter(move |entry| entry.diagnostic.group_id == group_id)
148 .map(|entry| entry.resolve(buffer))
149 }
150}
151
152impl sum_tree::Item for DiagnosticEntry<Anchor> {
153 type Summary = Summary;
154
155 fn summary(&self) -> Self::Summary {
156 Summary {
157 start: self.range.start.clone(),
158 end: self.range.end.clone(),
159 min_start: self.range.start.clone(),
160 max_end: self.range.end.clone(),
161 count: 1,
162 }
163 }
164}
165
166impl DiagnosticEntry<Anchor> {
167 pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
168 DiagnosticEntry {
169 range: O::from_anchor(&self.range.start, buffer)
170 ..O::from_anchor(&self.range.end, buffer),
171 diagnostic: self.diagnostic.clone(),
172 }
173 }
174}
175
176impl Default for Summary {
177 fn default() -> Self {
178 Self {
179 start: Anchor::min(),
180 end: Anchor::max(),
181 min_start: Anchor::max(),
182 max_end: Anchor::min(),
183 count: 0,
184 }
185 }
186}
187
188impl sum_tree::Summary for Summary {
189 type Context = text::BufferSnapshot;
190
191 fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
192 if other
193 .min_start
194 .cmp(&self.min_start, buffer)
195 .unwrap()
196 .is_lt()
197 {
198 self.min_start = other.min_start.clone();
199 }
200 if other.max_end.cmp(&self.max_end, buffer).unwrap().is_gt() {
201 self.max_end = other.max_end.clone();
202 }
203 self.start = other.start.clone();
204 self.end = other.end.clone();
205 self.count += other.count;
206 }
207}