Add support of runnables arguments in Rust Analyzer

This commit is contained in:
Igor Aleksanov 2020-09-05 12:52:27 +03:00
parent c01cd6e3ed
commit 2c125f3c63
4 changed files with 30 additions and 0 deletions

View File

@ -38,6 +38,7 @@ pub struct Config {
pub cargo: CargoConfig,
pub rustfmt: RustfmtConfig,
pub flycheck: Option<FlycheckConfig>,
pub runnables: RunnablesConfig,
pub inlay_hints: InlayHintsConfig,
pub completion: CompletionConfig,
@ -124,6 +125,15 @@ pub enum RustfmtConfig {
CustomCommand { command: String, args: Vec<String> },
}
/// Configuration for runnable items, such as `main` function or tests.
#[derive(Debug, Clone, Default)]
pub struct RunnablesConfig {
/// Stuff to be inserted before `cargo`, e.g. `RUST_LOG=info`.
pub cargo_prefix: Vec<String>,
/// Additional arguments for the `cargo`, e.g. `--release`.
pub cargo_extra_args: Vec<String>,
}
#[derive(Debug, Clone, Default)]
pub struct ClientCapsConfig {
pub location_link: bool,
@ -164,6 +174,7 @@ impl Config {
extra_args: Vec::new(),
features: Vec::new(),
}),
runnables: RunnablesConfig::default(),
inlay_hints: InlayHintsConfig {
type_hints: true,
@ -220,6 +231,10 @@ impl Config {
load_out_dirs_from_check: data.cargo_loadOutDirsFromCheck,
target: data.cargo_target.clone(),
};
self.runnables = RunnablesConfig {
cargo_prefix: data.runnables_cargoPrefix,
cargo_extra_args: data.runnables_cargoExtraArgs,
};
self.proc_macro_srv = if data.procMacro_enable {
std::env::current_exe().ok().map(|path| (path, vec!["proc-macro".into()]))
@ -474,6 +489,9 @@ config_data! {
notifications_cargoTomlNotFound: bool = true,
procMacro_enable: bool = false,
runnables_cargoPrefix: Vec<String> = Vec::new(),
runnables_cargoExtraArgs: Vec<String> = Vec::new(),
rustfmt_extraArgs: Vec<String> = Vec::new(),
rustfmt_overrideCommand: Option<Vec<String>> = None,

View File

@ -491,6 +491,7 @@ pub(crate) fn handle_runnables(
}
// Add `cargo check` and `cargo test` for all targets of the whole package
let config = &snap.config.runnables;
match cargo_spec {
Some(spec) => {
for &cmd in ["check", "test"].iter() {
@ -500,12 +501,14 @@ pub(crate) fn handle_runnables(
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::CargoRunnable {
workspace_root: Some(spec.workspace_root.clone().into()),
cargo_prefix: config.cargo_prefix.clone(),
cargo_args: vec![
cmd.to_string(),
"--package".to_string(),
spec.package.clone(),
"--all-targets".to_string(),
],
cargo_extra_args: config.cargo_extra_args.clone(),
executable_args: Vec::new(),
expect_test: None,
},
@ -519,7 +522,9 @@ pub(crate) fn handle_runnables(
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::CargoRunnable {
workspace_root: None,
cargo_prefix: config.cargo_prefix.clone(),
cargo_args: vec!["check".to_string(), "--workspace".to_string()],
cargo_extra_args: config.cargo_extra_args.clone(),
executable_args: Vec::new(),
expect_test: None,
},

View File

@ -171,10 +171,14 @@ pub enum RunnableKind {
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CargoRunnable {
// stuff before `cargo` command, e.g. `RUST_LOG=info`
pub cargo_prefix: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub workspace_root: Option<PathBuf>,
// command, --package and --lib stuff
pub cargo_args: Vec<String>,
// user-specified additional cargo args, like `--release`.
pub cargo_extra_args: Vec<String>,
// stuff after --
pub executable_args: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]

View File

@ -740,6 +740,7 @@ pub(crate) fn runnable(
file_id: FileId,
runnable: Runnable,
) -> Result<lsp_ext::Runnable> {
let config = &snap.config.runnables;
let spec = CargoTargetSpec::for_file(snap, file_id)?;
let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone());
let target = spec.as_ref().map(|s| s.target.clone());
@ -754,7 +755,9 @@ pub(crate) fn runnable(
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::CargoRunnable {
workspace_root: workspace_root.map(|it| it.into()),
cargo_prefix: config.cargo_prefix.clone(),
cargo_args,
cargo_extra_args: config.cargo_extra_args.clone(),
executable_args,
expect_test: None,
},