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