1use crate::{range_to_lsp, Diagnostic};
2use anyhow::Result;
3use collections::HashMap;
4use lsp::LanguageServerId;
5use std::{
6 cmp::{Ordering, Reverse},
7 iter,
8 ops::Range,
9};
10use sum_tree::{self, Bias, SumTree};
11use text::{Anchor, FromAnchor, PointUtf16, ToOffset};
12
13/// A set of diagnostics associated with a given buffer, provided
14/// by a single language server.
15///
16/// The diagnostics are stored in a [`SumTree`], which allows this struct
17/// to be cheaply copied, and allows for efficient retrieval of the
18/// diagnostics that intersect a given range of the buffer.
19#[derive(Clone, Debug)]
20pub struct DiagnosticSet {
21 diagnostics: SumTree<DiagnosticEntry<Anchor>>,
22}
23
24/// A single diagnostic in a set. Generic over its range type, because
25/// the diagnostics are stored internally as [`Anchor`]s, but can be
26/// resolved to different coordinates types like [`usize`] byte offsets or
27/// [`Point`](gpui::Point)s.
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub struct DiagnosticEntry<T> {
30 /// The range of the buffer where the diagnostic applies.
31 pub range: Range<T>,
32 /// The information about the diagnostic.
33 pub diagnostic: Diagnostic,
34}
35
36/// A group of related diagnostics, ordered by their start position
37/// in the buffer.
38#[derive(Debug)]
39pub struct DiagnosticGroup<T> {
40 /// The diagnostics.
41 pub entries: Vec<DiagnosticEntry<T>>,
42 /// The index into `entries` where the primary diagnostic is stored.
43 pub primary_ix: usize,
44}
45
46#[derive(Clone, Debug)]
47pub struct Summary {
48 start: Anchor,
49 end: Anchor,
50 min_start: Anchor,
51 max_end: Anchor,
52 count: usize,
53}
54
55impl DiagnosticEntry<PointUtf16> {
56 /// Returns a raw LSP diagnostic used to provide diagnostic context to LSP
57 /// codeAction request
58 pub fn to_lsp_diagnostic_stub(&self) -> Result<lsp::Diagnostic> {
59 let code = self
60 .diagnostic
61 .code
62 .clone()
63 .map(lsp::NumberOrString::String);
64
65 let range = range_to_lsp(self.range.clone())?;
66
67 Ok(lsp::Diagnostic {
68 code,
69 range,
70 severity: Some(self.diagnostic.severity),
71 source: self.diagnostic.source.clone(),
72 message: self.diagnostic.message.clone(),
73 data: self.diagnostic.data.clone(),
74 ..Default::default()
75 })
76 }
77}
78
79impl DiagnosticSet {
80 /// Constructs a [DiagnosticSet] from a sequence of entries, ordered by
81 /// their position in the buffer.
82 pub fn from_sorted_entries<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
83 where
84 I: IntoIterator<Item = DiagnosticEntry<Anchor>>,
85 {
86 Self {
87 diagnostics: SumTree::from_iter(iter, buffer),
88 }
89 }
90
91 /// Constructs a [DiagnosticSet] from a sequence of entries in an arbitrary order.
92 pub fn new<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
93 where
94 I: IntoIterator<Item = DiagnosticEntry<PointUtf16>>,
95 {
96 let mut entries = iter.into_iter().collect::<Vec<_>>();
97 entries.sort_unstable_by_key(|entry| (entry.range.start, Reverse(entry.range.end)));
98 Self {
99 diagnostics: SumTree::from_iter(
100 entries.into_iter().map(|entry| DiagnosticEntry {
101 range: buffer.anchor_before(entry.range.start)
102 ..buffer.anchor_before(entry.range.end),
103 diagnostic: entry.diagnostic,
104 }),
105 buffer,
106 ),
107 }
108 }
109
110 /// Returns the number of diagnostics in the set.
111 pub fn len(&self) -> usize {
112 self.diagnostics.summary().count
113 }
114 /// Returns true when there are no diagnostics in this diagnostic set
115 pub fn is_empty(&self) -> bool {
116 self.len() == 0
117 }
118
119 /// Returns an iterator over the diagnostic entries in the set.
120 pub fn iter(&self) -> impl Iterator<Item = &DiagnosticEntry<Anchor>> {
121 self.diagnostics.iter()
122 }
123
124 /// Returns an iterator over the diagnostic entries that intersect the
125 /// given range of the buffer.
126 pub fn range<'a, T, O>(
127 &'a self,
128 range: Range<T>,
129 buffer: &'a text::BufferSnapshot,
130 inclusive: bool,
131 reversed: bool,
132 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
133 where
134 T: 'a + ToOffset,
135 O: FromAnchor,
136 {
137 let end_bias = if inclusive { Bias::Right } else { Bias::Left };
138 let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias);
139 let mut cursor = self.diagnostics.filter::<_, ()>(buffer, {
140 move |summary: &Summary| {
141 let start_cmp = range.start.cmp(&summary.max_end, buffer);
142 let end_cmp = range.end.cmp(&summary.min_start, buffer);
143 if inclusive {
144 start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal
145 } else {
146 start_cmp == Ordering::Less && end_cmp == Ordering::Greater
147 }
148 }
149 });
150
151 if reversed {
152 cursor.prev(buffer);
153 } else {
154 cursor.next(buffer);
155 }
156 iter::from_fn({
157 move || {
158 if let Some(diagnostic) = cursor.item() {
159 if reversed {
160 cursor.prev(buffer);
161 } else {
162 cursor.next(buffer);
163 }
164 Some(diagnostic.resolve(buffer))
165 } else {
166 None
167 }
168 }
169 })
170 }
171
172 /// Adds all of this set's diagnostic groups to the given output vector.
173 pub fn groups(
174 &self,
175 language_server_id: LanguageServerId,
176 output: &mut Vec<(LanguageServerId, DiagnosticGroup<Anchor>)>,
177 buffer: &text::BufferSnapshot,
178 ) {
179 let mut groups = HashMap::default();
180 for entry in self.diagnostics.iter() {
181 groups
182 .entry(entry.diagnostic.group_id)
183 .or_insert(Vec::new())
184 .push(entry.clone());
185 }
186
187 let start_ix = output.len();
188 output.extend(groups.into_values().filter_map(|mut entries| {
189 entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer));
190 entries
191 .iter()
192 .position(|entry| entry.diagnostic.is_primary)
193 .map(|primary_ix| {
194 (
195 language_server_id,
196 DiagnosticGroup {
197 entries,
198 primary_ix,
199 },
200 )
201 })
202 }));
203 output[start_ix..].sort_unstable_by(|(id_a, group_a), (id_b, group_b)| {
204 group_a.entries[group_a.primary_ix]
205 .range
206 .start
207 .cmp(&group_b.entries[group_b.primary_ix].range.start, buffer)
208 .then_with(|| id_a.cmp(id_b))
209 });
210 }
211
212 /// Returns all of the diagnostics in a particular diagnostic group,
213 /// in order of their position in the buffer.
214 pub fn group<'a, O: FromAnchor>(
215 &'a self,
216 group_id: usize,
217 buffer: &'a text::BufferSnapshot,
218 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>> {
219 self.iter()
220 .filter(move |entry| entry.diagnostic.group_id == group_id)
221 .map(|entry| entry.resolve(buffer))
222 }
223}
224
225impl sum_tree::Item for DiagnosticEntry<Anchor> {
226 type Summary = Summary;
227
228 fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
229 Summary {
230 start: self.range.start,
231 end: self.range.end,
232 min_start: self.range.start,
233 max_end: self.range.end,
234 count: 1,
235 }
236 }
237}
238
239impl DiagnosticEntry<Anchor> {
240 /// Converts the [DiagnosticEntry] to a different buffer coordinate type.
241 pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
242 DiagnosticEntry {
243 range: O::from_anchor(&self.range.start, buffer)
244 ..O::from_anchor(&self.range.end, buffer),
245 diagnostic: self.diagnostic.clone(),
246 }
247 }
248}
249
250impl Default for Summary {
251 fn default() -> Self {
252 Self {
253 start: Anchor::MIN,
254 end: Anchor::MAX,
255 min_start: Anchor::MAX,
256 max_end: Anchor::MIN,
257 count: 0,
258 }
259 }
260}
261
262impl sum_tree::Summary for Summary {
263 type Context = text::BufferSnapshot;
264
265 fn zero(_cx: &Self::Context) -> Self {
266 Default::default()
267 }
268
269 fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
270 if other.min_start.cmp(&self.min_start, buffer).is_lt() {
271 self.min_start = other.min_start;
272 }
273 if other.max_end.cmp(&self.max_end, buffer).is_gt() {
274 self.max_end = other.max_end;
275 }
276 self.start = other.start;
277 self.end = other.end;
278 self.count += other.count;
279 }
280}