Skip to content

Commit a63b5d3

Browse files
committed
feat: Only flycheck workspace that belongs to saved file
1 parent ea41617 commit a63b5d3

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

crates/flycheck/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct FlycheckHandle {
5555
// XXX: drop order is significant
5656
sender: Sender<Restart>,
5757
_thread: jod_thread::JoinHandle,
58+
id: usize,
5859
}
5960

6061
impl FlycheckHandle {
@@ -70,13 +71,17 @@ impl FlycheckHandle {
7071
.name("Flycheck".to_owned())
7172
.spawn(move || actor.run(receiver))
7273
.expect("failed to spawn thread");
73-
FlycheckHandle { sender, _thread: thread }
74+
FlycheckHandle { id, sender, _thread: thread }
7475
}
7576

7677
/// Schedule a re-start of the cargo check worker.
7778
pub fn update(&self) {
7879
self.sender.send(Restart).unwrap();
7980
}
81+
82+
pub fn id(&self) -> usize {
83+
self.id
84+
}
8085
}
8186

8287
pub enum Message {

crates/paths/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ impl AsRef<Path> for AbsPath {
103103
}
104104
}
105105

106+
impl ToOwned for AbsPath {
107+
type Owned = AbsPathBuf;
108+
109+
fn to_owned(&self) -> Self::Owned {
110+
AbsPathBuf(self.0.to_owned())
111+
}
112+
}
113+
106114
impl<'a> TryFrom<&'a Path> for &'a AbsPath {
107115
type Error = &'a Path;
108116
fn try_from(path: &'a Path) -> Result<&'a AbsPath, &'a Path> {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! requests/replies and notifications back to the client.
33
use std::{
44
fmt,
5+
ops::Deref,
56
sync::Arc,
67
time::{Duration, Instant},
78
};
@@ -720,13 +721,62 @@ impl GlobalState {
720721
Ok(())
721722
})?
722723
.on::<lsp_types::notification::DidSaveTextDocument>(|this, params| {
723-
for flycheck in &this.flycheck {
724-
flycheck.update();
724+
let mut updated = false;
725+
if let Ok(vfs_path) = from_proto::vfs_path(&params.text_document.uri) {
726+
let (vfs, _) = &*this.vfs.read();
727+
if let Some(file_id) = vfs.file_id(&vfs_path) {
728+
let analysis = this.analysis_host.analysis();
729+
let crate_ids = analysis.crate_for(file_id)?;
730+
731+
let paths: Vec<_> = crate_ids
732+
.iter()
733+
.filter_map(|&crate_id| {
734+
analysis
735+
.crate_root(crate_id)
736+
.map(|file_id| {
737+
vfs.file_path(file_id).as_path().map(ToOwned::to_owned)
738+
})
739+
.transpose()
740+
})
741+
.collect::<ide::Cancellable<_>>()?;
742+
let paths: Vec<_> = paths.iter().map(Deref::deref).collect();
743+
744+
let workspace_ids =
745+
this.workspaces.iter().enumerate().filter(|(_, ws)| match ws {
746+
project_model::ProjectWorkspace::Cargo { cargo, .. } => {
747+
cargo.packages().filter(|&pkg| cargo[pkg].is_member).any(
748+
|pkg| {
749+
cargo[pkg].targets.iter().any(|&it| {
750+
paths.contains(&cargo[it].root.as_path())
751+
})
752+
},
753+
)
754+
}
755+
project_model::ProjectWorkspace::Json { project, .. } => project
756+
.crates()
757+
.any(|(c, _)| crate_ids.iter().any(|&crate_id| crate_id == c)),
758+
project_model::ProjectWorkspace::DetachedFiles { .. } => false,
759+
});
760+
'workspace: for (id, _) in workspace_ids {
761+
for flycheck in &this.flycheck {
762+
if id == flycheck.id() {
763+
updated = true;
764+
flycheck.update();
765+
continue 'workspace;
766+
}
767+
}
768+
}
769+
}
770+
if let Some(abs_path) = vfs_path.as_path() {
771+
if reload::should_refresh_for_change(&abs_path, ChangeKind::Modify) {
772+
this.fetch_workspaces_queue
773+
.request_op(format!("DidSaveTextDocument {}", abs_path.display()));
774+
}
775+
}
725776
}
726-
if let Ok(abs_path) = from_proto::abs_path(&params.text_document.uri) {
727-
if reload::should_refresh_for_change(&abs_path, ChangeKind::Modify) {
728-
this.fetch_workspaces_queue
729-
.request_op(format!("DidSaveTextDocument {}", abs_path.display()));
777+
if !updated {
778+
for flycheck in &this.flycheck {
779+
flycheck.update();
730780
}
731781
}
732782
Ok(())

0 commit comments

Comments
 (0)