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)]
 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<PointUtf16>>,
 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        reversed: bool,
 75    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
 76    where
 77        T: 'a + ToOffset,
 78        O: FromAnchor,
 79    {
 80        let end_bias = if inclusive { Bias::Right } else { Bias::Left };
 81        let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias);
 82        let mut cursor = self.diagnostics.filter::<_, ()>({
 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
 94        if reversed {
 95            cursor.prev(buffer);
 96        } else {
 97            cursor.next(buffer);
 98        }
 99        iter::from_fn({
100            move || {
101                if let Some(diagnostic) = cursor.item() {
102                    if reversed {
103                        cursor.prev(buffer);
104                    } else {
105                        cursor.next(buffer);
106                    }
107                    Some(diagnostic.resolve(buffer))
108                } else {
109                    None
110                }
111            }
112        })
113    }
114
115    pub fn groups(&self, output: &mut Vec<DiagnosticGroup<Anchor>>, buffer: &text::BufferSnapshot) {
116        let mut groups = HashMap::default();
117        for entry in self.diagnostics.iter() {
118            groups
119                .entry(entry.diagnostic.group_id)
120                .or_insert(Vec::new())
121                .push(entry.clone());
122        }
123
124        let start_ix = output.len();
125        output.extend(groups.into_values().filter_map(|mut entries| {
126            entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer).unwrap());
127            entries
128                .iter()
129                .position(|entry| entry.diagnostic.is_primary)
130                .map(|primary_ix| DiagnosticGroup {
131                    entries,
132                    primary_ix,
133                })
134        }));
135        output[start_ix..].sort_unstable_by(|a, b| {
136            a.entries[a.primary_ix]
137                .range
138                .start
139                .cmp(&b.entries[b.primary_ix].range.start, buffer)
140                .unwrap()
141        });
142    }
143
144    pub fn group<'a, O: FromAnchor>(
145        &'a self,
146        group_id: usize,
147        buffer: &'a text::BufferSnapshot,
148    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>> {
149        self.iter()
150            .filter(move |entry| entry.diagnostic.group_id == group_id)
151            .map(|entry| entry.resolve(buffer))
152    }
153}
154
155impl Default for DiagnosticSet {
156    fn default() -> Self {
157        Self {
158            diagnostics: Default::default(),
159        }
160    }
161}
162
163impl sum_tree::Item for DiagnosticEntry<Anchor> {
164    type Summary = Summary;
165
166    fn summary(&self) -> Self::Summary {
167        Summary {
168            start: self.range.start.clone(),
169            end: self.range.end.clone(),
170            min_start: self.range.start.clone(),
171            max_end: self.range.end.clone(),
172            count: 1,
173        }
174    }
175}
176
177impl DiagnosticEntry<Anchor> {
178    pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
179        DiagnosticEntry {
180            range: O::from_anchor(&self.range.start, buffer)
181                ..O::from_anchor(&self.range.end, buffer),
182            diagnostic: self.diagnostic.clone(),
183        }
184    }
185}
186
187impl Default for Summary {
188    fn default() -> Self {
189        Self {
190            start: Anchor::min(),
191            end: Anchor::max(),
192            min_start: Anchor::max(),
193            max_end: Anchor::min(),
194            count: 0,
195        }
196    }
197}
198
199impl sum_tree::Summary for Summary {
200    type Context = text::BufferSnapshot;
201
202    fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
203        if other
204            .min_start
205            .cmp(&self.min_start, buffer)
206            .unwrap()
207            .is_lt()
208        {
209            self.min_start = other.min_start.clone();
210        }
211        if other.max_end.cmp(&self.max_end, buffer).unwrap().is_gt() {
212            self.max_end = other.max_end.clone();
213        }
214        self.start = other.start.clone();
215        self.end = other.end.clone();
216        self.count += other.count;
217    }
218}