diagnostic_set.rs

  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, 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
 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_before(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}
170impl sum_tree::Item for DiagnosticEntry<Anchor> {
171    type Summary = Summary;
172
173    fn summary(&self) -> Self::Summary {
174        Summary {
175            start: self.range.start,
176            end: self.range.end,
177            min_start: self.range.start,
178            max_end: self.range.end,
179            count: 1,
180        }
181    }
182}
183
184impl DiagnosticEntry<Anchor> {
185    pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
186        DiagnosticEntry {
187            range: O::from_anchor(&self.range.start, buffer)
188                ..O::from_anchor(&self.range.end, buffer),
189            diagnostic: self.diagnostic.clone(),
190        }
191    }
192}
193
194impl Default for Summary {
195    fn default() -> Self {
196        Self {
197            start: Anchor::MIN,
198            end: Anchor::MAX,
199            min_start: Anchor::MAX,
200            max_end: Anchor::MIN,
201            count: 0,
202        }
203    }
204}
205
206impl sum_tree::Summary for Summary {
207    type Context = text::BufferSnapshot;
208
209    fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
210        if other.min_start.cmp(&self.min_start, buffer).is_lt() {
211            self.min_start = other.min_start;
212        }
213        if other.max_end.cmp(&self.max_end, buffer).is_gt() {
214            self.max_end = other.max_end;
215        }
216        self.start = other.start;
217        self.end = other.end;
218        self.count += other.count;
219    }
220}