mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Preliminary runnables refactoring
This commit is contained in:
parent
d4a92b4fef
commit
de74c0dcab
3 changed files with 122 additions and 54 deletions
|
@ -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
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue