Skip to content

Commit 2c125f3

Browse files
committed
Add support of runnables arguments in Rust Analyzer
1 parent c01cd6e commit 2c125f3

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct Config {
3838
pub cargo: CargoConfig,
3939
pub rustfmt: RustfmtConfig,
4040
pub flycheck: Option<FlycheckConfig>,
41+
pub runnables: RunnablesConfig,
4142

4243
pub inlay_hints: InlayHintsConfig,
4344
pub completion: CompletionConfig,
@@ -124,6 +125,15 @@ pub enum RustfmtConfig {
124125
CustomCommand { command: String, args: Vec<String> },
125126
}
126127

128+
/// Configuration for runnable items, such as `main` function or tests.
129+
#[derive(Debug, Clone, Default)]
130+
pub struct RunnablesConfig {
131+
/// Stuff to be inserted before `cargo`, e.g. `RUST_LOG=info`.
132+
pub cargo_prefix: Vec<String>,
133+
/// Additional arguments for the `cargo`, e.g. `--release`.
134+
pub cargo_extra_args: Vec<String>,
135+
}
136+
127137
#[derive(Debug, Clone, Default)]
128138
pub struct ClientCapsConfig {
129139
pub location_link: bool,
@@ -164,6 +174,7 @@ impl Config {
164174
extra_args: Vec::new(),
165175
features: Vec::new(),
166176
}),
177+
runnables: RunnablesConfig::default(),
167178

168179
inlay_hints: InlayHintsConfig {
169180
type_hints: true,
@@ -220,6 +231,10 @@ impl Config {
220231
load_out_dirs_from_check: data.cargo_loadOutDirsFromCheck,
221232
target: data.cargo_target.clone(),
222233
};
234+
self.runnables = RunnablesConfig {
235+
cargo_prefix: data.runnables_cargoPrefix,
236+
cargo_extra_args: data.runnables_cargoExtraArgs,
237+
};
223238

224239
self.proc_macro_srv = if data.procMacro_enable {
225240
std::env::current_exe().ok().map(|path| (path, vec!["proc-macro".into()]))
@@ -474,6 +489,9 @@ config_data! {
474489
notifications_cargoTomlNotFound: bool = true,
475490
procMacro_enable: bool = false,
476491

492+
runnables_cargoPrefix: Vec<String> = Vec::new(),
493+
runnables_cargoExtraArgs: Vec<String> = Vec::new(),
494+
477495
rustfmt_extraArgs: Vec<String> = Vec::new(),
478496
rustfmt_overrideCommand: Option<Vec<String>> = None,
479497

crates/rust-analyzer/src/handlers.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ pub(crate) fn handle_runnables(
491491
}
492492

493493
// Add `cargo check` and `cargo test` for all targets of the whole package
494+
let config = &snap.config.runnables;
494495
match cargo_spec {
495496
Some(spec) => {
496497
for &cmd in ["check", "test"].iter() {
@@ -500,12 +501,14 @@ pub(crate) fn handle_runnables(
500501
kind: lsp_ext::RunnableKind::Cargo,
501502
args: lsp_ext::CargoRunnable {
502503
workspace_root: Some(spec.workspace_root.clone().into()),
504+
cargo_prefix: config.cargo_prefix.clone(),
503505
cargo_args: vec![
504506
cmd.to_string(),
505507
"--package".to_string(),
506508
spec.package.clone(),
507509
"--all-targets".to_string(),
508510
],
511+
cargo_extra_args: config.cargo_extra_args.clone(),
509512
executable_args: Vec::new(),
510513
expect_test: None,
511514
},
@@ -519,7 +522,9 @@ pub(crate) fn handle_runnables(
519522
kind: lsp_ext::RunnableKind::Cargo,
520523
args: lsp_ext::CargoRunnable {
521524
workspace_root: None,
525+
cargo_prefix: config.cargo_prefix.clone(),
522526
cargo_args: vec!["check".to_string(), "--workspace".to_string()],
527+
cargo_extra_args: config.cargo_extra_args.clone(),
523528
executable_args: Vec::new(),
524529
expect_test: None,
525530
},

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ pub enum RunnableKind {
171171
#[derive(Deserialize, Serialize, Debug)]
172172
#[serde(rename_all = "camelCase")]
173173
pub struct CargoRunnable {
174+
// stuff before `cargo` command, e.g. `RUST_LOG=info`
175+
pub cargo_prefix: Vec<String>,
174176
#[serde(skip_serializing_if = "Option::is_none")]
175177
pub workspace_root: Option<PathBuf>,
176178
// command, --package and --lib stuff
177179
pub cargo_args: Vec<String>,
180+
// user-specified additional cargo args, like `--release`.
181+
pub cargo_extra_args: Vec<String>,
178182
// stuff after --
179183
pub executable_args: Vec<String>,
180184
#[serde(skip_serializing_if = "Option::is_none")]

crates/rust-analyzer/src/to_proto.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ pub(crate) fn runnable(
740740
file_id: FileId,
741741
runnable: Runnable,
742742
) -> Result<lsp_ext::Runnable> {
743+
let config = &snap.config.runnables;
743744
let spec = CargoTargetSpec::for_file(snap, file_id)?;
744745
let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone());
745746
let target = spec.as_ref().map(|s| s.target.clone());
@@ -754,7 +755,9 @@ pub(crate) fn runnable(
754755
kind: lsp_ext::RunnableKind::Cargo,
755756
args: lsp_ext::CargoRunnable {
756757
workspace_root: workspace_root.map(|it| it.into()),
758+
cargo_prefix: config.cargo_prefix.clone(),
757759
cargo_args,
760+
cargo_extra_args: config.cargo_extra_args.clone(),
758761
executable_args,
759762
expect_test: None,
760763
},

0 commit comments

Comments
 (0)