Preliminary runnables refactoring

This commit is contained in:
vsrs 2020-06-06 12:00:46 +03:00
parent d4a92b4fef
commit de74c0dcab
3 changed files with 122 additions and 54 deletions

View File

@ -42,6 +42,42 @@ pub enum RunnableKind {
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
//
// 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 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]
fn test_runnables() {
let (analysis, pos) = analysis_and_position(
@ -221,6 +266,9 @@ mod tests {
#[test]
#[ignore]
fn test_foo() {}
#[bench]
fn bench() {}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
@ -295,9 +343,32 @@ mod tests {
},
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]
@ -361,6 +432,7 @@ mod tests {
]
"###
);
assert_actions(&runnables, &[&BIN, &DOCTEST]);
}
#[test]
@ -427,6 +499,7 @@ mod tests {
]
"###
);
assert_actions(&runnables, &[&BIN, &DOCTEST]);
}
#[test]
@ -493,6 +566,7 @@ mod tests {
]
"###
);
assert_actions(&runnables, &[&TEST, &TEST]);
}
#[test]
@ -561,6 +635,7 @@ mod tests {
]
"###
);
assert_actions(&runnables, &[&TEST, &TEST]);
}
#[test]
@ -631,6 +706,7 @@ mod tests {
]
"###
);
assert_actions(&runnables, &[&TEST, &TEST]);
}
#[test]
@ -681,6 +757,7 @@ mod tests {
]
"###
);
assert_actions(&runnables, &[&TEST]);
}
#[test]
@ -739,6 +816,7 @@ mod tests {
]
"###
);
assert_actions(&runnables, &[&TEST]);
}
#[test]

View File

@ -18,7 +18,7 @@ use lsp_types::{
TextDocumentIdentifier, Url, WorkspaceEdit,
};
use ra_ide::{
FileId, FilePosition, FileRange, HoverAction, Query, RangeInfo, RunnableKind, SearchScope,
FileId, FilePosition, FileRange, HoverAction, Query, RangeInfo, Runnable, RunnableKind, SearchScope,
TextEdit,
};
use ra_prof::profile;
@ -403,16 +403,11 @@ pub fn handle_runnables(
if !runnable.nav.full_range().contains_inclusive(offset) {
continue;
}
}
if is_lib_target(&runnable, cargo_spec.as_ref()) {
continue;
}
// 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 => {}
_ => continue,
}
}
}
res.push(to_proto::runnable(&snap, file_id, runnable)?);
}
@ -817,53 +812,26 @@ pub fn handle_code_lens(
if snap.config.lens.runnable() {
// Gather runnables
for runnable in snap.analysis().runnables(file_id)? {
let (run_title, debugee) = match &runnable.kind {
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => {
("\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,
}
}
};
if is_lib_target(&runnable, cargo_spec.as_ref()) {
continue;
}
let action = runnable.action();
let range = to_proto::range(&line_index, runnable.nav.range());
let r = to_proto::runnable(&snap, file_id, runnable)?;
if snap.config.lens.run {
let lens = CodeLens {
range,
command: Some(Command {
title: run_title.to_string(),
command: "rust-analyzer.runSingle".into(),
arguments: Some(vec![to_value(&r).unwrap()]),
}),
command: Some(run_single_command(&r, action.run_title)),
data: None,
};
lenses.push(lens);
}
if debugee && snap.config.lens.debug {
if action.debugee && snap.config.lens.debug {
let debug_lens = CodeLens {
range,
command: Some(Command {
title: "Debug".into(),
command: "rust-analyzer.debugSingle".into(),
arguments: Some(vec![to_value(r).unwrap()]),
}),
command: Some(debug_single_command(r)),
data: None,
};
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 {
lsp_ext::CommandLink { tooltip: Some(tooltip), command }
}
@ -1214,3 +1198,17 @@ fn prepare_hover_actions(
})
.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
}

View File

@ -4,7 +4,7 @@ use ra_ide::{
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
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_vfs::LineEndings;
@ -662,15 +662,7 @@ pub(crate) fn runnable(
let target = spec.as_ref().map(|s| s.target.clone());
let (cargo_args, executable_args) =
CargoTargetSpec::runnable_args(spec, &runnable.kind, &runnable.cfg_exprs)?;
let label = match &runnable.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))
}
};
let label = runnable.label(target);
let location = location_link(snap, None, runnable.nav)?;
Ok(lsp_ext::Runnable {