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);
85 let end_cmp = range.end.cmp(&summary.min_start, buffer);
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));
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 });
141 }
142
143 pub fn group<'a, O: FromAnchor>(
144 &'a self,
145 group_id: usize,
146 buffer: &'a text::BufferSnapshot,
147 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>> {
148 self.iter()
149 .filter(move |entry| entry.diagnostic.group_id == group_id)
150 .map(|entry| entry.resolve(buffer))
151 }
152}
153
154impl Default for DiagnosticSet {
155 fn default() -> Self {
156 Self {
157 diagnostics: Default::default(),
158 }
159 }
160}
161
162impl sum_tree::Item for DiagnosticEntry<Anchor> {
163 type Summary = Summary;
164
165 fn summary(&self) -> Self::Summary {
166 Summary {
167 start: self.range.start.clone(),
168 end: self.range.end.clone(),
169 min_start: self.range.start.clone(),
170 max_end: self.range.end.clone(),
171 count: 1,
172 }
173 }
174}
175
176impl DiagnosticEntry<Anchor> {
177 pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
178 DiagnosticEntry {
179 range: O::from_anchor(&self.range.start, buffer)
180 ..O::from_anchor(&self.range.end, buffer),
181 diagnostic: self.diagnostic.clone(),
182 }
183 }
184}
185
186impl Default for Summary {
187 fn default() -> Self {
188 Self {
189 start: Anchor::MIN,
190 end: Anchor::MAX,
191 min_start: Anchor::MAX,
192 max_end: Anchor::MIN,
193 count: 0,
194 }
195 }
196}
197
198impl sum_tree::Summary for Summary {
199 type Context = text::BufferSnapshot;
200
201 fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
202 if other.min_start.cmp(&self.min_start, buffer).is_lt() {
203 self.min_start = other.min_start.clone();
204 }
205 if other.max_end.cmp(&self.max_end, buffer).is_gt() {
206 self.max_end = other.max_end.clone();
207 }
208 self.start = other.start.clone();
209 self.end = other.end.clone();
210 self.count += other.count;
211 }
212}