Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,6 @@ pub(crate) fn handle_runnables(
all_targets = if all_targets { " --all-targets" } else { "" }
),
location: None,
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
workspace_root: Some(spec.workspace_root.clone().into()),
cwd: cwd.into(),
Expand All @@ -1076,7 +1075,6 @@ pub(crate) fn handle_runnables(
res.push(lsp_ext::Runnable {
label: "cargo check --workspace".to_owned(),
location: None,
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
workspace_root: None,
cwd: path.as_path().unwrap().to_path_buf().into(),
Expand Down
103 changes: 93 additions & 10 deletions crates/rust-analyzer/src/lsp/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,25 +451,17 @@ pub struct Runnable {
pub label: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub location: Option<lsp_types::LocationLink>,
pub kind: RunnableKind,
#[serde(flatten)]
pub args: RunnableArgs,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
#[serde(tag = "kind", content = "args", rename_all = "lowercase")]
pub enum RunnableArgs {
Cargo(CargoRunnableArgs),
Shell(ShellRunnableArgs),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum RunnableKind {
Cargo,
Shell,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CargoRunnableArgs {
Expand Down Expand Up @@ -881,3 +873,94 @@ impl Request for GetFailedObligations {
type Result = String;
const METHOD: &'static str = "rust-analyzer/getFailedObligations";
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn cargo_runnable_round_trips() {
let runnable = Runnable {
label: "cargo test -p my-crate".to_owned(),
location: None,
args: RunnableArgs::Cargo(CargoRunnableArgs {
environment: [("RUSTC_TOOLCHAIN".to_owned(), "/toolchain".to_owned())]
.into_iter()
.collect(),
cwd: "/project".into(),
override_cargo: None,
workspace_root: Some("/project".into()),
cargo_args: vec![
"test".into(),
"--package".into(),
"my-crate".into(),
"--lib".into(),
],
executable_args: vec!["my_test".into(), "--exact".into()],
}),
};
let expected = json!({
"label": "cargo test -p my-crate",
"kind": "cargo",
"args": {
"environment": {"RUSTC_TOOLCHAIN": "/toolchain"},
"cwd": "/project",
"overrideCargo": null,
"workspaceRoot": "/project",
"cargoArgs": ["test", "--package", "my-crate", "--lib"],
"executableArgs": ["my_test", "--exact"],
}
});

let serialized = serde_json::to_value(&runnable).expect("serialized runnable");
assert_eq!(serialized, expected);

let deserialized: Runnable =
serde_json::from_value(expected).expect("cargo runnable should deserialize");
let RunnableArgs::Cargo(cargo) = &deserialized.args else {
panic!("expected Cargo variant, got {:?}", deserialized.args);
};
assert_eq!(cargo.cargo_args, vec!["test", "--package", "my-crate", "--lib"]);
assert_eq!(cargo.executable_args, vec!["my_test", "--exact"]);
}

#[test]
fn shell_runnable_round_trips() {
let runnable = Runnable {
label: "nextest test_one".to_owned(),
location: None,
args: RunnableArgs::Shell(ShellRunnableArgs {
environment: [("RUSTC_TOOLCHAIN".to_owned(), "/toolchain".to_owned())]
.into_iter()
.collect(),
cwd: "/project".into(),
program: "cargo".into(),
args: vec!["nextest".into(), "run".into(), "--package".into(), "my-crate".into()],
}),
};
let expected = json!({
"label": "nextest test_one",
"kind": "shell",
"args": {
"environment": {"RUSTC_TOOLCHAIN": "/toolchain"},
"cwd": "/project",
"program": "cargo",
"args": ["nextest", "run", "--package", "my-crate"],
}
});

let serialized = serde_json::to_value(&runnable).expect("serialized runnable");
assert_eq!(serialized, expected);

// Every shell runnable is a structurally valid cargo runnable if the `kind` tag isn't
// used. This test ensures that the `kind` tag is used.
let deserialized: Runnable =
serde_json::from_value(expected).expect("shell runnable should deserialize");
let RunnableArgs::Shell(shell) = &deserialized.args else {
panic!("expected Shell variant, got {:?}", deserialized.args);
};
assert_eq!(shell.program, "cargo");
assert_eq!(shell.args, vec!["nextest", "run", "--package", "my-crate"]);
}
}
4 changes: 0 additions & 4 deletions crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,6 @@ pub(crate) fn runnable(
Some((program, args)) => Some(lsp_ext::Runnable {
label,
location: Some(location),
kind: lsp_ext::RunnableKind::Shell,
args: lsp_ext::RunnableArgs::Shell(lsp_ext::ShellRunnableArgs {
environment,
cwd: cwd.into(),
Expand All @@ -1621,7 +1620,6 @@ pub(crate) fn runnable(
None => Some(lsp_ext::Runnable {
label,
location: Some(location),
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
workspace_root: Some(workspace_root.into()),
override_cargo: config.override_cargo,
Expand All @@ -1648,7 +1646,6 @@ pub(crate) fn runnable(
Ok(Some(lsp_ext::Runnable {
label,
location: Some(location),
kind: lsp_ext::RunnableKind::Shell,
args: lsp_ext::RunnableArgs::Shell(runnable_args),
}))
}
Expand All @@ -1668,7 +1665,6 @@ pub(crate) fn runnable(
Ok(Some(lsp_ext::Runnable {
label,
location: Some(location),
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::RunnableArgs::Cargo(lsp_ext::CargoRunnableArgs {
workspace_root: None,
override_cargo: config.override_cargo,
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/contributing/lsp-extensions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!---
lsp/ext.rs hash: edab2f3c124dc28e
lsp/ext.rs hash: 57ae57d2a5c65b14

If you need to change the above hash to make the test pass, please check if you
need to adjust this doc as well and ping this issue:
Expand Down
Loading