about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2024-08-23 11:45:47 +0200
committerLukas Wirth <lukastw97@gmail.com>2024-08-23 11:45:47 +0200
commit5f7489a3d7dcf48a06ca70aea3c1941aa973c271 (patch)
tree5d51e5af5cb7558bf65b21a063e0495beda88cdd /src
parent39cc5b61f8b4432c127c667f806f8f60548199cd (diff)
downloadrust-5f7489a3d7dcf48a06ca70aea3c1941aa973c271.tar.gz
rust-5f7489a3d7dcf48a06ca70aea3c1941aa973c271.zip
internal: Don't requery crates_for for flycheck when crates are known
Diffstat (limited to 'src')
-rw-r--r--src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs1
-rw-r--r--src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs46
-rw-r--r--src/tools/rust-analyzer/crates/rust-analyzer/src/target_spec.rs1
3 files changed, 26 insertions, 22 deletions
diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs
index 8fcdfa5829a..afdce6cdf1a 100644
--- a/src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs
+++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs
@@ -696,6 +696,7 @@ impl GlobalStateSnapshot {
                     };
 
                     return Some(TargetSpec::ProjectJson(ProjectJsonTargetSpec {
+                        crate_id,
                         label: build.label,
                         target_kind: build.target_kind,
                         shell_runnables: project.runnables().to_owned(),
diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs
index f99319271ba..b08b5b1d725 100644
--- a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs
+++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs
@@ -10,6 +10,7 @@ use lsp_types::{
     DidOpenTextDocumentParams, DidSaveTextDocumentParams, WorkDoneProgressCancelParams,
 };
 use paths::Utf8PathBuf;
+use stdx::TupleExt;
 use triomphe::Arc;
 use vfs::{AbsPathBuf, ChangeKind, VfsPath};
 
@@ -290,11 +291,11 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
         let mut updated = false;
         let task = move || -> std::result::Result<(), ide::Cancelled> {
             // Is the target binary? If so we let flycheck run only for the workspace that contains the crate.
-            let target = TargetSpec::for_file(&world, file_id)?.and_then(|x| {
-                let tgt_kind = x.target_kind();
-                let tgt_name = match x {
-                    TargetSpec::Cargo(c) => c.target,
-                    TargetSpec::ProjectJson(p) => p.label,
+            let target = TargetSpec::for_file(&world, file_id)?.and_then(|it| {
+                let tgt_kind = it.target_kind();
+                let (tgt_name, crate_id) = match it {
+                    TargetSpec::Cargo(c) => (c.target, c.crate_id),
+                    TargetSpec::ProjectJson(p) => (p.label, p.crate_id),
                 };
 
                 let tgt = match tgt_kind {
@@ -305,25 +306,25 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
                     _ => return None,
                 };
 
-                Some(tgt)
+                Some((tgt, crate_id))
             });
 
-            let crate_ids = if target.is_some() {
-                // Trigger flychecks for the only workspace which the binary crate belongs to
-                world.analysis.crates_for(file_id)?.into_iter().unique().collect::<Vec<_>>()
-            } else {
-                // Trigger flychecks for all workspaces that depend on the saved file
-                // Crates containing or depending on the saved file
-                world
-                    .analysis
-                    .crates_for(file_id)?
-                    .into_iter()
-                    .flat_map(|id| world.analysis.transitive_rev_deps(id))
-                    .flatten()
-                    .unique()
-                    .collect::<Vec<_>>()
+            let crate_ids = match target {
+                // Trigger flychecks for the only crate which the target belongs to
+                Some((_, krate)) => vec![krate],
+                None => {
+                    // Trigger flychecks for all workspaces that depend on the saved file
+                    // Crates containing or depending on the saved file
+                    world
+                        .analysis
+                        .crates_for(file_id)?
+                        .into_iter()
+                        .flat_map(|id| world.analysis.transitive_rev_deps(id))
+                        .flatten()
+                        .unique()
+                        .collect::<Vec<_>>()
+                }
             };
-
             let crate_root_paths: Vec<_> = crate_ids
                 .iter()
                 .filter_map(|&crate_id| {
@@ -375,7 +376,8 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
                         match package
                             .filter(|_| !world.config.flycheck_workspace() || target.is_some())
                         {
-                            Some(package) => flycheck.restart_for_package(package, target.clone()),
+                            Some(package) => flycheck
+                                .restart_for_package(package, target.clone().map(TupleExt::head)),
                             None => flycheck.restart_workspace(saved_file.clone()),
                         }
                         continue;
diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/target_spec.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/target_spec.rs
index 965fd415e99..954e13cbf27 100644
--- a/src/tools/rust-analyzer/crates/rust-analyzer/src/target_spec.rs
+++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/target_spec.rs
@@ -62,6 +62,7 @@ pub(crate) struct CargoTargetSpec {
 
 #[derive(Clone, Debug)]
 pub(crate) struct ProjectJsonTargetSpec {
+    pub(crate) crate_id: CrateId,
     pub(crate) label: String,
     pub(crate) target_kind: TargetKind,
     pub(crate) shell_runnables: Vec<Runnable>,