diagnostic_set.rs

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