Show only disk-based diagnostics in ProjectDiagnosticsEditor

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-12-23 16:47:54 +01:00
parent 304afc1813
commit b9d1ca4341
7 changed files with 46 additions and 42 deletions

View file

@ -237,7 +237,11 @@ impl ProjectDiagnosticsEditor {
let mut diagnostic_blocks = Vec::new(); let mut diagnostic_blocks = Vec::new();
let excerpts_snapshot = self.excerpts.update(cx, |excerpts, excerpts_cx| { let excerpts_snapshot = self.excerpts.update(cx, |excerpts, excerpts_cx| {
let mut old_groups = groups.iter_mut().enumerate().peekable(); let mut old_groups = groups.iter_mut().enumerate().peekable();
let mut new_groups = snapshot.diagnostic_groups().into_iter().peekable(); let mut new_groups = snapshot
.diagnostic_groups()
.into_iter()
.filter(|group| group.entries[group.primary_ix].diagnostic.is_disk_based)
.peekable();
loop { loop {
let mut to_insert = None; let mut to_insert = None;

View file

@ -19,7 +19,7 @@ use std::{
any::Any, any::Any,
cell::RefCell, cell::RefCell,
cmp::{self, Ordering}, cmp::{self, Ordering},
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap},
ffi::OsString, ffi::OsString,
future::Future, future::Future,
iter::{Iterator, Peekable}, iter::{Iterator, Peekable},
@ -87,13 +87,13 @@ pub struct BufferSnapshot {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diagnostic { pub struct Diagnostic {
pub source: Option<String>,
pub code: Option<String>, pub code: Option<String>,
pub severity: DiagnosticSeverity, pub severity: DiagnosticSeverity,
pub message: String, pub message: String,
pub group_id: usize, pub group_id: usize,
pub is_valid: bool, pub is_valid: bool,
pub is_primary: bool, pub is_primary: bool,
pub is_disk_based: bool,
} }
struct LanguageServerState { struct LanguageServerState {
@ -733,7 +733,7 @@ impl Buffer {
fn compare_diagnostics(a: &Diagnostic, b: &Diagnostic) -> Ordering { fn compare_diagnostics(a: &Diagnostic, b: &Diagnostic) -> Ordering {
Ordering::Equal Ordering::Equal
.then_with(|| b.is_primary.cmp(&a.is_primary)) .then_with(|| b.is_primary.cmp(&a.is_primary))
.then_with(|| a.source.cmp(&b.source)) .then_with(|| a.is_disk_based.cmp(&b.is_disk_based))
.then_with(|| a.severity.cmp(&b.severity)) .then_with(|| a.severity.cmp(&b.severity))
.then_with(|| a.message.cmp(&b.message)) .then_with(|| a.message.cmp(&b.message))
} }
@ -760,13 +760,6 @@ impl Buffer {
self.deref() self.deref()
}; };
let empty_set = HashSet::new();
let disk_based_sources = self
.language
.as_ref()
.and_then(|language| language.disk_based_diagnostic_sources())
.unwrap_or(&empty_set);
let mut edits_since_save = content let mut edits_since_save = content
.edits_since::<PointUtf16>(&self.saved_version) .edits_since::<PointUtf16>(&self.saved_version)
.peekable(); .peekable();
@ -781,12 +774,7 @@ impl Buffer {
// Some diagnostics are based on files on disk instead of buffers' // Some diagnostics are based on files on disk instead of buffers'
// current contents. Adjust these diagnostics' ranges to reflect // current contents. Adjust these diagnostics' ranges to reflect
// any unsaved edits. // any unsaved edits.
if entry if entry.diagnostic.is_disk_based {
.diagnostic
.source
.as_ref()
.map_or(false, |source| disk_based_sources.contains(source))
{
while let Some(edit) = edits_since_save.peek() { while let Some(edit) = edits_since_save.peek() {
if edit.old.end <= start { if edit.old.end <= start {
last_edit_old_end = edit.old.end; last_edit_old_end = edit.old.end;
@ -2008,13 +1996,13 @@ impl operation_queue::Operation for Operation {
impl Default for Diagnostic { impl Default for Diagnostic {
fn default() -> Self { fn default() -> Self {
Self { Self {
source: Default::default(),
code: Default::default(), code: Default::default(),
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: Default::default(), message: Default::default(),
group_id: Default::default(), group_id: Default::default(),
is_primary: Default::default(), is_primary: Default::default(),
is_valid: true, is_valid: true,
is_disk_based: false,
} }
} }
} }

View file

@ -8,13 +8,14 @@ mod tests;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
pub use buffer::Operation; pub use buffer::Operation;
pub use buffer::*; pub use buffer::*;
use collections::HashSet;
pub use diagnostic_set::DiagnosticEntry; pub use diagnostic_set::DiagnosticEntry;
use gpui::AppContext; use gpui::AppContext;
use highlight_map::HighlightMap; use highlight_map::HighlightMap;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use parking_lot::Mutex; use parking_lot::Mutex;
use serde::Deserialize; use serde::Deserialize;
use std::{collections::HashSet, path::Path, str, sync::Arc}; use std::{path::Path, str, sync::Arc};
use theme::SyntaxTheme; use theme::SyntaxTheme;
use tree_sitter::{self, Query}; use tree_sitter::{self, Query};
pub use tree_sitter::{Parser, Tree}; pub use tree_sitter::{Parser, Tree};

View file

@ -119,7 +119,7 @@ pub fn serialize_diagnostics<'a>(
is_primary: entry.diagnostic.is_primary, is_primary: entry.diagnostic.is_primary,
is_valid: entry.diagnostic.is_valid, is_valid: entry.diagnostic.is_valid,
code: entry.diagnostic.code.clone(), code: entry.diagnostic.code.clone(),
source: entry.diagnostic.source.clone(), is_disk_based: entry.diagnostic.is_disk_based,
}) })
.collect() .collect()
} }
@ -271,10 +271,10 @@ pub fn deserialize_diagnostics(
}, },
message: diagnostic.message, message: diagnostic.message,
group_id: diagnostic.group_id as usize, group_id: diagnostic.group_id as usize,
is_primary: diagnostic.is_primary,
code: diagnostic.code, code: diagnostic.code,
source: diagnostic.source,
is_valid: diagnostic.is_valid, is_valid: diagnostic.is_valid,
is_primary: diagnostic.is_primary,
is_disk_based: diagnostic.is_disk_based,
}, },
}) })
}) })

View file

@ -519,7 +519,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(), message: "undefined variable 'A'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 0, group_id: 0,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -530,7 +530,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(), message: "undefined variable 'BB'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 1, group_id: 1,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -540,7 +540,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
range: PointUtf16::new(2, 9)..PointUtf16::new(2, 12), range: PointUtf16::new(2, 9)..PointUtf16::new(2, 12),
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
source: Some("disk".to_string()), is_disk_based: true,
message: "undefined variable 'CCC'".to_string(), message: "undefined variable 'CCC'".to_string(),
group_id: 2, group_id: 2,
is_primary: true, is_primary: true,
@ -564,7 +564,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(), message: "undefined variable 'BB'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 1, group_id: 1,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -575,7 +575,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'CCC'".to_string(), message: "undefined variable 'CCC'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 2, group_id: 2,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -614,7 +614,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(), message: "undefined variable 'A'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 0, group_id: 0,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -655,7 +655,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(), message: "undefined variable 'A'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 0, group_id: 0,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -704,7 +704,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(), message: "undefined variable 'BB'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 1, group_id: 1,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -715,7 +715,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(), message: "undefined variable 'A'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 0, group_id: 0,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -736,7 +736,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'A'".to_string(), message: "undefined variable 'A'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 0, group_id: 0,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -747,7 +747,7 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(), message: "undefined variable 'BB'".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 4, group_id: 4,
is_primary: true, is_primary: true,
..Default::default() ..Default::default()
@ -798,7 +798,7 @@ async fn test_preserving_old_group_ids_and_disk_based_diagnostics(mut cx: gpui::
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "cannot find value `d` in this scope".to_string(), message: "cannot find value `d` in this scope".to_string(),
source: Some("disk".to_string()), is_disk_based: true,
group_id: 1, group_id: 1,
is_primary: true, is_primary: true,
is_valid: true, is_valid: true,

View file

@ -7,8 +7,8 @@ use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use client::{proto, Client, PeerId, TypedEnvelope, UserStore}; use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
use clock::ReplicaId; use clock::ReplicaId;
use collections::BTreeMap;
use collections::{hash_map, HashMap}; use collections::{hash_map, HashMap};
use collections::{BTreeMap, HashSet};
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use fuzzy::CharBag; use fuzzy::CharBag;
use gpui::{ use gpui::{
@ -675,6 +675,7 @@ impl Worktree {
pub fn update_diagnostics( pub fn update_diagnostics(
&mut self, &mut self,
mut params: lsp::PublishDiagnosticsParams, mut params: lsp::PublishDiagnosticsParams,
disk_based_sources: &HashSet<String>,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
) -> Result<()> { ) -> Result<()> {
let this = self.as_local_mut().ok_or_else(|| anyhow!("not local"))?; let this = self.as_local_mut().ok_or_else(|| anyhow!("not local"))?;
@ -712,7 +713,6 @@ impl Worktree {
range: diagnostic.range.start.to_point_utf16() range: diagnostic.range.start.to_point_utf16()
..diagnostic.range.end.to_point_utf16(), ..diagnostic.range.end.to_point_utf16(),
diagnostic: Diagnostic { diagnostic: Diagnostic {
source: diagnostic.source.clone(),
code: diagnostic.code.clone().map(|code| match code { code: diagnostic.code.clone().map(|code| match code {
lsp::NumberOrString::Number(code) => code.to_string(), lsp::NumberOrString::Number(code) => code.to_string(),
lsp::NumberOrString::String(code) => code, lsp::NumberOrString::String(code) => code,
@ -722,6 +722,10 @@ impl Worktree {
group_id, group_id,
is_primary: false, is_primary: false,
is_valid: true, is_valid: true,
is_disk_based: diagnostic
.source
.as_ref()
.map_or(false, |source| disk_based_sources.contains(source)),
}, },
}); });
} }
@ -1018,6 +1022,10 @@ impl LocalWorktree {
.log_err() .log_err()
.flatten() .flatten()
{ {
let disk_based_sources = language
.disk_based_diagnostic_sources()
.cloned()
.unwrap_or_default();
let (diagnostics_tx, diagnostics_rx) = smol::channel::unbounded(); let (diagnostics_tx, diagnostics_rx) = smol::channel::unbounded();
language_server language_server
.on_notification::<lsp::notification::PublishDiagnostics, _>(move |params| { .on_notification::<lsp::notification::PublishDiagnostics, _>(move |params| {
@ -1028,7 +1036,8 @@ impl LocalWorktree {
while let Ok(diagnostics) = diagnostics_rx.recv().await { while let Ok(diagnostics) = diagnostics_rx.recv().await {
if let Some(handle) = cx.read(|cx| this.upgrade(cx)) { if let Some(handle) = cx.read(|cx| this.upgrade(cx)) {
handle.update(&mut cx, |this, cx| { handle.update(&mut cx, |this, cx| {
this.update_diagnostics(diagnostics, cx).log_err(); this.update_diagnostics(diagnostics, &disk_based_sources, cx)
.log_err();
}); });
} else { } else {
break; break;
@ -3812,7 +3821,9 @@ mod tests {
}; };
worktree worktree
.update(&mut cx, |tree, cx| tree.update_diagnostics(message, cx)) .update(&mut cx, |tree, cx| {
tree.update_diagnostics(message, &Default::default(), cx)
})
.unwrap(); .unwrap();
let buffer = buffer.read_with(&cx, |buffer, _| buffer.snapshot()); let buffer = buffer.read_with(&cx, |buffer, _| buffer.snapshot());

View file

@ -304,10 +304,10 @@ message Diagnostic {
Severity severity = 3; Severity severity = 3;
string message = 4; string message = 4;
optional string code = 5; optional string code = 5;
optional string source = 6; uint64 group_id = 6;
uint64 group_id = 7; bool is_primary = 7;
bool is_primary = 8; bool is_valid = 8;
bool is_valid = 9; bool is_disk_based = 9;
enum Severity { enum Severity {
None = 0; None = 0;