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 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 util::TryFutureExt;
use workspace::{NavHistory, Workspace};
@ -49,7 +49,7 @@ struct ProjectDiagnosticsEditor {
editor: ViewHandle<Editor>,
excerpts: ModelHandle<MultiBuffer>,
path_states: Vec<PathState>,
paths_to_update: HashMap<WorktreeId, BTreeSet<ProjectPath>>,
paths_to_update: BTreeSet<ProjectPath>,
build_settings: BuildSettings,
settings: watch::Receiver<workspace::Settings>,
}
@ -119,16 +119,12 @@ impl ProjectDiagnosticsEditor {
) -> Self {
let project = model.read(cx).project.clone();
cx.subscribe(&project, |this, _, event, cx| match event {
project::Event::DiskBasedDiagnosticsUpdated { worktree_id } => {
if let Some(paths) = this.paths_to_update.remove(&worktree_id) {
this.update_excerpts(paths, cx);
}
project::Event::DiskBasedDiagnosticsFinished => {
let paths = mem::take(&mut this.paths_to_update);
this.update_excerpts(paths, cx);
}
project::Event::DiagnosticsUpdated(path) => {
this.paths_to_update
.entry(path.worktree_id)
.or_default()
.insert(path.clone());
this.paths_to_update.insert(path.clone());
}
_ => {}
})
@ -909,7 +905,7 @@ mod tests {
worktree_id,
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;
@ -1029,7 +1025,7 @@ mod tests {
worktree_id,
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 file

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

View file

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

View file

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