mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Preliminary runnables refactoring
This commit is contained in:
parent
d4a92b4fef
commit
de74c0dcab
3 changed files with 122 additions and 54 deletions
|
@ -42,6 +42,42 @@ pub enum RunnableKind {
|
||||||
Bin,
|
Bin,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
pub struct RunnableAction {
|
||||||
|
pub run_title: &'static str,
|
||||||
|
pub debugee: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
const TEST: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run Test", debugee: true };
|
||||||
|
const DOCTEST: RunnableAction =
|
||||||
|
RunnableAction { run_title: "▶\u{fe0e} Run Doctest", debugee: false };
|
||||||
|
const BENCH: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run Bench", debugee: true };
|
||||||
|
const BIN: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run", debugee: true };
|
||||||
|
|
||||||
|
impl Runnable {
|
||||||
|
// test package::module::testname
|
||||||
|
pub fn label(&self, target: Option<String>) -> String {
|
||||||
|
match &self.kind {
|
||||||
|
RunnableKind::Test { test_id, .. } => format!("test {}", test_id),
|
||||||
|
RunnableKind::TestMod { path } => format!("test-mod {}", path),
|
||||||
|
RunnableKind::Bench { test_id } => format!("bench {}", test_id),
|
||||||
|
RunnableKind::DocTest { test_id, .. } => format!("doctest {}", test_id),
|
||||||
|
RunnableKind::Bin => {
|
||||||
|
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn action(&self) -> &'static RunnableAction {
|
||||||
|
match &self.kind {
|
||||||
|
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => &TEST,
|
||||||
|
RunnableKind::DocTest { .. } => &DOCTEST,
|
||||||
|
RunnableKind::Bench { .. } => &BENCH,
|
||||||
|
RunnableKind::Bin => &BIN,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Feature: Run
|
// Feature: Run
|
||||||
//
|
//
|
||||||
// Shows a popup suggesting to run a test/benchmark/binary **at the current cursor
|
// Shows a popup suggesting to run a test/benchmark/binary **at the current cursor
|
||||||
|
@ -207,6 +243,15 @@ mod tests {
|
||||||
|
|
||||||
use crate::mock_analysis::analysis_and_position;
|
use crate::mock_analysis::analysis_and_position;
|
||||||
|
|
||||||
|
use super::{Runnable, RunnableAction, BENCH, BIN, DOCTEST, TEST};
|
||||||
|
|
||||||
|
fn assert_actions(runnables: &[Runnable], actions: &[&RunnableAction]) {
|
||||||
|
assert_eq!(
|
||||||
|
actions,
|
||||||
|
runnables.into_iter().map(|it| it.action()).collect::<Vec<_>>().as_slice()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_runnables() {
|
fn test_runnables() {
|
||||||
let (analysis, pos) = analysis_and_position(
|
let (analysis, pos) = analysis_and_position(
|
||||||
|
@ -221,6 +266,9 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn test_foo() {}
|
fn test_foo() {}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn bench() {}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
let runnables = analysis.runnables(pos.file_id).unwrap();
|
let runnables = analysis.runnables(pos.file_id).unwrap();
|
||||||
|
@ -295,9 +343,32 @@ mod tests {
|
||||||
},
|
},
|
||||||
cfg_exprs: [],
|
cfg_exprs: [],
|
||||||
},
|
},
|
||||||
|
Runnable {
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
full_range: 82..104,
|
||||||
|
name: "bench",
|
||||||
|
kind: FN_DEF,
|
||||||
|
focus_range: Some(
|
||||||
|
94..99,
|
||||||
|
),
|
||||||
|
container_name: None,
|
||||||
|
description: None,
|
||||||
|
docs: None,
|
||||||
|
},
|
||||||
|
kind: Bench {
|
||||||
|
test_id: Path(
|
||||||
|
"bench",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
cfg_exprs: [],
|
||||||
|
},
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&BIN, &TEST, &TEST, &BENCH]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -361,6 +432,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&BIN, &DOCTEST]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -427,6 +499,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&BIN, &DOCTEST]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -493,6 +566,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&TEST, &TEST]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -561,6 +635,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&TEST, &TEST]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -631,6 +706,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&TEST, &TEST]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -681,6 +757,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&TEST]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -739,6 +816,7 @@ mod tests {
|
||||||
]
|
]
|
||||||
"###
|
"###
|
||||||
);
|
);
|
||||||
|
assert_actions(&runnables, &[&TEST]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -18,7 +18,7 @@ use lsp_types::{
|
||||||
TextDocumentIdentifier, Url, WorkspaceEdit,
|
TextDocumentIdentifier, Url, WorkspaceEdit,
|
||||||
};
|
};
|
||||||
use ra_ide::{
|
use ra_ide::{
|
||||||
FileId, FilePosition, FileRange, HoverAction, Query, RangeInfo, RunnableKind, SearchScope,
|
FileId, FilePosition, FileRange, HoverAction, Query, RangeInfo, Runnable, RunnableKind, SearchScope,
|
||||||
TextEdit,
|
TextEdit,
|
||||||
};
|
};
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
|
@ -404,15 +404,10 @@ pub fn handle_runnables(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Do not suggest binary run on other target than binary
|
if is_lib_target(&runnable, cargo_spec.as_ref()) {
|
||||||
if let RunnableKind::Bin = runnable.kind {
|
continue;
|
||||||
if let Some(spec) = &cargo_spec {
|
|
||||||
match spec.target_kind {
|
|
||||||
TargetKind::Bin => {}
|
|
||||||
_ => continue,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.push(to_proto::runnable(&snap, file_id, runnable)?);
|
res.push(to_proto::runnable(&snap, file_id, runnable)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -817,53 +812,26 @@ pub fn handle_code_lens(
|
||||||
if snap.config.lens.runnable() {
|
if snap.config.lens.runnable() {
|
||||||
// Gather runnables
|
// Gather runnables
|
||||||
for runnable in snap.analysis().runnables(file_id)? {
|
for runnable in snap.analysis().runnables(file_id)? {
|
||||||
let (run_title, debugee) = match &runnable.kind {
|
if is_lib_target(&runnable, cargo_spec.as_ref()) {
|
||||||
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => {
|
continue;
|
||||||
("▶\u{fe0e} Run Test", true)
|
|
||||||
}
|
}
|
||||||
RunnableKind::DocTest { .. } => {
|
|
||||||
// cargo does not support -no-run for doctests
|
|
||||||
("▶\u{fe0e} Run Doctest", false)
|
|
||||||
}
|
|
||||||
RunnableKind::Bench { .. } => {
|
|
||||||
// Nothing wrong with bench debugging
|
|
||||||
("Run Bench", true)
|
|
||||||
}
|
|
||||||
RunnableKind::Bin => {
|
|
||||||
// Do not suggest binary run on other target than binary
|
|
||||||
match &cargo_spec {
|
|
||||||
Some(spec) => match spec.target_kind {
|
|
||||||
TargetKind::Bin => ("Run", true),
|
|
||||||
_ => continue,
|
|
||||||
},
|
|
||||||
None => continue,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
let action = runnable.action();
|
||||||
let range = to_proto::range(&line_index, runnable.nav.range());
|
let range = to_proto::range(&line_index, runnable.nav.range());
|
||||||
let r = to_proto::runnable(&snap, file_id, runnable)?;
|
let r = to_proto::runnable(&snap, file_id, runnable)?;
|
||||||
if snap.config.lens.run {
|
if snap.config.lens.run {
|
||||||
let lens = CodeLens {
|
let lens = CodeLens {
|
||||||
range,
|
range,
|
||||||
command: Some(Command {
|
command: Some(run_single_command(&r, action.run_title)),
|
||||||
title: run_title.to_string(),
|
|
||||||
command: "rust-analyzer.runSingle".into(),
|
|
||||||
arguments: Some(vec![to_value(&r).unwrap()]),
|
|
||||||
}),
|
|
||||||
data: None,
|
data: None,
|
||||||
};
|
};
|
||||||
lenses.push(lens);
|
lenses.push(lens);
|
||||||
}
|
}
|
||||||
|
|
||||||
if debugee && snap.config.lens.debug {
|
if action.debugee && snap.config.lens.debug {
|
||||||
let debug_lens = CodeLens {
|
let debug_lens = CodeLens {
|
||||||
range,
|
range,
|
||||||
command: Some(Command {
|
command: Some(debug_single_command(r)),
|
||||||
title: "Debug".into(),
|
|
||||||
command: "rust-analyzer.debugSingle".into(),
|
|
||||||
arguments: Some(vec![to_value(r).unwrap()]),
|
|
||||||
}),
|
|
||||||
data: None,
|
data: None,
|
||||||
};
|
};
|
||||||
lenses.push(debug_lens);
|
lenses.push(debug_lens);
|
||||||
|
@ -1169,6 +1137,22 @@ fn show_references_command(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_single_command(runnable: &lsp_ext::Runnable, title: &str) -> Command {
|
||||||
|
Command {
|
||||||
|
title: title.to_string(),
|
||||||
|
command: "rust-analyzer.runSingle".into(),
|
||||||
|
arguments: Some(vec![to_value(runnable).unwrap()]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn debug_single_command(runnable: lsp_ext::Runnable) -> Command {
|
||||||
|
Command {
|
||||||
|
title: "Debug".into(),
|
||||||
|
command: "rust-analyzer.debugSingle".into(),
|
||||||
|
arguments: Some(vec![to_value(runnable).unwrap()]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink {
|
fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink {
|
||||||
lsp_ext::CommandLink { tooltip: Some(tooltip), command }
|
lsp_ext::CommandLink { tooltip: Some(tooltip), command }
|
||||||
}
|
}
|
||||||
|
@ -1214,3 +1198,17 @@ fn prepare_hover_actions(
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_lib_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
|
||||||
|
// Do not suggest binary run on other target than binary
|
||||||
|
if let RunnableKind::Bin = runnable.kind {
|
||||||
|
if let Some(spec) = cargo_spec {
|
||||||
|
match spec.target_kind {
|
||||||
|
TargetKind::Bin => return true,
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ use ra_ide::{
|
||||||
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
|
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
|
||||||
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
|
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
|
||||||
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess,
|
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess,
|
||||||
ResolvedAssist, Runnable, RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit,
|
ResolvedAssist, Runnable, Severity, SourceChange, SourceFileEdit, TextEdit,
|
||||||
};
|
};
|
||||||
use ra_syntax::{SyntaxKind, TextRange, TextSize};
|
use ra_syntax::{SyntaxKind, TextRange, TextSize};
|
||||||
use ra_vfs::LineEndings;
|
use ra_vfs::LineEndings;
|
||||||
|
@ -662,15 +662,7 @@ pub(crate) fn runnable(
|
||||||
let target = spec.as_ref().map(|s| s.target.clone());
|
let target = spec.as_ref().map(|s| s.target.clone());
|
||||||
let (cargo_args, executable_args) =
|
let (cargo_args, executable_args) =
|
||||||
CargoTargetSpec::runnable_args(spec, &runnable.kind, &runnable.cfg_exprs)?;
|
CargoTargetSpec::runnable_args(spec, &runnable.kind, &runnable.cfg_exprs)?;
|
||||||
let label = match &runnable.kind {
|
let label = runnable.label(target);
|
||||||
RunnableKind::Test { test_id, .. } => format!("test {}", test_id),
|
|
||||||
RunnableKind::TestMod { path } => format!("test-mod {}", path),
|
|
||||||
RunnableKind::Bench { test_id } => format!("bench {}", test_id),
|
|
||||||
RunnableKind::DocTest { test_id, .. } => format!("doctest {}", test_id),
|
|
||||||
RunnableKind::Bin => {
|
|
||||||
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let location = location_link(snap, None, runnable.nav)?;
|
let location = location_link(snap, None, runnable.nav)?;
|
||||||
|
|
||||||
Ok(lsp_ext::Runnable {
|
Ok(lsp_ext::Runnable {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue