Skip to content

Commit 03456c5

Browse files
committed
Apply changes
1 parent ffc3bfe commit 03456c5

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

crates/rust-analyzer/src/flycheck.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ pub(crate) struct CargoOptions {
3434
pub(crate) target_dir: Option<Utf8PathBuf>,
3535
}
3636

37+
#[derive(Clone)]
38+
pub(crate) enum Target {
39+
Bin(String),
40+
Example(String),
41+
Benchmark(String),
42+
Test(String),
43+
}
44+
3745
impl CargoOptions {
3846
pub(crate) fn apply_on_command(&self, cmd: &mut Command) {
3947
for target in &self.target_triples {
@@ -118,12 +126,12 @@ impl FlycheckHandle {
118126
}
119127

120128
/// Schedule a re-start of the cargo check worker to do a workspace wide check.
121-
pub(crate) fn restart_workspace(&self, saved_file: Option<AbsPathBuf>, target: Option<String>) {
122-
self.sender.send(StateChange::Restart { package: None, saved_file, target }).unwrap();
129+
pub(crate) fn restart_workspace(&self, saved_file: Option<AbsPathBuf>) {
130+
self.sender.send(StateChange::Restart { package: None, saved_file, target: None }).unwrap();
123131
}
124132

125133
/// Schedule a re-start of the cargo check worker to do a package wide check.
126-
pub(crate) fn restart_for_package(&self, package: String, target: Option<String>) {
134+
pub(crate) fn restart_for_package(&self, package: String, target: Option<Target>) {
127135
self.sender
128136
.send(StateChange::Restart { package: Some(package), saved_file: None, target })
129137
.unwrap();
@@ -183,7 +191,7 @@ pub(crate) enum Progress {
183191
}
184192

185193
enum StateChange {
186-
Restart { package: Option<String>, saved_file: Option<AbsPathBuf>, target: Option<String> },
194+
Restart { package: Option<String>, saved_file: Option<AbsPathBuf>, target: Option<Target> },
187195
Cancel,
188196
}
189197

@@ -281,14 +289,12 @@ impl FlycheckActor {
281289
}
282290
}
283291

284-
let command = match self.check_command(
285-
package.as_deref(),
286-
saved_file.as_deref(),
287-
target.as_deref(),
288-
) {
289-
Some(c) => c,
290-
None => continue,
292+
let Some(command) =
293+
self.check_command(package.as_deref(), saved_file.as_deref(), target)
294+
else {
295+
continue;
291296
};
297+
292298
let formatted_command = format!("{command:?}");
293299

294300
tracing::debug!(?command, "will restart flycheck");
@@ -384,7 +390,7 @@ impl FlycheckActor {
384390
&self,
385391
package: Option<&str>,
386392
saved_file: Option<&AbsPath>,
387-
bin_target: Option<&str>,
393+
target: Option<Target>,
388394
) -> Option<Command> {
389395
match &self.config {
390396
FlycheckConfig::CargoCommand { command, options, ansi_color_output } => {
@@ -400,8 +406,13 @@ impl FlycheckActor {
400406
None => cmd.arg("--workspace"),
401407
};
402408

403-
if let Some(tgt) = bin_target {
404-
cmd.arg("--bin").arg(tgt);
409+
if let Some(tgt) = target {
410+
match tgt {
411+
Target::Bin(tgt) => cmd.arg("--bin").arg(tgt),
412+
Target::Example(tgt) => cmd.arg("--example").arg(tgt),
413+
Target::Test(tgt) => cmd.arg("--test").arg(tgt),
414+
Target::Benchmark(tgt) => cmd.arg("--bench").arg(tgt),
415+
};
405416
}
406417

407418
cmd.arg(if *ansi_color_output {

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

+25-18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use vfs::{AbsPathBuf, ChangeKind, VfsPath};
1515

1616
use crate::{
1717
config::{Config, ConfigChange},
18+
flycheck::Target,
1819
global_state::{FetchWorkspaceRequest, GlobalState},
1920
lsp::{from_proto, utils::apply_document_changes},
2021
lsp_ext::{self, RunFlycheckParams},
@@ -186,7 +187,7 @@ pub(crate) fn handle_did_save_text_document(
186187
} else if state.config.check_on_save() {
187188
// No specific flycheck was triggered, so let's trigger all of them.
188189
for flycheck in state.flycheck.iter() {
189-
flycheck.restart_workspace(None, None);
190+
flycheck.restart_workspace(None);
190191
}
191192
}
192193
Ok(())
@@ -289,18 +290,25 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
289290
let mut updated = false;
290291
let task = move || -> std::result::Result<(), ide::Cancelled> {
291292
// Is the target binary? If so we let flycheck run only for the workspace that contains the crate.
292-
let target_is_bin = TargetSpec::for_file(&world, file_id)?.and_then(|x| {
293-
if x.target_kind() == project_model::TargetKind::Bin {
294-
return match x {
295-
TargetSpec::Cargo(c) => Some(c.target),
296-
TargetSpec::ProjectJson(p) => Some(p.label),
297-
};
298-
}
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,
298+
};
299+
300+
let tgt = match tgt_kind {
301+
project_model::TargetKind::Bin => Target::Bin(tgt_name),
302+
project_model::TargetKind::Example => Target::Example(tgt_name),
303+
project_model::TargetKind::Test => Target::Test(tgt_name),
304+
project_model::TargetKind::Bench => Target::Benchmark(tgt_name),
305+
_ => return None,
306+
};
299307

300-
None
308+
Some(tgt)
301309
});
302310

303-
let crate_ids = if target_is_bin.is_some() {
311+
let crate_ids = if target.is_some() {
304312
// Trigger flychecks for the only workspace which the binary crate belongs to
305313
world.analysis.crates_for(file_id)?.into_iter().unique().collect::<Vec<_>>()
306314
} else {
@@ -364,12 +372,11 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
364372
for (id, package) in workspace_ids.clone() {
365373
if id == flycheck.id() {
366374
updated = true;
367-
match package.filter(|_| !world.config.flycheck_workspace()) {
368-
Some(package) => {
369-
flycheck.restart_for_package(package, target_is_bin.clone())
370-
}
371-
None => flycheck
372-
.restart_workspace(saved_file.clone(), target_is_bin.clone()),
375+
match package
376+
.filter(|_| !world.config.flycheck_workspace() || target.is_some())
377+
{
378+
Some(package) => flycheck.restart_for_package(package, target.clone()),
379+
None => flycheck.restart_workspace(saved_file.clone()),
373380
}
374381
continue;
375382
}
@@ -378,7 +385,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
378385
// No specific flycheck was triggered, so let's trigger all of them.
379386
if !updated {
380387
for flycheck in world.flycheck.iter() {
381-
flycheck.restart_workspace(saved_file.clone(), None);
388+
flycheck.restart_workspace(saved_file.clone());
382389
}
383390
}
384391
Ok(())
@@ -420,7 +427,7 @@ pub(crate) fn handle_run_flycheck(
420427
}
421428
// No specific flycheck was triggered, so let's trigger all of them.
422429
for flycheck in state.flycheck.iter() {
423-
flycheck.restart_workspace(None, None);
430+
flycheck.restart_workspace(None);
424431
}
425432
Ok(())
426433
}

crates/rust-analyzer/src/main_loop.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ impl GlobalState {
406406
if became_quiescent {
407407
if self.config.check_on_save() {
408408
// Project has loaded properly, kick off initial flycheck
409-
self.flycheck
410-
.iter()
411-
.for_each(|flycheck| flycheck.restart_workspace(None, None));
409+
self.flycheck.iter().for_each(|flycheck| flycheck.restart_workspace(None));
412410
}
413411
if self.config.prefill_caches() {
414412
self.prime_caches_queue.request_op("became quiescent".to_owned(), ());

0 commit comments

Comments
 (0)