use std::path::PathBuf;
use serde_json::Value;
#[derive(Debug, Clone, PartialEq)]
pub struct EditorLens {
pub line: usize,
pub line_text: String,
pub title: String,
pub command: Option<String>,
pub arguments: Vec<Value>,
pub server_side: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LensAction {
ShowLocations(Vec<(PathBuf, u32, u32)>),
RunInTerminal { label: String, command: String },
DebugAt(usize),
ServerCommand {
command: String,
arguments: Vec<Value>,
},
Unsupported(String),
}
pub fn action_for(lens: &EditorLens) -> LensAction {
let Some(command) = lens.command.as_deref() else {
return LensAction::Unsupported(format!("\"{}\" has no command", lens.title));
};
if lens.server_side {
return LensAction::ServerCommand {
command: command.to_string(),
arguments: lens.arguments.clone(),
};
}
if command.ends_with("showReferences") || command == "editor.action.goToLocations" {
return match lens.arguments.get(2).map(locations) {
Some(locs) if !locs.is_empty() => LensAction::ShowLocations(locs),
_ => LensAction::Unsupported(String::from("no locations to show")),
};
}
match command {
"rust-analyzer.runSingle" => match lens.arguments.first().and_then(cargo_command) {
Some((label, command)) => LensAction::RunInTerminal { label, command },
None => LensAction::Unsupported(String::from("not a cargo runnable")),
},
"rust-analyzer.debugSingle" => LensAction::DebugAt(lens.line),
other => LensAction::Unsupported(format!("{other} is not a command croft runs")),
}
}
fn locations(v: &Value) -> Vec<(PathBuf, u32, u32)> {
let Some(items) = v.as_array() else {
return Vec::new();
};
items
.iter()
.filter_map(|loc| {
let uri = loc.get("uri").or_else(|| loc.get("targetUri"))?.as_str()?;
let range = loc
.get("range")
.or_else(|| loc.get("targetSelectionRange"))?;
let start = range.get("start")?;
let path = url::Url::parse(uri).ok()?.to_file_path().ok()?;
Some((
path,
start.get("line")?.as_u64()? as u32,
start.get("character")?.as_u64()? as u32,
))
})
.collect()
}
fn cargo_command(runnable: &Value) -> Option<(String, String)> {
if runnable.get("kind")?.as_str()? != "cargo" {
return None;
}
let args = runnable.get("args")?;
let strings = |key: &str| -> Vec<String> {
args.get(key)
.and_then(Value::as_array)
.map(|a| a.iter().filter_map(|s| s.as_str().map(sh_quote)).collect())
.unwrap_or_default()
};
let cargo_args = strings("cargoArgs");
if cargo_args.is_empty() {
return None;
}
let mut command = String::new();
if let Some(dir) = args
.get("cwd")
.or_else(|| args.get("workspaceRoot"))
.and_then(Value::as_str)
{
command.push_str(&format!("cd {} && ", sh_quote(dir)));
}
command.push_str("cargo ");
command.push_str(&cargo_args.join(" "));
let exec_args = strings("executableArgs");
if !exec_args.is_empty() {
command.push_str(" -- ");
command.push_str(&exec_args.join(" "));
}
let label = runnable
.get("label")
.and_then(Value::as_str)
.unwrap_or("cargo")
.to_string();
Some((label, command))
}
fn sh_quote(s: &str) -> String {
let plain = !s.is_empty()
&& s.chars()
.all(|c| c.is_ascii_alphanumeric() || "-_./:=+@%,".contains(c));
if plain {
s.to_string()
} else {
format!("'{}'", s.replace('\'', r"'\''"))
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn lens(command: &str, arguments: Vec<Value>, server_side: bool) -> EditorLens {
EditorLens {
line: 4,
line_text: String::from("fn it_works() {"),
title: String::from("t"),
command: Some(command.to_string()),
arguments,
server_side,
}
}
#[test]
fn a_references_lens_shows_its_locations() {
let l = lens(
"rust-analyzer.showReferences",
vec![
json!("file:///p/a.rs"),
json!({"line": 0, "character": 3}),
json!([{"uri": "file:///p/b.rs", "range": {"start": {"line": 7, "character": 2}, "end": {"line": 7, "character": 5}}}]),
],
false,
);
assert_eq!(
action_for(&l),
LensAction::ShowLocations(vec![(PathBuf::from("/p/b.rs"), 7, 2)])
);
}
#[test]
fn a_rust_analyzer_run_lens_becomes_a_cargo_command() {
let l = lens(
"rust-analyzer.runSingle",
vec![json!({
"label": "test tests::it_works",
"kind": "cargo",
"args": {
"cwd": "/p/my crate",
"cargoArgs": ["test", "--package", "demo", "--lib"],
"executableArgs": ["tests::it_works", "--exact", "--nocapture"]
}
})],
false,
);
assert_eq!(
action_for(&l),
LensAction::RunInTerminal {
label: String::from("test tests::it_works"),
command: String::from(
"cd '/p/my crate' && cargo test --package demo --lib -- tests::it_works --exact --nocapture"
),
}
);
}
#[test]
fn a_debug_lens_debugs_the_test_on_its_line() {
assert_eq!(
action_for(&lens("rust-analyzer.debugSingle", vec![], false)),
LensAction::DebugAt(4)
);
}
#[test]
fn a_command_the_server_advertises_goes_back_to_it() {
let l = lens(
"gopls.run_tests",
vec![json!({"URI": "file:///p/a_test.go"})],
true,
);
assert!(matches!(action_for(&l), LensAction::ServerCommand { .. }));
}
#[test]
fn an_unknown_client_command_is_inert_and_says_why() {
let l = lens("editor.action.somethingElse", vec![], false);
let LensAction::Unsupported(why) = action_for(&l) else {
panic!("expected Unsupported");
};
assert!(why.contains("editor.action.somethingElse"));
}
#[test]
fn shell_quoting_leaves_plain_words_bare() {
assert_eq!(sh_quote("--exact"), "--exact");
assert_eq!(sh_quote("a b"), "'a b'");
assert_eq!(sh_quote("it's"), r"'it'\''s'");
}
}