Rip out "diagnostic providers"

This commit is contained in:
Antonio Scandurra 2022-01-04 16:11:29 +01:00
parent 496066db59
commit 508b9dc024
16 changed files with 267 additions and 688 deletions

View file

@ -66,7 +66,7 @@ pub struct Buffer {
parsing_in_background: bool,
parse_count: usize,
remote_selections: TreeMap<ReplicaId, Arc<[Selection<Anchor>]>>,
diagnostic_sets: Vec<DiagnosticSet>,
diagnostics: DiagnosticSet,
diagnostics_update_count: usize,
language_server: Option<LanguageServerState>,
deferred_ops: OperationQueue<Operation>,
@ -77,7 +77,7 @@ pub struct Buffer {
pub struct BufferSnapshot {
text: text::BufferSnapshot,
tree: Option<Tree>,
diagnostic_sets: Vec<DiagnosticSet>,
diagnostics: DiagnosticSet,
remote_selections: TreeMap<ReplicaId, Arc<[Selection<Anchor>]>>,
diagnostics_update_count: usize,
is_parsing: bool,
@ -121,7 +121,6 @@ struct LanguageServerSnapshot {
pub enum Operation {
Buffer(text::Operation),
UpdateDiagnostics {
provider_name: String,
diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
lamport_timestamp: clock::Lamport,
},
@ -306,17 +305,11 @@ impl Buffer {
);
}
let snapshot = this.snapshot();
for diagnostic_set in message.diagnostic_sets {
let (provider_name, entries) = proto::deserialize_diagnostic_set(diagnostic_set);
this.apply_diagnostic_update(
DiagnosticSet::from_sorted_entries(
provider_name,
entries.into_iter().cloned(),
&snapshot,
),
cx,
);
}
let entries = proto::deserialize_diagnostics(message.diagnostics);
this.apply_diagnostic_update(
DiagnosticSet::from_sorted_entries(entries.into_iter().cloned(), &snapshot),
cx,
);
Ok(this)
}
@ -338,13 +331,7 @@ impl Buffer {
selections: proto::serialize_selections(selections),
})
.collect(),
diagnostic_sets: self
.diagnostic_sets
.iter()
.map(|set| {
proto::serialize_diagnostic_set(set.provider_name().to_string(), set.iter())
})
.collect(),
diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()),
}
}
@ -379,7 +366,7 @@ impl Buffer {
pending_autoindent: Default::default(),
language: None,
remote_selections: Default::default(),
diagnostic_sets: Default::default(),
diagnostics: Default::default(),
diagnostics_update_count: 0,
language_server: None,
deferred_ops: OperationQueue::new(),
@ -393,7 +380,7 @@ impl Buffer {
text: self.text.snapshot(),
tree: self.syntax_tree(),
remote_selections: self.remote_selections.clone(),
diagnostic_sets: self.diagnostic_sets.clone(),
diagnostics: self.diagnostics.clone(),
diagnostics_update_count: self.diagnostics_update_count,
is_parsing: self.parsing_in_background,
language: self.language.clone(),
@ -743,7 +730,6 @@ impl Buffer {
pub fn update_diagnostics<T>(
&mut self,
provider_name: Arc<str>,
version: Option<i32>,
mut diagnostics: Vec<DiagnosticEntry<T>>,
cx: &mut ModelContext<Self>,
@ -833,10 +819,9 @@ impl Buffer {
}
drop(edits_since_save);
let set = DiagnosticSet::new(provider_name, sanitized_diagnostics, content);
let set = DiagnosticSet::new(sanitized_diagnostics, content);
self.apply_diagnostic_update(set.clone(), cx);
Ok(Operation::UpdateDiagnostics {
provider_name: set.provider_name().to_string(),
diagnostics: set.iter().cloned().collect(),
lamport_timestamp: self.text.lamport_clock.tick(),
})
@ -1347,17 +1332,12 @@ impl Buffer {
unreachable!("buffer operations should never be applied at this layer")
}
Operation::UpdateDiagnostics {
provider_name,
diagnostics: diagnostic_set,
..
} => {
let snapshot = self.snapshot();
self.apply_diagnostic_update(
DiagnosticSet::from_sorted_entries(
provider_name,
diagnostic_set.iter().cloned(),
&snapshot,
),
DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
cx,
);
}
@ -1379,15 +1359,8 @@ impl Buffer {
}
}
fn apply_diagnostic_update(&mut self, set: DiagnosticSet, cx: &mut ModelContext<Self>) {
match self
.diagnostic_sets
.binary_search_by_key(&set.provider_name(), |set| set.provider_name())
{
Ok(ix) => self.diagnostic_sets[ix] = set.clone(),
Err(ix) => self.diagnostic_sets.insert(ix, set.clone()),
}
fn apply_diagnostic_update(&mut self, diagnostics: DiagnosticSet, cx: &mut ModelContext<Self>) {
self.diagnostics = diagnostics;
self.diagnostics_update_count += 1;
cx.notify();
cx.emit(Event::DiagnosticsUpdated);
@ -1625,7 +1598,7 @@ impl BufferSnapshot {
let mut highlights = None;
let mut diagnostic_endpoints = Vec::<DiagnosticEndpoint>::new();
if let Some(theme) = theme {
for (_, entry) in self.diagnostics_in_range::<_, usize>(range.clone()) {
for entry in self.diagnostics_in_range::<_, usize>(range.clone()) {
diagnostic_endpoints.push(DiagnosticEndpoint {
offset: entry.range.start,
is_start: true,
@ -1756,38 +1729,28 @@ impl BufferSnapshot {
pub fn diagnostics_in_range<'a, T, O>(
&'a self,
search_range: Range<T>,
) -> impl 'a + Iterator<Item = (&'a str, DiagnosticEntry<O>)>
) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
where
T: 'a + Clone + ToOffset,
O: 'a + FromAnchor,
{
self.diagnostic_sets.iter().flat_map(move |set| {
set.range(search_range.clone(), self, true)
.map(|e| (set.provider_name(), e))
})
self.diagnostics.range(search_range.clone(), self, true)
}
pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
let mut groups = Vec::new();
for set in &self.diagnostic_sets {
set.groups(&mut groups, self);
}
self.diagnostics.groups(&mut groups, self);
groups
}
pub fn diagnostic_group<'a, O>(
&'a self,
provider_name: &str,
group_id: usize,
) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
where
O: 'a + FromAnchor,
{
self.diagnostic_sets
.iter()
.find(|s| s.provider_name() == provider_name)
.into_iter()
.flat_map(move |s| s.group(group_id, self))
self.diagnostics.group(group_id, self)
}
pub fn diagnostics_update_count(&self) -> usize {
@ -1805,7 +1768,7 @@ impl Clone for BufferSnapshot {
text: self.text.clone(),
tree: self.tree.clone(),
remote_selections: self.remote_selections.clone(),
diagnostic_sets: self.diagnostic_sets.clone(),
diagnostics: self.diagnostics.clone(),
diagnostics_update_count: self.diagnostics_update_count,
is_parsing: self.is_parsing,
language: self.language.clone(),

View file

@ -4,14 +4,12 @@ use std::{
cmp::{Ordering, Reverse},
iter,
ops::Range,
sync::Arc,
};
use sum_tree::{self, Bias, SumTree};
use text::{Anchor, FromAnchor, Point, ToOffset};
#[derive(Clone, Debug)]
pub struct DiagnosticSet {
provider_name: Arc<str>,
diagnostics: SumTree<DiagnosticEntry<Anchor>>,
}
@ -36,32 +34,22 @@ pub struct Summary {
}
impl DiagnosticSet {
pub fn provider_name(&self) -> &str {
&self.provider_name
}
pub fn from_sorted_entries<I>(
provider_name: impl Into<Arc<str>>,
iter: I,
buffer: &text::BufferSnapshot,
) -> Self
pub fn from_sorted_entries<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
where
I: IntoIterator<Item = DiagnosticEntry<Anchor>>,
{
Self {
provider_name: provider_name.into(),
diagnostics: SumTree::from_iter(iter, buffer),
}
}
pub fn new<I>(provider_name: Arc<str>, iter: I, buffer: &text::BufferSnapshot) -> Self
pub fn new<I>(iter: I, buffer: &text::BufferSnapshot) -> Self
where
I: IntoIterator<Item = DiagnosticEntry<Point>>,
{
let mut entries = iter.into_iter().collect::<Vec<_>>();
entries.sort_unstable_by_key(|entry| (entry.range.start, Reverse(entry.range.end)));
Self {
provider_name,
diagnostics: SumTree::from_iter(
entries.into_iter().map(|entry| DiagnosticEntry {
range: buffer.anchor_before(entry.range.start)
@ -159,7 +147,6 @@ impl DiagnosticSet {
impl Default for DiagnosticSet {
fn default() -> Self {
Self {
provider_name: "".into(),
diagnostics: Default::default(),
}
}

View file

@ -6,10 +6,9 @@ pub mod proto;
mod tests;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
pub use buffer::Operation;
pub use buffer::*;
use collections::{HashMap, HashSet};
use collections::HashSet;
pub use diagnostic_set::DiagnosticEntry;
use gpui::AppContext;
use highlight_map::HighlightMap;
@ -60,18 +59,9 @@ pub struct BracketPair {
pub newline: bool,
}
#[async_trait]
pub trait DiagnosticProvider: 'static + Send + Sync {
async fn diagnose(
&self,
path: Arc<Path>,
) -> Result<HashMap<Arc<Path>, Vec<DiagnosticEntry<usize>>>>;
}
pub struct Language {
pub(crate) config: LanguageConfig,
pub(crate) grammar: Option<Arc<Grammar>>,
pub(crate) diagnostic_provider: Option<Arc<dyn DiagnosticProvider>>,
}
pub struct Grammar {
@ -136,7 +126,6 @@ impl Language {
highlight_map: Default::default(),
})
}),
diagnostic_provider: None,
}
}
@ -170,11 +159,6 @@ impl Language {
Ok(self)
}
pub fn with_diagnostic_provider(mut self, source: impl DiagnosticProvider) -> Self {
self.diagnostic_provider = Some(Arc::new(source));
self
}
pub fn name(&self) -> &str {
self.config.name.as_str()
}
@ -208,10 +192,6 @@ impl Language {
}
}
pub fn diagnostic_provider(&self) -> Option<&Arc<dyn DiagnosticProvider>> {
self.diagnostic_provider.as_ref()
}
pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
self.config
.language_server

View file

@ -57,16 +57,12 @@ pub fn serialize_operation(operation: &Operation) -> proto::Operation {
lamport_timestamp: lamport_timestamp.value,
}),
Operation::UpdateDiagnostics {
provider_name,
diagnostics,
lamport_timestamp,
} => proto::operation::Variant::UpdateDiagnosticSet(proto::UpdateDiagnosticSet {
} => proto::operation::Variant::UpdateDiagnostics(proto::UpdateDiagnostics {
replica_id: lamport_timestamp.replica_id as u32,
lamport_timestamp: lamport_timestamp.value,
diagnostic_set: Some(serialize_diagnostic_set(
provider_name.clone(),
diagnostics.iter(),
)),
diagnostics: serialize_diagnostics(diagnostics.iter()),
}),
}),
}
@ -103,33 +99,29 @@ pub fn serialize_selections(selections: &Arc<[Selection<Anchor>]>) -> Vec<proto:
.collect()
}
pub fn serialize_diagnostic_set<'a>(
provider_name: String,
pub fn serialize_diagnostics<'a>(
diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<Anchor>>,
) -> proto::DiagnosticSet {
proto::DiagnosticSet {
provider_name,
diagnostics: diagnostics
.into_iter()
.map(|entry| proto::Diagnostic {
start: Some(serialize_anchor(&entry.range.start)),
end: Some(serialize_anchor(&entry.range.end)),
message: entry.diagnostic.message.clone(),
severity: match entry.diagnostic.severity {
DiagnosticSeverity::ERROR => proto::diagnostic::Severity::Error,
DiagnosticSeverity::WARNING => proto::diagnostic::Severity::Warning,
DiagnosticSeverity::INFORMATION => proto::diagnostic::Severity::Information,
DiagnosticSeverity::HINT => proto::diagnostic::Severity::Hint,
_ => proto::diagnostic::Severity::None,
} as i32,
group_id: entry.diagnostic.group_id as u64,
is_primary: entry.diagnostic.is_primary,
is_valid: entry.diagnostic.is_valid,
code: entry.diagnostic.code.clone(),
is_disk_based: entry.diagnostic.is_disk_based,
})
.collect(),
}
) -> Vec<proto::Diagnostic> {
diagnostics
.into_iter()
.map(|entry| proto::Diagnostic {
start: Some(serialize_anchor(&entry.range.start)),
end: Some(serialize_anchor(&entry.range.end)),
message: entry.diagnostic.message.clone(),
severity: match entry.diagnostic.severity {
DiagnosticSeverity::ERROR => proto::diagnostic::Severity::Error,
DiagnosticSeverity::WARNING => proto::diagnostic::Severity::Warning,
DiagnosticSeverity::INFORMATION => proto::diagnostic::Severity::Information,
DiagnosticSeverity::HINT => proto::diagnostic::Severity::Hint,
_ => proto::diagnostic::Severity::None,
} as i32,
group_id: entry.diagnostic.group_id as u64,
is_primary: entry.diagnostic.is_primary,
is_valid: entry.diagnostic.is_valid,
code: entry.diagnostic.code.clone(),
is_disk_based: entry.diagnostic.is_disk_based,
})
.collect()
}
fn serialize_anchor(anchor: &Anchor) -> proto::Anchor {
@ -215,21 +207,13 @@ pub fn deserialize_operation(message: proto::Operation) -> Result<Operation> {
value: message.lamport_timestamp,
},
},
proto::operation::Variant::UpdateDiagnosticSet(message) => {
let (provider_name, diagnostics) = deserialize_diagnostic_set(
message
.diagnostic_set
.ok_or_else(|| anyhow!("missing diagnostic set"))?,
);
Operation::UpdateDiagnostics {
provider_name,
diagnostics,
lamport_timestamp: clock::Lamport {
replica_id: message.replica_id as ReplicaId,
value: message.lamport_timestamp,
},
}
}
proto::operation::Variant::UpdateDiagnostics(message) => Operation::UpdateDiagnostics {
diagnostics: deserialize_diagnostics(message.diagnostics),
lamport_timestamp: clock::Lamport {
replica_id: message.replica_id as ReplicaId,
value: message.lamport_timestamp,
},
},
},
)
}
@ -269,40 +253,32 @@ pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selecti
)
}
pub fn deserialize_diagnostic_set(
message: proto::DiagnosticSet,
) -> (String, Arc<[DiagnosticEntry<Anchor>]>) {
(
message.provider_name,
message
.diagnostics
.into_iter()
.filter_map(|diagnostic| {
Some(DiagnosticEntry {
range: deserialize_anchor(diagnostic.start?)?
..deserialize_anchor(diagnostic.end?)?,
diagnostic: Diagnostic {
severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)?
{
proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
proto::diagnostic::Severity::Information => {
DiagnosticSeverity::INFORMATION
}
proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
proto::diagnostic::Severity::None => return None,
},
message: diagnostic.message,
group_id: diagnostic.group_id as usize,
code: diagnostic.code,
is_valid: diagnostic.is_valid,
is_primary: diagnostic.is_primary,
is_disk_based: diagnostic.is_disk_based,
pub fn deserialize_diagnostics(
diagnostics: Vec<proto::Diagnostic>,
) -> Arc<[DiagnosticEntry<Anchor>]> {
diagnostics
.into_iter()
.filter_map(|diagnostic| {
Some(DiagnosticEntry {
range: deserialize_anchor(diagnostic.start?)?..deserialize_anchor(diagnostic.end?)?,
diagnostic: Diagnostic {
severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)? {
proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
proto::diagnostic::Severity::Information => DiagnosticSeverity::INFORMATION,
proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
proto::diagnostic::Severity::None => return None,
},
})
message: diagnostic.message,
group_id: diagnostic.group_id as usize,
code: diagnostic.code,
is_valid: diagnostic.is_valid,
is_primary: diagnostic.is_primary,
is_disk_based: diagnostic.is_disk_based,
},
})
.collect(),
)
})
.collect()
}
fn deserialize_anchor(anchor: proto::Anchor) -> Option<Anchor> {

View file

@ -455,7 +455,6 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
// Receive diagnostics for an earlier version of the buffer.
buffer
.update_diagnostics(
"lsp".into(),
Some(open_notification.text_document.version),
vec![
DiagnosticEntry {
@ -503,34 +502,28 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
.diagnostics_in_range::<_, Point>(Point::new(3, 0)..Point::new(5, 0))
.collect::<Vec<_>>(),
&[
(
"lsp",
DiagnosticEntry {
range: Point::new(3, 9)..Point::new(3, 11),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(),
is_disk_based: true,
group_id: 1,
is_primary: true,
..Default::default()
},
DiagnosticEntry {
range: Point::new(3, 9)..Point::new(3, 11),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(),
is_disk_based: true,
group_id: 1,
is_primary: true,
..Default::default()
},
},
DiagnosticEntry {
range: Point::new(4, 9)..Point::new(4, 12),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'CCC'".to_string(),
is_disk_based: true,
group_id: 2,
is_primary: true,
..Default::default()
}
),
(
"lsp",
DiagnosticEntry {
range: Point::new(4, 9)..Point::new(4, 12),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'CCC'".to_string(),
is_disk_based: true,
group_id: 2,
is_primary: true,
..Default::default()
}
}
)
}
]
);
assert_eq!(
@ -557,7 +550,6 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
// Ensure overlapping diagnostics are highlighted correctly.
buffer
.update_diagnostics(
"lsp".into(),
Some(open_notification.text_document.version),
vec![
DiagnosticEntry {
@ -591,33 +583,27 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
.diagnostics_in_range::<_, Point>(Point::new(2, 0)..Point::new(3, 0))
.collect::<Vec<_>>(),
&[
(
"lsp",
DiagnosticEntry {
range: Point::new(2, 9)..Point::new(2, 12),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::WARNING,
message: "unreachable statement".to_string(),
group_id: 1,
is_primary: true,
..Default::default()
}
DiagnosticEntry {
range: Point::new(2, 9)..Point::new(2, 12),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::WARNING,
message: "unreachable statement".to_string(),
group_id: 1,
is_primary: true,
..Default::default()
}
),
(
"lsp",
DiagnosticEntry {
range: Point::new(2, 9)..Point::new(2, 10),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(),
is_disk_based: true,
group_id: 0,
is_primary: true,
..Default::default()
},
}
)
},
DiagnosticEntry {
range: Point::new(2, 9)..Point::new(2, 10),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(),
is_disk_based: true,
group_id: 0,
is_primary: true,
..Default::default()
},
}
]
);
assert_eq!(
@ -654,7 +640,6 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
buffer.update(&mut cx, |buffer, cx| {
buffer
.update_diagnostics(
"lsp".into(),
Some(change_notification_2.text_document.version),
vec![
DiagnosticEntry {
@ -689,34 +674,28 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
.diagnostics_in_range::<_, Point>(0..buffer.len())
.collect::<Vec<_>>(),
&[
(
"lsp",
DiagnosticEntry {
range: Point::new(2, 21)..Point::new(2, 22),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(),
is_disk_based: true,
group_id: 0,
is_primary: true,
..Default::default()
}
DiagnosticEntry {
range: Point::new(2, 21)..Point::new(2, 22),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(),
is_disk_based: true,
group_id: 0,
is_primary: true,
..Default::default()
}
),
(
"lsp",
DiagnosticEntry {
range: Point::new(3, 9)..Point::new(3, 11),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(),
is_disk_based: true,
group_id: 1,
is_primary: true,
..Default::default()
},
}
)
},
DiagnosticEntry {
range: Point::new(3, 9)..Point::new(3, 11),
diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(),
is_disk_based: true,
group_id: 1,
is_primary: true,
..Default::default()
},
}
]
);
});
@ -735,7 +714,6 @@ async fn test_empty_diagnostic_ranges(mut cx: gpui::TestAppContext) {
buffer.set_language(Some(Arc::new(rust_lang())), None, cx);
buffer
.update_diagnostics(
"lsp".into(),
None,
vec![
DiagnosticEntry {