Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/flycheck/src/lib.rs')
| -rw-r--r-- | crates/flycheck/src/lib.rs | 104 |
1 files changed, 55 insertions, 49 deletions
diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs index 4ee86954ac..5dfaaf7742 100644 --- a/crates/flycheck/src/lib.rs +++ b/crates/flycheck/src/lib.rs @@ -42,18 +42,49 @@ pub enum InvocationLocation { } #[derive(Clone, Debug, PartialEq, Eq)] +pub struct CargoOptions { + pub target_triples: Vec<String>, + pub all_targets: bool, + pub no_default_features: bool, + pub all_features: bool, + pub features: Vec<String>, + pub extra_args: Vec<String>, + pub extra_env: FxHashMap<String, String>, + pub target_dir: Option<Utf8PathBuf>, +} + +impl CargoOptions { + fn apply_on_command(&self, cmd: &mut Command) { + for target in &self.target_triples { + cmd.args(["--target", target.as_str()]); + } + if self.all_targets { + cmd.arg("--all-targets"); + } + if self.all_features { + cmd.arg("--all-features"); + } else { + if self.no_default_features { + cmd.arg("--no-default-features"); + } + if !self.features.is_empty() { + cmd.arg("--features"); + cmd.arg(self.features.join(" ")); + } + } + if let Some(target_dir) = &self.target_dir { + cmd.arg("--target-dir").arg(target_dir); + } + cmd.envs(&self.extra_env); + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] pub enum FlycheckConfig { CargoCommand { command: String, - target_triples: Vec<String>, - all_targets: bool, - no_default_features: bool, - all_features: bool, - features: Vec<String>, - extra_args: Vec<String>, - extra_env: FxHashMap<String, String>, + options: CargoOptions, ansi_color_output: bool, - target_dir: Option<Utf8PathBuf>, }, CustomCommand { command: String, @@ -184,6 +215,8 @@ struct FlycheckActor { /// have to wrap sub-processes output handling in a thread and pass messages /// back over a channel. command_handle: Option<CommandHandle<CargoCheckMessage>>, + /// The receiver side of the channel mentioned above. + command_receiver: Option<Receiver<CargoCheckMessage>>, } enum Event { @@ -209,6 +242,7 @@ impl FlycheckActor { sysroot_root, root: workspace_root, command_handle: None, + command_receiver: None, } } @@ -217,14 +251,13 @@ impl FlycheckActor { } fn next_event(&self, inbox: &Receiver<StateChange>) -> Option<Event> { - let check_chan = self.command_handle.as_ref().map(|cargo| &cargo.receiver); if let Ok(msg) = inbox.try_recv() { // give restarts a preference so check outputs don't block a restart or stop return Some(Event::RequestStateChange(msg)); } select! { recv(inbox) -> msg => msg.ok().map(Event::RequestStateChange), - recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())), + recv(self.command_receiver.as_ref().unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())), } } @@ -253,10 +286,12 @@ impl FlycheckActor { let formatted_command = format!("{:?}", command); tracing::debug!(?command, "will restart flycheck"); - match CommandHandle::spawn(command) { + let (sender, receiver) = unbounded(); + match CommandHandle::spawn(command, sender) { Ok(command_handle) => { - tracing::debug!(command = formatted_command, "did restart flycheck"); + tracing::debug!(command = formatted_command, "did restart flycheck"); self.command_handle = Some(command_handle); + self.command_receiver = Some(receiver); self.report_progress(Progress::DidStart); } Err(error) => { @@ -272,13 +307,15 @@ impl FlycheckActor { // Watcher finished let command_handle = self.command_handle.take().unwrap(); + self.command_receiver.take(); let formatted_handle = format!("{:?}", command_handle); let res = command_handle.join(); - if res.is_err() { + if let Err(error) = &res { tracing::error!( - "Flycheck failed to run the following command: {}", - formatted_handle + "Flycheck failed to run the following command: {}, error={}", + formatted_handle, + error ); } self.report_progress(Progress::DidFinish(res)); @@ -332,18 +369,7 @@ impl FlycheckActor { saved_file: Option<&AbsPath>, ) -> Option<Command> { let (mut cmd, args) = match &self.config { - FlycheckConfig::CargoCommand { - command, - target_triples, - no_default_features, - all_targets, - all_features, - extra_args, - features, - extra_env, - ansi_color_output, - target_dir, - } => { + FlycheckConfig::CargoCommand { command, options, ansi_color_output } => { let mut cmd = Command::new(Tool::Cargo.path()); if let Some(sysroot_root) = &self.sysroot_root { cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(sysroot_root)); @@ -365,28 +391,8 @@ impl FlycheckActor { cmd.arg("--manifest-path"); cmd.arg(self.root.join("Cargo.toml")); - for target in target_triples { - cmd.args(["--target", target.as_str()]); - } - if *all_targets { - cmd.arg("--all-targets"); - } - if *all_features { - cmd.arg("--all-features"); - } else { - if *no_default_features { - cmd.arg("--no-default-features"); - } - if !features.is_empty() { - cmd.arg("--features"); - cmd.arg(features.join(" ")); - } - } - if let Some(target_dir) = target_dir { - cmd.arg("--target-dir").arg(target_dir); - } - cmd.envs(extra_env); - (cmd, extra_args.clone()) + options.apply_on_command(&mut cmd); + (cmd, options.extra_args.clone()) } FlycheckConfig::CustomCommand { command, |