Get diagnostics crate's tests passing

Update diagnostics on project instead of on worktree

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-01-21 18:02:10 -08:00
parent 2712cadaf6
commit 622aff3be2
2 changed files with 44 additions and 48 deletions

View file

@ -725,7 +725,6 @@ mod tests {
use gpui::TestAppContext; use gpui::TestAppContext;
use language::{Diagnostic, DiagnosticEntry, DiagnosticSeverity, PointUtf16}; use language::{Diagnostic, DiagnosticEntry, DiagnosticSeverity, PointUtf16};
use serde_json::json; use serde_json::json;
use std::sync::Arc;
use unindent::Unindent as _; use unindent::Unindent as _;
use workspace::WorkspaceParams; use workspace::WorkspaceParams;
@ -764,21 +763,19 @@ mod tests {
) )
.await; .await;
let worktree = project project
.update(&mut cx, |project, cx| { .update(&mut cx, |project, cx| {
project.add_local_worktree("/test", false, cx) project.add_local_worktree("/test", false, cx)
}) })
.await .await
.unwrap(); .unwrap();
let worktree_id = worktree.read_with(&cx, |tree, _| tree.id());
// Create some diagnostics // Create some diagnostics
worktree.update(&mut cx, |worktree, cx| { project.update(&mut cx, |project, cx| {
worktree project
.as_local_mut() .update_diagnostic_entries(
.unwrap() PathBuf::from("/test/main.rs"),
.update_diagnostics( None,
Arc::from("/test/main.rs".as_ref()),
vec![ vec![
DiagnosticEntry { DiagnosticEntry {
range: PointUtf16::new(1, 8)..PointUtf16::new(1, 9), range: PointUtf16::new(1, 8)..PointUtf16::new(1, 9),
@ -925,12 +922,12 @@ mod tests {
}); });
// Diagnostics are added for another earlier path. // Diagnostics are added for another earlier path.
worktree.update(&mut cx, |worktree, cx| { project.update(&mut cx, |project, cx| {
worktree project.disk_based_diagnostics_started(cx);
.as_local_mut() project
.unwrap() .update_diagnostic_entries(
.update_diagnostics( PathBuf::from("/test/consts.rs"),
Arc::from("/test/consts.rs".as_ref()), None,
vec![DiagnosticEntry { vec![DiagnosticEntry {
range: PointUtf16::new(0, 15)..PointUtf16::new(0, 15), range: PointUtf16::new(0, 15)..PointUtf16::new(0, 15),
diagnostic: Diagnostic { diagnostic: Diagnostic {
@ -945,13 +942,7 @@ mod tests {
cx, cx,
) )
.unwrap(); .unwrap();
}); project.disk_based_diagnostics_finished(cx);
project.update(&mut cx, |_, cx| {
cx.emit(project::Event::DiagnosticsUpdated(ProjectPath {
worktree_id,
path: Arc::from("/test/consts.rs".as_ref()),
}));
cx.emit(project::Event::DiskBasedDiagnosticsFinished);
}); });
view.next_notification(&cx).await; view.next_notification(&cx).await;
@ -1030,12 +1021,12 @@ mod tests {
}); });
// Diagnostics are added to the first path // Diagnostics are added to the first path
worktree.update(&mut cx, |worktree, cx| { project.update(&mut cx, |project, cx| {
worktree project.disk_based_diagnostics_started(cx);
.as_local_mut() project
.unwrap() .update_diagnostic_entries(
.update_diagnostics( PathBuf::from("/test/consts.rs"),
Arc::from("/test/consts.rs".as_ref()), None,
vec![ vec![
DiagnosticEntry { DiagnosticEntry {
range: PointUtf16::new(0, 15)..PointUtf16::new(0, 15), range: PointUtf16::new(0, 15)..PointUtf16::new(0, 15),
@ -1064,13 +1055,7 @@ mod tests {
cx, cx,
) )
.unwrap(); .unwrap();
}); project.disk_based_diagnostics_finished(cx);
project.update(&mut cx, |_, cx| {
cx.emit(project::Event::DiagnosticsUpdated(ProjectPath {
worktree_id,
path: Arc::from("/test/consts.rs".as_ref()),
}));
cx.emit(project::Event::DiskBasedDiagnosticsFinished);
}); });
view.next_notification(&cx).await; view.next_notification(&cx).await;

View file

@ -14,7 +14,7 @@ use gpui::{
}; };
use language::{ use language::{
range_from_lsp, Bias, Buffer, Diagnostic, DiagnosticEntry, File as _, Language, range_from_lsp, Bias, Buffer, Diagnostic, DiagnosticEntry, File as _, Language,
LanguageRegistry, Operation, ToOffset, ToPointUtf16, LanguageRegistry, Operation, PointUtf16, ToOffset, ToPointUtf16,
}; };
use lsp::{DiagnosticSeverity, LanguageServer}; use lsp::{DiagnosticSeverity, LanguageServer};
use postage::{prelude::Stream, watch}; use postage::{prelude::Stream, watch};
@ -787,17 +787,10 @@ impl Project {
disk_based_sources: &HashSet<String>, disk_based_sources: &HashSet<String>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Result<()> { ) -> Result<()> {
let path = params let abs_path = params
.uri .uri
.to_file_path() .to_file_path()
.map_err(|_| anyhow!("URI is not a file"))?; .map_err(|_| anyhow!("URI is not a file"))?;
let (worktree, relative_path) = self
.find_worktree_for_abs_path(&path, cx)
.ok_or_else(|| anyhow!("no worktree found for diagnostics"))?;
let project_path = ProjectPath {
worktree_id: worktree.read(cx).id(),
path: relative_path.into(),
};
let mut next_group_id = 0; let mut next_group_id = 0;
let mut diagnostics = Vec::default(); let mut diagnostics = Vec::default();
let mut primary_diagnostic_group_ids = HashMap::default(); let mut primary_diagnostic_group_ids = HashMap::default();
@ -885,6 +878,25 @@ impl Project {
} }
} }
self.update_diagnostic_entries(abs_path, params.version, diagnostics, cx)?;
Ok(())
}
pub fn update_diagnostic_entries(
&mut self,
abs_path: PathBuf,
version: Option<i32>,
diagnostics: Vec<DiagnosticEntry<PointUtf16>>,
cx: &mut ModelContext<Project>,
) -> Result<(), anyhow::Error> {
let (worktree, relative_path) = self
.find_worktree_for_abs_path(&abs_path, cx)
.ok_or_else(|| anyhow!("no worktree found for diagnostics"))?;
let project_path = ProjectPath {
worktree_id: worktree.read(cx).id(),
path: relative_path.into(),
};
for buffer in self.open_buffers.values() { for buffer in self.open_buffers.values() {
if let Some(buffer) = buffer.upgrade(cx) { if let Some(buffer) = buffer.upgrade(cx) {
if buffer if buffer
@ -893,13 +905,12 @@ impl Project {
.map_or(false, |file| *file.path() == project_path.path) .map_or(false, |file| *file.path() == project_path.path)
{ {
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.update_diagnostics(params.version, diagnostics.clone(), cx) buffer.update_diagnostics(version, diagnostics.clone(), cx)
})?; })?;
break; break;
} }
} }
} }
worktree.update(cx, |worktree, cx| { worktree.update(cx, |worktree, cx| {
worktree worktree
.as_local_mut() .as_local_mut()
@ -1268,14 +1279,14 @@ impl Project {
}) })
} }
fn disk_based_diagnostics_started(&mut self, cx: &mut ModelContext<Self>) { pub fn disk_based_diagnostics_started(&mut self, cx: &mut ModelContext<Self>) {
self.language_servers_with_diagnostics_running += 1; self.language_servers_with_diagnostics_running += 1;
if self.language_servers_with_diagnostics_running == 1 { if self.language_servers_with_diagnostics_running == 1 {
cx.emit(Event::DiskBasedDiagnosticsStarted); cx.emit(Event::DiskBasedDiagnosticsStarted);
} }
} }
fn disk_based_diagnostics_finished(&mut self, cx: &mut ModelContext<Self>) { pub fn disk_based_diagnostics_finished(&mut self, cx: &mut ModelContext<Self>) {
cx.emit(Event::DiskBasedDiagnosticsUpdated); cx.emit(Event::DiskBasedDiagnosticsUpdated);
self.language_servers_with_diagnostics_running -= 1; self.language_servers_with_diagnostics_running -= 1;
if self.language_servers_with_diagnostics_running == 0 { if self.language_servers_with_diagnostics_running == 0 {