2021-12-09 15:38:46 +00:00
|
|
|
use crate::Diagnostic;
|
2021-12-15 01:26:42 +00:00
|
|
|
use collections::HashMap;
|
2021-12-09 15:38:46 +00:00
|
|
|
use std::{
|
|
|
|
cmp::{Ordering, Reverse},
|
|
|
|
iter,
|
|
|
|
ops::Range,
|
|
|
|
};
|
|
|
|
use sum_tree::{self, Bias, SumTree};
|
2022-11-23 18:20:47 +00:00
|
|
|
use text::{Anchor, FromAnchor, PointUtf16, ToOffset};
|
2021-12-09 15:38:46 +00:00
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-12-09 15:38:46 +00:00
|
|
|
pub struct DiagnosticSet {
|
2021-12-09 16:53:08 +00:00
|
|
|
diagnostics: SumTree<DiagnosticEntry<Anchor>>,
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 16:53:08 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct DiagnosticEntry<T> {
|
|
|
|
pub range: Range<T>,
|
2021-12-09 15:38:46 +00:00
|
|
|
pub diagnostic: Diagnostic,
|
|
|
|
}
|
|
|
|
|
2022-01-04 16:38:45 +00:00
|
|
|
#[derive(Debug)]
|
2021-12-15 01:26:42 +00:00
|
|
|
pub struct DiagnosticGroup<T> {
|
2021-12-17 02:09:12 +00:00
|
|
|
pub entries: Vec<DiagnosticEntry<T>>,
|
|
|
|
pub primary_ix: usize,
|
2021-12-15 01:26:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 15:38:46 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Summary {
|
|
|
|
start: Anchor,
|
|
|
|
end: Anchor,
|
|
|
|
min_start: Anchor,
|
|
|
|
max_end: Anchor,
|
|
|
|
count: usize,
|
|
|
|
}
|
|
|
|
|
2022-03-31 22:39:52 +00:00
|
|
|
impl<T> DiagnosticEntry<T> {
|
|
|
|
// Used to provide diagnostic context to lsp codeAction request
|
|
|
|
pub fn to_lsp_diagnostic_stub(&self) -> lsp::Diagnostic {
|
|
|
|
let code = self
|
|
|
|
.diagnostic
|
|
|
|
.code
|
|
|
|
.clone()
|
|
|
|
.map(lsp::NumberOrString::String);
|
|
|
|
|
|
|
|
lsp::Diagnostic {
|
|
|
|
code,
|
|
|
|
severity: Some(self.diagnostic.severity),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:38:46 +00:00
|
|
|
impl DiagnosticSet {
|
2022-01-04 15:11:29 +00:00
|
|
|
pub fn from_sorted_entries<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
|
2021-12-09 15:38:46 +00:00
|
|
|
where
|
2021-12-09 16:53:08 +00:00
|
|
|
I: IntoIterator<Item = DiagnosticEntry<Anchor>>,
|
2021-12-09 15:38:46 +00:00
|
|
|
{
|
|
|
|
Self {
|
|
|
|
diagnostics: SumTree::from_iter(iter, buffer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 15:11:29 +00:00
|
|
|
pub fn new<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
|
2021-12-09 15:38:46 +00:00
|
|
|
where
|
2022-11-23 18:20:47 +00:00
|
|
|
I: IntoIterator<Item = DiagnosticEntry<PointUtf16>>,
|
2021-12-09 15:38:46 +00:00
|
|
|
{
|
|
|
|
let mut entries = iter.into_iter().collect::<Vec<_>>();
|
2021-12-09 16:53:08 +00:00
|
|
|
entries.sort_unstable_by_key(|entry| (entry.range.start, Reverse(entry.range.end)));
|
2021-12-10 08:43:21 +00:00
|
|
|
Self {
|
|
|
|
diagnostics: SumTree::from_iter(
|
|
|
|
entries.into_iter().map(|entry| DiagnosticEntry {
|
2022-11-17 03:20:16 +00:00
|
|
|
range: buffer.anchor_before(entry.range.start)
|
|
|
|
..buffer.anchor_before(entry.range.end),
|
2021-12-10 08:43:21 +00:00
|
|
|
diagnostic: entry.diagnostic,
|
|
|
|
}),
|
|
|
|
buffer,
|
|
|
|
),
|
|
|
|
}
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 16:53:08 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &DiagnosticEntry<Anchor>> {
|
2021-12-09 15:38:46 +00:00
|
|
|
self.diagnostics.iter()
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:53:08 +00:00
|
|
|
pub fn range<'a, T, O>(
|
2021-12-09 15:38:46 +00:00
|
|
|
&'a self,
|
|
|
|
range: Range<T>,
|
2021-12-10 14:58:37 +00:00
|
|
|
buffer: &'a text::BufferSnapshot,
|
2021-12-09 15:38:46 +00:00
|
|
|
inclusive: bool,
|
2022-03-15 14:11:41 +00:00
|
|
|
reversed: bool,
|
2021-12-09 16:53:08 +00:00
|
|
|
) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
|
2021-12-09 15:38:46 +00:00
|
|
|
where
|
|
|
|
T: 'a + ToOffset,
|
2021-12-09 16:53:08 +00:00
|
|
|
O: FromAnchor,
|
2021-12-09 15:38:46 +00:00
|
|
|
{
|
|
|
|
let end_bias = if inclusive { Bias::Right } else { Bias::Left };
|
|
|
|
let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias);
|
2022-03-15 13:57:30 +00:00
|
|
|
let mut cursor = self.diagnostics.filter::<_, ()>({
|
|
|
|
move |summary: &Summary| {
|
2022-03-24 18:48:31 +00:00
|
|
|
let start_cmp = range.start.cmp(&summary.max_end, buffer);
|
|
|
|
let end_cmp = range.end.cmp(&summary.min_start, buffer);
|
2022-03-15 13:57:30 +00:00
|
|
|
if inclusive {
|
|
|
|
start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal
|
|
|
|
} else {
|
|
|
|
start_cmp == Ordering::Less && end_cmp == Ordering::Greater
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
2022-03-15 13:57:30 +00:00
|
|
|
}
|
|
|
|
});
|
2021-12-09 15:38:46 +00:00
|
|
|
|
2022-03-15 14:11:41 +00:00
|
|
|
if reversed {
|
|
|
|
cursor.prev(buffer);
|
|
|
|
} else {
|
|
|
|
cursor.next(buffer);
|
|
|
|
}
|
2021-12-09 15:38:46 +00:00
|
|
|
iter::from_fn({
|
|
|
|
move || {
|
|
|
|
if let Some(diagnostic) = cursor.item() {
|
2022-03-15 14:11:41 +00:00
|
|
|
if reversed {
|
|
|
|
cursor.prev(buffer);
|
|
|
|
} else {
|
|
|
|
cursor.next(buffer);
|
|
|
|
}
|
2021-12-09 16:53:08 +00:00
|
|
|
Some(diagnostic.resolve(buffer))
|
2021-12-09 15:38:46 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-23 22:21:10 +00:00
|
|
|
pub fn groups(&self, output: &mut Vec<DiagnosticGroup<Anchor>>, buffer: &text::BufferSnapshot) {
|
2021-12-17 02:09:12 +00:00
|
|
|
let mut groups = HashMap::default();
|
2021-12-15 01:26:42 +00:00
|
|
|
for entry in self.diagnostics.iter() {
|
2021-12-17 02:09:12 +00:00
|
|
|
groups
|
2021-12-15 01:26:42 +00:00
|
|
|
.entry(entry.diagnostic.group_id)
|
2021-12-17 02:09:12 +00:00
|
|
|
.or_insert(Vec::new())
|
2021-12-23 14:32:21 +00:00
|
|
|
.push(entry.clone());
|
2021-12-15 01:26:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 22:21:10 +00:00
|
|
|
let start_ix = output.len();
|
|
|
|
output.extend(groups.into_values().filter_map(|mut entries| {
|
2022-03-24 18:48:31 +00:00
|
|
|
entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer));
|
2021-12-23 22:21:10 +00:00
|
|
|
entries
|
|
|
|
.iter()
|
|
|
|
.position(|entry| entry.diagnostic.is_primary)
|
|
|
|
.map(|primary_ix| DiagnosticGroup {
|
|
|
|
entries,
|
|
|
|
primary_ix,
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
output[start_ix..].sort_unstable_by(|a, b| {
|
2021-12-23 14:32:21 +00:00
|
|
|
a.entries[a.primary_ix]
|
|
|
|
.range
|
|
|
|
.start
|
|
|
|
.cmp(&b.entries[b.primary_ix].range.start, buffer)
|
|
|
|
});
|
2021-12-15 01:26:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 16:53:08 +00:00
|
|
|
pub fn group<'a, O: FromAnchor>(
|
|
|
|
&'a self,
|
|
|
|
group_id: usize,
|
2021-12-10 14:58:37 +00:00
|
|
|
buffer: &'a text::BufferSnapshot,
|
2021-12-09 16:53:08 +00:00
|
|
|
) -> impl 'a + Iterator<Item = DiagnosticEntry<O>> {
|
2021-12-09 15:38:46 +00:00
|
|
|
self.iter()
|
|
|
|
.filter(move |entry| entry.diagnostic.group_id == group_id)
|
2021-12-09 16:53:08 +00:00
|
|
|
.map(|entry| entry.resolve(buffer))
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 16:53:08 +00:00
|
|
|
impl sum_tree::Item for DiagnosticEntry<Anchor> {
|
2021-12-09 15:38:46 +00:00
|
|
|
type Summary = Summary;
|
|
|
|
|
|
|
|
fn summary(&self) -> Self::Summary {
|
|
|
|
Summary {
|
2022-08-10 21:39:24 +00:00
|
|
|
start: self.range.start,
|
|
|
|
end: self.range.end,
|
|
|
|
min_start: self.range.start,
|
|
|
|
max_end: self.range.end,
|
2021-12-09 15:38:46 +00:00
|
|
|
count: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:53:08 +00:00
|
|
|
impl DiagnosticEntry<Anchor> {
|
2021-12-10 14:58:37 +00:00
|
|
|
pub fn resolve<O: FromAnchor>(&self, buffer: &text::BufferSnapshot) -> DiagnosticEntry<O> {
|
2021-12-09 16:53:08 +00:00
|
|
|
DiagnosticEntry {
|
|
|
|
range: O::from_anchor(&self.range.start, buffer)
|
|
|
|
..O::from_anchor(&self.range.end, buffer),
|
|
|
|
diagnostic: self.diagnostic.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:38:46 +00:00
|
|
|
impl Default for Summary {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-03-24 17:35:30 +00:00
|
|
|
start: Anchor::MIN,
|
|
|
|
end: Anchor::MAX,
|
|
|
|
min_start: Anchor::MAX,
|
|
|
|
max_end: Anchor::MIN,
|
2021-12-09 15:38:46 +00:00
|
|
|
count: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl sum_tree::Summary for Summary {
|
2021-12-10 14:58:37 +00:00
|
|
|
type Context = text::BufferSnapshot;
|
2021-12-09 15:38:46 +00:00
|
|
|
|
|
|
|
fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
|
2022-03-24 18:48:31 +00:00
|
|
|
if other.min_start.cmp(&self.min_start, buffer).is_lt() {
|
2022-08-10 21:39:24 +00:00
|
|
|
self.min_start = other.min_start;
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
2022-03-24 18:48:31 +00:00
|
|
|
if other.max_end.cmp(&self.max_end, buffer).is_gt() {
|
2022-08-10 21:39:24 +00:00
|
|
|
self.max_end = other.max_end;
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
2022-08-10 21:39:24 +00:00
|
|
|
self.start = other.start;
|
|
|
|
self.end = other.end;
|
2021-12-09 15:38:46 +00:00
|
|
|
self.count += other.count;
|
|
|
|
}
|
|
|
|
}
|