Skip to content

Commit de74c0d

Browse files
committed
Preliminary runnables refactoring
1 parent d4a92b4 commit de74c0d

File tree

3 files changed

+122
-54
lines changed

3 files changed

+122
-54
lines changed

crates/ra_ide/src/runnables.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,42 @@ pub enum RunnableKind {
4242
Bin,
4343
}
4444

45+
#[derive(Debug, Eq, PartialEq)]
46+
pub struct RunnableAction {
47+
pub run_title: &'static str,
48+
pub debugee: bool,
49+
}
50+
51+
const TEST: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run Test", debugee: true };
52+
const DOCTEST: RunnableAction =
53+
RunnableAction { run_title: "▶\u{fe0e} Run Doctest", debugee: false };
54+
const BENCH: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run Bench", debugee: true };
55+
const BIN: RunnableAction = RunnableAction { run_title: "▶\u{fe0e} Run", debugee: true };
56+
57+
impl Runnable {
58+
// test package::module::testname
59+
pub fn label(&self, target: Option<String>) -> String {
60+
match &self.kind {
61+
RunnableKind::Test { test_id, .. } => format!("test {}", test_id),
62+
RunnableKind::TestMod { path } => format!("test-mod {}", path),
63+
RunnableKind::Bench { test_id } => format!("bench {}", test_id),
64+
RunnableKind::DocTest { test_id, .. } => format!("doctest {}", test_id),
65+
RunnableKind::Bin => {
66+
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
67+
}
68+
}
69+
}
70+
71+
pub fn action(&self) -> &'static RunnableAction {
72+
match &self.kind {
73+
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => &TEST,
74+
RunnableKind::DocTest { .. } => &DOCTEST,
75+
RunnableKind::Bench { .. } => &BENCH,
76+
RunnableKind::Bin => &BIN,
77+
}
78+
}
79+
}
80+
4581
// Feature: Run
4682
//
4783
// Shows a popup suggesting to run a test/benchmark/binary **at the current cursor
@@ -207,6 +243,15 @@ mod tests {
207243

208244
use crate::mock_analysis::analysis_and_position;
209245

246+
use super::{Runnable, RunnableAction, BENCH, BIN, DOCTEST, TEST};
247+
248+
fn assert_actions(runnables: &[Runnable], actions: &[&RunnableAction]) {
249+
assert_eq!(
250+
actions,
251+
runnables.into_iter().map(|it| it.action()).collect::<Vec<_>>().as_slice()
252+
);
253+
}
254+
210255
#[test]
211256
fn test_runnables() {
212257
let (analysis, pos) = analysis_and_position(
@@ -221,6 +266,9 @@ mod tests {
221266
#[test]
222267
#[ignore]
223268
fn test_foo() {}
269+
270+
#[bench]
271+
fn bench() {}
224272
"#,
225273
);
226274
let runnables = analysis.runnables(pos.file_id).unwrap();
@@ -295,9 +343,32 @@ mod tests {
295343
},
296344
cfg_exprs: [],
297345
},
346+
Runnable {
347+
nav: NavigationTarget {
348+
file_id: FileId(
349+
1,
350+
),
351+
full_range: 82..104,
352+
name: "bench",
353+
kind: FN_DEF,
354+
focus_range: Some(
355+
94..99,
356+
),
357+
container_name: None,
358+
description: None,
359+
docs: None,
360+
},
361+
kind: Bench {
362+
test_id: Path(
363+
"bench",
364+
),
365+
},
366+
cfg_exprs: [],
367+
},
298368
]
299369
"###
300370
);
371+
assert_actions(&runnables, &[&BIN, &TEST, &TEST, &BENCH]);
301372
}
302373

303374
#[test]
@@ -361,6 +432,7 @@ mod tests {
361432
]
362433
"###
363434
);
435+
assert_actions(&runnables, &[&BIN, &DOCTEST]);
364436
}
365437

366438
#[test]
@@ -427,6 +499,7 @@ mod tests {
427499
]
428500
"###
429501
);
502+
assert_actions(&runnables, &[&BIN, &DOCTEST]);
430503
}
431504

432505
#[test]
@@ -493,6 +566,7 @@ mod tests {
493566
]
494567
"###
495568
);
569+
assert_actions(&runnables, &[&TEST, &TEST]);
496570
}
497571

498572
#[test]
@@ -561,6 +635,7 @@ mod tests {
561635
]
562636
"###
563637
);
638+
assert_actions(&runnables, &[&TEST, &TEST]);
564639
}
565640

566641
#[test]
@@ -631,6 +706,7 @@ mod tests {
631706
]
632707
"###
633708
);
709+
assert_actions(&runnables, &[&TEST, &TEST]);
634710
}
635711

636712
#[test]
@@ -681,6 +757,7 @@ mod tests {
681757
]
682758
"###
683759
);
760+
assert_actions(&runnables, &[&TEST]);
684761
}
685762

686763
#[test]
@@ -739,6 +816,7 @@ mod tests {
739816
]
740817
"###
741818
);
819+
assert_actions(&runnables, &[&TEST]);
742820
}
743821

744822
#[test]

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use lsp_types::{
1818
TextDocumentIdentifier, Url, WorkspaceEdit,
1919
};
2020
use ra_ide::{
21-
FileId, FilePosition, FileRange, HoverAction, Query, RangeInfo, RunnableKind, SearchScope,
21+
FileId, FilePosition, FileRange, HoverAction, Query, RangeInfo, Runnable, RunnableKind, SearchScope,
2222
TextEdit,
2323
};
2424
use ra_prof::profile;
@@ -403,16 +403,11 @@ pub fn handle_runnables(
403403
if !runnable.nav.full_range().contains_inclusive(offset) {
404404
continue;
405405
}
406+
}
407+
if is_lib_target(&runnable, cargo_spec.as_ref()) {
408+
continue;
406409
}
407-
// Do not suggest binary run on other target than binary
408-
if let RunnableKind::Bin = runnable.kind {
409-
if let Some(spec) = &cargo_spec {
410-
match spec.target_kind {
411-
TargetKind::Bin => {}
412-
_ => continue,
413-
}
414-
}
415-
}
410+
416411
res.push(to_proto::runnable(&snap, file_id, runnable)?);
417412
}
418413

@@ -817,53 +812,26 @@ pub fn handle_code_lens(
817812
if snap.config.lens.runnable() {
818813
// Gather runnables
819814
for runnable in snap.analysis().runnables(file_id)? {
820-
let (run_title, debugee) = match &runnable.kind {
821-
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => {
822-
("▶\u{fe0e} Run Test", true)
823-
}
824-
RunnableKind::DocTest { .. } => {
825-
// cargo does not support -no-run for doctests
826-
("▶\u{fe0e} Run Doctest", false)
827-
}
828-
RunnableKind::Bench { .. } => {
829-
// Nothing wrong with bench debugging
830-
("Run Bench", true)
831-
}
832-
RunnableKind::Bin => {
833-
// Do not suggest binary run on other target than binary
834-
match &cargo_spec {
835-
Some(spec) => match spec.target_kind {
836-
TargetKind::Bin => ("Run", true),
837-
_ => continue,
838-
},
839-
None => continue,
840-
}
841-
}
842-
};
815+
if is_lib_target(&runnable, cargo_spec.as_ref()) {
816+
continue;
817+
}
843818

819+
let action = runnable.action();
844820
let range = to_proto::range(&line_index, runnable.nav.range());
845821
let r = to_proto::runnable(&snap, file_id, runnable)?;
846822
if snap.config.lens.run {
847823
let lens = CodeLens {
848824
range,
849-
command: Some(Command {
850-
title: run_title.to_string(),
851-
command: "rust-analyzer.runSingle".into(),
852-
arguments: Some(vec![to_value(&r).unwrap()]),
853-
}),
825+
command: Some(run_single_command(&r, action.run_title)),
854826
data: None,
855827
};
856828
lenses.push(lens);
857829
}
858830

859-
if debugee && snap.config.lens.debug {
831+
if action.debugee && snap.config.lens.debug {
860832
let debug_lens = CodeLens {
861833
range,
862-
command: Some(Command {
863-
title: "Debug".into(),
864-
command: "rust-analyzer.debugSingle".into(),
865-
arguments: Some(vec![to_value(r).unwrap()]),
866-
}),
834+
command: Some(debug_single_command(r)),
867835
data: None,
868836
};
869837
lenses.push(debug_lens);
@@ -1169,6 +1137,22 @@ fn show_references_command(
11691137
}
11701138
}
11711139

1140+
fn run_single_command(runnable: &lsp_ext::Runnable, title: &str) -> Command {
1141+
Command {
1142+
title: title.to_string(),
1143+
command: "rust-analyzer.runSingle".into(),
1144+
arguments: Some(vec![to_value(runnable).unwrap()]),
1145+
}
1146+
}
1147+
1148+
fn debug_single_command(runnable: lsp_ext::Runnable) -> Command {
1149+
Command {
1150+
title: "Debug".into(),
1151+
command: "rust-analyzer.debugSingle".into(),
1152+
arguments: Some(vec![to_value(runnable).unwrap()]),
1153+
}
1154+
}
1155+
11721156
fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink {
11731157
lsp_ext::CommandLink { tooltip: Some(tooltip), command }
11741158
}
@@ -1214,3 +1198,17 @@ fn prepare_hover_actions(
12141198
})
12151199
.collect()
12161200
}
1201+
1202+
fn is_lib_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>) -> bool {
1203+
// Do not suggest binary run on other target than binary
1204+
if let RunnableKind::Bin = runnable.kind {
1205+
if let Some(spec) = cargo_spec {
1206+
match spec.target_kind {
1207+
TargetKind::Bin => return true,
1208+
_ => ()
1209+
}
1210+
}
1211+
}
1212+
1213+
false
1214+
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ra_ide::{
44
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
55
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
66
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess,
7-
ResolvedAssist, Runnable, RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit,
7+
ResolvedAssist, Runnable, Severity, SourceChange, SourceFileEdit, TextEdit,
88
};
99
use ra_syntax::{SyntaxKind, TextRange, TextSize};
1010
use ra_vfs::LineEndings;
@@ -662,15 +662,7 @@ pub(crate) fn runnable(
662662
let target = spec.as_ref().map(|s| s.target.clone());
663663
let (cargo_args, executable_args) =
664664
CargoTargetSpec::runnable_args(spec, &runnable.kind, &runnable.cfg_exprs)?;
665-
let label = match &runnable.kind {
666-
RunnableKind::Test { test_id, .. } => format!("test {}", test_id),
667-
RunnableKind::TestMod { path } => format!("test-mod {}", path),
668-
RunnableKind::Bench { test_id } => format!("bench {}", test_id),
669-
RunnableKind::DocTest { test_id, .. } => format!("doctest {}", test_id),
670-
RunnableKind::Bin => {
671-
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
672-
}
673-
};
665+
let label = runnable.label(target);
674666
let location = location_link(snap, None, runnable.nav)?;
675667

676668
Ok(lsp_ext::Runnable {

0 commit comments

Comments
 (0)