diagnostic_set.rs

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