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