mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Merge #513
513: Add bench runnable and code lens r=matklad a=kjeremy Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
This commit is contained in:
commit
a2659abed8
@ -17,6 +17,7 @@ pub struct Runnable {
|
|||||||
pub enum RunnableKind {
|
pub enum RunnableKind {
|
||||||
Test { name: String },
|
Test { name: String },
|
||||||
TestMod { path: String },
|
TestMod { path: String },
|
||||||
|
Bench { name: String },
|
||||||
Bin,
|
Bin,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +49,10 @@ fn runnable_fn(fn_def: &ast::FnDef) -> Option<Runnable> {
|
|||||||
RunnableKind::Test {
|
RunnableKind::Test {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
}
|
}
|
||||||
|
} else if fn_def.has_atom_attr("bench") {
|
||||||
|
RunnableKind::Bench {
|
||||||
|
name: name.to_string(),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
@ -32,6 +32,15 @@ pub(crate) fn runnable_args(
|
|||||||
res.push(path.to_string());
|
res.push(path.to_string());
|
||||||
res.push("--nocapture".to_string());
|
res.push("--nocapture".to_string());
|
||||||
}
|
}
|
||||||
|
RunnableKind::Bench { name } => {
|
||||||
|
res.push("bench".to_string());
|
||||||
|
if let Some(spec) = spec {
|
||||||
|
spec.push_to(&mut res);
|
||||||
|
}
|
||||||
|
res.push("--".to_string());
|
||||||
|
res.push(name.to_string());
|
||||||
|
res.push("--nocapture".to_string());
|
||||||
|
}
|
||||||
RunnableKind::Bin => {
|
RunnableKind::Bin => {
|
||||||
res.push("run".to_string());
|
res.push("run".to_string());
|
||||||
if let Some(spec) = spec {
|
if let Some(spec) = spec {
|
||||||
|
@ -2,22 +2,23 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use gen_lsp_server::ErrorCode;
|
use gen_lsp_server::ErrorCode;
|
||||||
use languageserver_types::{
|
use languageserver_types::{
|
||||||
CodeActionResponse, Command, CodeLens, Diagnostic, DiagnosticSeverity, DocumentFormattingParams,
|
CodeActionResponse, CodeLens, Command, Diagnostic, DiagnosticSeverity,
|
||||||
DocumentHighlight, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind,
|
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, Documentation, FoldingRange,
|
||||||
FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind,
|
FoldingRangeKind, FoldingRangeParams, Hover, HoverContents, Location, MarkupContent,
|
||||||
ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range, RenameParams,
|
MarkupKind, ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range,
|
||||||
SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit,
|
RenameParams, SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit,
|
||||||
|
WorkspaceEdit,
|
||||||
};
|
};
|
||||||
use ra_ide_api::{
|
use ra_ide_api::{
|
||||||
FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, RangeInfo,
|
FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo, RunnableKind, Severity,
|
||||||
};
|
};
|
||||||
use ra_syntax::{TextUnit, AstNode};
|
use ra_syntax::{AstNode, TextUnit};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde_json::to_value;
|
use serde_json::to_value;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cargo_target_spec::{CargoTargetSpec, runnable_args},
|
cargo_target_spec::{runnable_args, CargoTargetSpec},
|
||||||
conv::{to_location, to_location_link, Conv, ConvWith, MapConvWith, TryConvWith},
|
conv::{to_location, to_location_link, Conv, ConvWith, MapConvWith, TryConvWith},
|
||||||
req::{self, Decoration},
|
req::{self, Decoration},
|
||||||
server_world::ServerWorld,
|
server_world::ServerWorld,
|
||||||
@ -258,6 +259,7 @@ pub fn handle_runnables(
|
|||||||
label: match &runnable.kind {
|
label: match &runnable.kind {
|
||||||
RunnableKind::Test { name } => format!("test {}", name),
|
RunnableKind::Test { name } => format!("test {}", name),
|
||||||
RunnableKind::TestMod { path } => format!("test-mod {}", path),
|
RunnableKind::TestMod { path } => format!("test-mod {}", path),
|
||||||
|
RunnableKind::Bench { name } => format!("bench {}", name),
|
||||||
RunnableKind::Bin => "run binary".to_string(),
|
RunnableKind::Bin => "run binary".to_string(),
|
||||||
},
|
},
|
||||||
bin: "cargo".to_string(),
|
bin: "cargo".to_string(),
|
||||||
@ -586,35 +588,37 @@ pub fn handle_code_lens(
|
|||||||
let mut lenses: Vec<CodeLens> = Default::default();
|
let mut lenses: Vec<CodeLens> = Default::default();
|
||||||
|
|
||||||
for runnable in world.analysis().runnables(file_id)? {
|
for runnable in world.analysis().runnables(file_id)? {
|
||||||
match &runnable.kind {
|
let title = match &runnable.kind {
|
||||||
RunnableKind::Test { name: _ } | RunnableKind::TestMod { path: _ } => {
|
RunnableKind::Test { name: _ } | RunnableKind::TestMod { path: _ } => Some("Run Test"),
|
||||||
let args = runnable_args(&world, file_id, &runnable.kind)?;
|
RunnableKind::Bench { name: _ } => Some("Run Bench"),
|
||||||
|
_ => None,
|
||||||
let range = runnable.range.conv_with(&line_index);
|
|
||||||
|
|
||||||
// This represents the actual command that will be run.
|
|
||||||
let r: req::Runnable = req::Runnable {
|
|
||||||
range,
|
|
||||||
label: Default::default(),
|
|
||||||
bin: "cargo".into(),
|
|
||||||
args,
|
|
||||||
env: Default::default(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let lens = CodeLens {
|
|
||||||
range,
|
|
||||||
command: Some(Command {
|
|
||||||
title: "Run Test".into(),
|
|
||||||
command: "ra-lsp.run-single".into(),
|
|
||||||
arguments: Some(vec![to_value(r).unwrap()]),
|
|
||||||
}),
|
|
||||||
data: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
lenses.push(lens);
|
|
||||||
}
|
|
||||||
_ => continue,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if let Some(title) = title {
|
||||||
|
let args = runnable_args(&world, file_id, &runnable.kind)?;
|
||||||
|
let range = runnable.range.conv_with(&line_index);
|
||||||
|
|
||||||
|
// This represents the actual command that will be run.
|
||||||
|
let r: req::Runnable = req::Runnable {
|
||||||
|
range,
|
||||||
|
label: Default::default(),
|
||||||
|
bin: "cargo".into(),
|
||||||
|
args,
|
||||||
|
env: Default::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let lens = CodeLens {
|
||||||
|
range,
|
||||||
|
command: Some(Command {
|
||||||
|
title: title.into(),
|
||||||
|
command: "ra-lsp.run-single".into(),
|
||||||
|
arguments: Some(vec![to_value(r).unwrap()]),
|
||||||
|
}),
|
||||||
|
data: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
lenses.push(lens);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(Some(lenses));
|
return Ok(Some(lenses));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user