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