Omit worktree id when emitting Event::DiskBasedDiagnosticsUpdated

Sometimes we will have more than one worktree associated with the same
language server and in that case it's unclear which worktree id we should
report an event for.
This commit is contained in:
Antonio Scandurra 2022-01-20 10:13:27 +01:00
parent 71082d4cdc
commit 6b1f989c2b
4 changed files with 30 additions and 53 deletions

View file

@ -14,7 +14,7 @@ use gpui::{
}; };
use language::{Bias, Buffer, Diagnostic, DiagnosticEntry, Point, Selection, SelectionGoal}; use language::{Bias, Buffer, Diagnostic, DiagnosticEntry, Point, Selection, SelectionGoal};
use postage::watch; use postage::watch;
use project::{Project, ProjectPath, WorktreeId}; use project::{Project, ProjectPath};
use std::{cmp::Ordering, mem, ops::Range, path::PathBuf, rc::Rc, sync::Arc}; use std::{cmp::Ordering, mem, ops::Range, path::PathBuf, rc::Rc, sync::Arc};
use util::TryFutureExt; use util::TryFutureExt;
use workspace::{NavHistory, Workspace}; use workspace::{NavHistory, Workspace};
@ -49,7 +49,7 @@ struct ProjectDiagnosticsEditor {
editor: ViewHandle<Editor>, editor: ViewHandle<Editor>,
excerpts: ModelHandle<MultiBuffer>, excerpts: ModelHandle<MultiBuffer>,
path_states: Vec<PathState>, path_states: Vec<PathState>,
paths_to_update: HashMap<WorktreeId, BTreeSet<ProjectPath>>, paths_to_update: BTreeSet<ProjectPath>,
build_settings: BuildSettings, build_settings: BuildSettings,
settings: watch::Receiver<workspace::Settings>, settings: watch::Receiver<workspace::Settings>,
} }
@ -119,16 +119,12 @@ impl ProjectDiagnosticsEditor {
) -> Self { ) -> Self {
let project = model.read(cx).project.clone(); let project = model.read(cx).project.clone();
cx.subscribe(&project, |this, _, event, cx| match event { cx.subscribe(&project, |this, _, event, cx| match event {
project::Event::DiskBasedDiagnosticsUpdated { worktree_id } => { project::Event::DiskBasedDiagnosticsFinished => {
if let Some(paths) = this.paths_to_update.remove(&worktree_id) { let paths = mem::take(&mut this.paths_to_update);
this.update_excerpts(paths, cx); this.update_excerpts(paths, cx);
}
} }
project::Event::DiagnosticsUpdated(path) => { project::Event::DiagnosticsUpdated(path) => {
this.paths_to_update this.paths_to_update.insert(path.clone());
.entry(path.worktree_id)
.or_default()
.insert(path.clone());
} }
_ => {} _ => {}
}) })
@ -909,7 +905,7 @@ mod tests {
worktree_id, worktree_id,
path: Arc::from("/test/consts.rs".as_ref()), path: Arc::from("/test/consts.rs".as_ref()),
})); }));
cx.emit(project::Event::DiskBasedDiagnosticsUpdated { worktree_id }); cx.emit(project::Event::DiskBasedDiagnosticsFinished);
}); });
view.next_notification(&cx).await; view.next_notification(&cx).await;
@ -1029,7 +1025,7 @@ mod tests {
worktree_id, worktree_id,
path: Arc::from("/test/consts.rs".as_ref()), path: Arc::from("/test/consts.rs".as_ref()),
})); }));
cx.emit(project::Event::DiskBasedDiagnosticsUpdated { worktree_id }); cx.emit(project::Event::DiskBasedDiagnosticsFinished);
}); });
view.next_notification(&cx).await; view.next_notification(&cx).await;

View file

@ -19,7 +19,7 @@ impl DiagnosticSummary {
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
cx.subscribe(project, |this, project, event, cx| match event { cx.subscribe(project, |this, project, event, cx| match event {
project::Event::DiskBasedDiagnosticsUpdated { .. } => { project::Event::DiskBasedDiagnosticsUpdated => {
this.summary = project.read(cx).diagnostic_summary(cx); this.summary = project.read(cx).diagnostic_summary(cx);
cx.notify(); cx.notify();
} }

View file

@ -64,7 +64,7 @@ pub enum Event {
ActiveEntryChanged(Option<ProjectEntry>), ActiveEntryChanged(Option<ProjectEntry>),
WorktreeRemoved(WorktreeId), WorktreeRemoved(WorktreeId),
DiskBasedDiagnosticsStarted, DiskBasedDiagnosticsStarted,
DiskBasedDiagnosticsUpdated { worktree_id: WorktreeId }, DiskBasedDiagnosticsUpdated,
DiskBasedDiagnosticsFinished, DiskBasedDiagnosticsFinished,
DiagnosticsUpdated(ProjectPath), DiagnosticsUpdated(ProjectPath),
} }
@ -527,14 +527,10 @@ impl Project {
.entry((worktree_id, language.name().to_string())) .entry((worktree_id, language.name().to_string()))
{ {
hash_map::Entry::Occupied(e) => Some(e.get().clone()), hash_map::Entry::Occupied(e) => Some(e.get().clone()),
hash_map::Entry::Vacant(e) => Self::start_language_server( hash_map::Entry::Vacant(e) => {
self.client.clone(), Self::start_language_server(self.client.clone(), language, &worktree_abs_path, cx)
language, .map(|server| e.insert(server).clone())
worktree_id, }
&worktree_abs_path,
cx,
)
.map(|server| e.insert(server).clone()),
}; };
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
@ -547,7 +543,6 @@ impl Project {
fn start_language_server( fn start_language_server(
rpc: Arc<Client>, rpc: Arc<Client>,
language: Arc<Language>, language: Arc<Language>,
worktree_id: WorktreeId,
worktree_path: &Path, worktree_path: &Path,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Option<Arc<LanguageServer>> { ) -> Option<Arc<LanguageServer>> {
@ -626,12 +621,9 @@ impl Project {
match message { match message {
LspEvent::DiagnosticsStart => { LspEvent::DiagnosticsStart => {
let send = this.update(&mut cx, |this, cx| { let send = this.update(&mut cx, |this, cx| {
this.disk_based_diagnostics_started(worktree_id, cx); this.disk_based_diagnostics_started(cx);
this.remote_id().map(|project_id| { this.remote_id().map(|project_id| {
rpc.send(proto::DiskBasedDiagnosticsUpdating { rpc.send(proto::DiskBasedDiagnosticsUpdating { project_id })
project_id,
worktree_id: worktree_id.to_proto(),
})
}) })
}); });
if let Some(send) = send { if let Some(send) = send {
@ -646,12 +638,9 @@ impl Project {
} }
LspEvent::DiagnosticsFinish => { LspEvent::DiagnosticsFinish => {
let send = this.update(&mut cx, |this, cx| { let send = this.update(&mut cx, |this, cx| {
this.disk_based_diagnostics_finished(worktree_id, cx); this.disk_based_diagnostics_finished(cx);
this.remote_id().map(|project_id| { this.remote_id().map(|project_id| {
rpc.send(proto::DiskBasedDiagnosticsUpdated { rpc.send(proto::DiskBasedDiagnosticsUpdated { project_id })
project_id,
worktree_id: worktree_id.to_proto(),
})
}) })
}); });
if let Some(send) = send { if let Some(send) = send {
@ -831,19 +820,15 @@ impl Project {
}) })
} }
fn disk_based_diagnostics_started(&mut self, _: WorktreeId, cx: &mut ModelContext<Self>) { 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( fn disk_based_diagnostics_finished(&mut self, cx: &mut ModelContext<Self>) {
&mut self, cx.emit(Event::DiskBasedDiagnosticsUpdated);
worktree_id: WorktreeId,
cx: &mut ModelContext<Self>,
) {
cx.emit(Event::DiskBasedDiagnosticsUpdated { worktree_id });
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 {
cx.emit(Event::DiskBasedDiagnosticsFinished); cx.emit(Event::DiskBasedDiagnosticsFinished);
@ -1010,27 +995,21 @@ impl Project {
fn handle_disk_based_diagnostics_updating( fn handle_disk_based_diagnostics_updating(
&mut self, &mut self,
envelope: TypedEnvelope<proto::DiskBasedDiagnosticsUpdating>, _: TypedEnvelope<proto::DiskBasedDiagnosticsUpdating>,
_: Arc<Client>, _: Arc<Client>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Result<()> { ) -> Result<()> {
self.disk_based_diagnostics_started( self.disk_based_diagnostics_started(cx);
WorktreeId::from_proto(envelope.payload.worktree_id),
cx,
);
Ok(()) Ok(())
} }
fn handle_disk_based_diagnostics_updated( fn handle_disk_based_diagnostics_updated(
&mut self, &mut self,
envelope: TypedEnvelope<proto::DiskBasedDiagnosticsUpdated>, _: TypedEnvelope<proto::DiskBasedDiagnosticsUpdated>,
_: Arc<Client>, _: Arc<Client>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Result<()> { ) -> Result<()> {
self.disk_based_diagnostics_finished( self.disk_based_diagnostics_finished(cx);
WorktreeId::from_proto(envelope.payload.worktree_id),
cx,
);
Ok(()) Ok(())
} }
@ -1491,7 +1470,11 @@ mod tests {
fake_server.end_progress(&progress_token).await; fake_server.end_progress(&progress_token).await;
assert_eq!( assert_eq!(
events.next().await.unwrap(), events.next().await.unwrap(),
Event::DiskBasedDiagnosticsUpdated { worktree_id } Event::DiskBasedDiagnosticsUpdated
);
assert_eq!(
events.next().await.unwrap(),
Event::DiskBasedDiagnosticsFinished
); );
let (buffer, _) = tree let (buffer, _) = tree

View file

@ -191,12 +191,10 @@ message DiagnosticSummary {
message DiskBasedDiagnosticsUpdating { message DiskBasedDiagnosticsUpdating {
uint64 project_id = 1; uint64 project_id = 1;
uint64 worktree_id = 2;
} }
message DiskBasedDiagnosticsUpdated { message DiskBasedDiagnosticsUpdated {
uint64 project_id = 1; uint64 project_id = 1;
uint64 worktree_id = 2;
} }
message GetChannels {} message GetChannels {}