Skip to content

Commit 806d54c

Browse files
committed
internal: Don't requery crates_for for flycheck when crates are known
1 parent 2025b43 commit 806d54c

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

crates/rust-analyzer/src/global_state.rs

+1
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ impl GlobalStateSnapshot {
696696
};
697697

698698
return Some(TargetSpec::ProjectJson(ProjectJsonTargetSpec {
699+
crate_id,
699700
label: build.label,
700701
target_kind: build.target_kind,
701702
shell_runnables: project.runnables().to_owned(),

crates/rust-analyzer/src/handlers/notification.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use lsp_types::{
1010
DidOpenTextDocumentParams, DidSaveTextDocumentParams, WorkDoneProgressCancelParams,
1111
};
1212
use paths::Utf8PathBuf;
13+
use stdx::TupleExt;
1314
use triomphe::Arc;
1415
use vfs::{AbsPathBuf, ChangeKind, VfsPath};
1516

@@ -290,11 +291,11 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
290291
let mut updated = false;
291292
let task = move || -> std::result::Result<(), ide::Cancelled> {
292293
// Is the target binary? If so we let flycheck run only for the workspace that contains the crate.
293-
let target = TargetSpec::for_file(&world, file_id)?.and_then(|x| {
294-
let tgt_kind = x.target_kind();
295-
let tgt_name = match x {
296-
TargetSpec::Cargo(c) => c.target,
297-
TargetSpec::ProjectJson(p) => p.label,
294+
let target = TargetSpec::for_file(&world, file_id)?.and_then(|it| {
295+
let tgt_kind = it.target_kind();
296+
let (tgt_name, crate_id) = match it {
297+
TargetSpec::Cargo(c) => (c.target, c.crate_id),
298+
TargetSpec::ProjectJson(p) => (p.label, p.crate_id),
298299
};
299300

300301
let tgt = match tgt_kind {
@@ -305,25 +306,25 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
305306
_ => return None,
306307
};
307308

308-
Some(tgt)
309+
Some((tgt, crate_id))
309310
});
310311

311-
let crate_ids = if target.is_some() {
312-
// Trigger flychecks for the only workspace which the binary crate belongs to
313-
world.analysis.crates_for(file_id)?.into_iter().unique().collect::<Vec<_>>()
314-
} else {
315-
// Trigger flychecks for all workspaces that depend on the saved file
316-
// Crates containing or depending on the saved file
317-
world
318-
.analysis
319-
.crates_for(file_id)?
320-
.into_iter()
321-
.flat_map(|id| world.analysis.transitive_rev_deps(id))
322-
.flatten()
323-
.unique()
324-
.collect::<Vec<_>>()
312+
let crate_ids = match target {
313+
// Trigger flychecks for the only crate which the target belongs to
314+
Some((_, krate)) => vec![krate],
315+
None => {
316+
// Trigger flychecks for all workspaces that depend on the saved file
317+
// Crates containing or depending on the saved file
318+
world
319+
.analysis
320+
.crates_for(file_id)?
321+
.into_iter()
322+
.flat_map(|id| world.analysis.transitive_rev_deps(id))
323+
.flatten()
324+
.unique()
325+
.collect::<Vec<_>>()
326+
}
325327
};
326-
327328
let crate_root_paths: Vec<_> = crate_ids
328329
.iter()
329330
.filter_map(|&crate_id| {
@@ -375,7 +376,8 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
375376
match package
376377
.filter(|_| !world.config.flycheck_workspace() || target.is_some())
377378
{
378-
Some(package) => flycheck.restart_for_package(package, target.clone()),
379+
Some(package) => flycheck
380+
.restart_for_package(package, target.clone().map(TupleExt::head)),
379381
None => flycheck.restart_workspace(saved_file.clone()),
380382
}
381383
continue;

crates/rust-analyzer/src/target_spec.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub(crate) struct CargoTargetSpec {
6262

6363
#[derive(Clone, Debug)]
6464
pub(crate) struct ProjectJsonTargetSpec {
65+
pub(crate) crate_id: CrateId,
6566
pub(crate) label: String,
6667
pub(crate) target_kind: TargetKind,
6768
pub(crate) shell_runnables: Vec<Runnable>,

0 commit comments

Comments
 (0)