Merge #500
500: Code lens support for running tests r=matklad a=kjeremy Supports running individual and mod tests. I feel like this kind of abuses the `Runnables` infrastructure but it works. Maybe later on down the line we should introduce a struct that is really just a tuple of binary, arguments, and environment and pass that back to the client instead. `run_single.ts` is just a paired down version of `runnables.ts` and there is duplication because I think run_single will probably change independent of runnables. Co-authored-by: Jeremy A. Kolb <jkolb@ara.com> Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
This commit is contained in:
commit
e56072bfa3
@ -1,5 +1,5 @@
|
||||
use languageserver_types::{
|
||||
CodeActionProviderCapability, CompletionOptions, DocumentOnTypeFormattingOptions,
|
||||
CodeActionProviderCapability, CodeLensOptions, CompletionOptions, DocumentOnTypeFormattingOptions,
|
||||
ExecuteCommandOptions, FoldingRangeProviderCapability, RenameOptions, RenameProviderCapability,
|
||||
ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind,
|
||||
TextDocumentSyncOptions,
|
||||
@ -32,7 +32,9 @@ pub fn server_capabilities() -> ServerCapabilities {
|
||||
document_symbol_provider: Some(true),
|
||||
workspace_symbol_provider: Some(true),
|
||||
code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
|
||||
code_lens_provider: None,
|
||||
code_lens_provider: Some(CodeLensOptions {
|
||||
resolve_provider: None,
|
||||
}),
|
||||
document_formatting_provider: Some(true),
|
||||
document_range_formatting_provider: None,
|
||||
document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions {
|
||||
|
100
crates/ra_lsp_server/src/cargo_target_spec.rs
Normal file
100
crates/ra_lsp_server/src/cargo_target_spec.rs
Normal file
@ -0,0 +1,100 @@
|
||||
use crate::{
|
||||
project_model::TargetKind,
|
||||
server_world::ServerWorld,
|
||||
Result
|
||||
};
|
||||
|
||||
use ra_ide_api::{FileId, RunnableKind};
|
||||
|
||||
pub(crate) fn runnable_args(
|
||||
world: &ServerWorld,
|
||||
file_id: FileId,
|
||||
kind: &RunnableKind,
|
||||
) -> Result<Vec<String>> {
|
||||
let spec = CargoTargetSpec::for_file(world, file_id)?;
|
||||
let mut res = Vec::new();
|
||||
match kind {
|
||||
RunnableKind::Test { name } => {
|
||||
res.push("test".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::TestMod { path } => {
|
||||
res.push("test".to_string());
|
||||
if let Some(spec) = spec {
|
||||
spec.push_to(&mut res);
|
||||
}
|
||||
res.push("--".to_string());
|
||||
res.push(path.to_string());
|
||||
res.push("--nocapture".to_string());
|
||||
}
|
||||
RunnableKind::Bin => {
|
||||
res.push("run".to_string());
|
||||
if let Some(spec) = spec {
|
||||
spec.push_to(&mut res);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub struct CargoTargetSpec {
|
||||
pub package: String,
|
||||
pub target: String,
|
||||
pub target_kind: TargetKind,
|
||||
}
|
||||
|
||||
impl CargoTargetSpec {
|
||||
pub fn for_file(world: &ServerWorld, file_id: FileId) -> Result<Option<CargoTargetSpec>> {
|
||||
let &crate_id = match world.analysis().crate_for(file_id)?.first() {
|
||||
Some(crate_id) => crate_id,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let file_id = world.analysis().crate_root(crate_id)?;
|
||||
let path = world
|
||||
.vfs
|
||||
.read()
|
||||
.file2path(ra_vfs::VfsFile(file_id.0.into()));
|
||||
let res = world.workspaces.iter().find_map(|ws| {
|
||||
let tgt = ws.cargo.target_by_root(&path)?;
|
||||
let res = CargoTargetSpec {
|
||||
package: tgt.package(&ws.cargo).name(&ws.cargo).to_string(),
|
||||
target: tgt.name(&ws.cargo).to_string(),
|
||||
target_kind: tgt.kind(&ws.cargo),
|
||||
};
|
||||
Some(res)
|
||||
});
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn push_to(self, buf: &mut Vec<String>) {
|
||||
buf.push("--package".to_string());
|
||||
buf.push(self.package);
|
||||
match self.target_kind {
|
||||
TargetKind::Bin => {
|
||||
buf.push("--bin".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Test => {
|
||||
buf.push("--test".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Bench => {
|
||||
buf.push("--bench".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Example => {
|
||||
buf.push("--example".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Lib => {
|
||||
buf.push("--lib".to_string());
|
||||
}
|
||||
TargetKind::Other => (),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
mod caps;
|
||||
mod cargo_target_spec;
|
||||
mod conv;
|
||||
mod main_loop;
|
||||
mod project_model;
|
||||
|
@ -300,6 +300,7 @@ fn on_request(
|
||||
.on::<req::DecorationsRequest>(handlers::handle_decorations)?
|
||||
.on::<req::Completion>(handlers::handle_completion)?
|
||||
.on::<req::CodeActionRequest>(handlers::handle_code_action)?
|
||||
.on::<req::CodeLensRequest>(handlers::handle_code_lens)?
|
||||
.on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
|
||||
.on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
|
||||
.on::<req::HoverRequest>(handlers::handle_hover)?
|
||||
|
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use gen_lsp_server::ErrorCode;
|
||||
use languageserver_types::{
|
||||
CodeActionResponse, Command, Diagnostic, DiagnosticSeverity, DocumentFormattingParams,
|
||||
CodeActionResponse, Command, CodeLens, Diagnostic, DiagnosticSeverity, DocumentFormattingParams,
|
||||
DocumentHighlight, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind,
|
||||
FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind,
|
||||
ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range, RenameParams,
|
||||
@ -17,8 +17,8 @@ use serde_json::to_value;
|
||||
use std::io::Write;
|
||||
|
||||
use crate::{
|
||||
cargo_target_spec::{CargoTargetSpec, runnable_args},
|
||||
conv::{to_location, to_location_link, Conv, ConvWith, MapConvWith, TryConvWith},
|
||||
project_model::TargetKind,
|
||||
req::{self, Decoration},
|
||||
server_world::ServerWorld,
|
||||
LspError, Result,
|
||||
@ -291,99 +291,6 @@ pub fn handle_runnables(
|
||||
env: FxHashMap::default(),
|
||||
});
|
||||
return Ok(res);
|
||||
|
||||
fn runnable_args(
|
||||
world: &ServerWorld,
|
||||
file_id: FileId,
|
||||
kind: &RunnableKind,
|
||||
) -> Result<Vec<String>> {
|
||||
let spec = CargoTargetSpec::for_file(world, file_id)?;
|
||||
let mut res = Vec::new();
|
||||
match kind {
|
||||
RunnableKind::Test { name } => {
|
||||
res.push("test".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::TestMod { path } => {
|
||||
res.push("test".to_string());
|
||||
if let Some(spec) = spec {
|
||||
spec.push_to(&mut res);
|
||||
}
|
||||
res.push("--".to_string());
|
||||
res.push(path.to_string());
|
||||
res.push("--nocapture".to_string());
|
||||
}
|
||||
RunnableKind::Bin => {
|
||||
res.push("run".to_string());
|
||||
if let Some(spec) = spec {
|
||||
spec.push_to(&mut res);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
struct CargoTargetSpec {
|
||||
package: String,
|
||||
target: String,
|
||||
target_kind: TargetKind,
|
||||
}
|
||||
|
||||
impl CargoTargetSpec {
|
||||
fn for_file(world: &ServerWorld, file_id: FileId) -> Result<Option<CargoTargetSpec>> {
|
||||
let &crate_id = match world.analysis().crate_for(file_id)?.first() {
|
||||
Some(crate_id) => crate_id,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let file_id = world.analysis().crate_root(crate_id)?;
|
||||
let path = world
|
||||
.vfs
|
||||
.read()
|
||||
.file2path(ra_vfs::VfsFile(file_id.0.into()));
|
||||
let res = world.workspaces.iter().find_map(|ws| {
|
||||
let tgt = ws.cargo.target_by_root(&path)?;
|
||||
let res = CargoTargetSpec {
|
||||
package: tgt.package(&ws.cargo).name(&ws.cargo).to_string(),
|
||||
target: tgt.name(&ws.cargo).to_string(),
|
||||
target_kind: tgt.kind(&ws.cargo),
|
||||
};
|
||||
Some(res)
|
||||
});
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn push_to(self, buf: &mut Vec<String>) {
|
||||
buf.push("--package".to_string());
|
||||
buf.push(self.package);
|
||||
match self.target_kind {
|
||||
TargetKind::Bin => {
|
||||
buf.push("--bin".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Test => {
|
||||
buf.push("--test".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Bench => {
|
||||
buf.push("--bench".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Example => {
|
||||
buf.push("--example".to_string());
|
||||
buf.push(self.target);
|
||||
}
|
||||
TargetKind::Lib => {
|
||||
buf.push("--lib".to_string());
|
||||
}
|
||||
TargetKind::Other => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_decorations(
|
||||
@ -669,6 +576,50 @@ pub fn handle_code_action(
|
||||
Ok(Some(CodeActionResponse::Commands(res)))
|
||||
}
|
||||
|
||||
pub fn handle_code_lens(
|
||||
world: ServerWorld,
|
||||
params: req::CodeLensParams,
|
||||
) -> Result<Option<Vec<CodeLens>>> {
|
||||
let file_id = params.text_document.try_conv_with(&world)?;
|
||||
let line_index = world.analysis().file_line_index(file_id);
|
||||
|
||||
let mut lenses: Vec<CodeLens> = Default::default();
|
||||
|
||||
for runnable in world.analysis().runnables(file_id)? {
|
||||
match &runnable.kind {
|
||||
RunnableKind::Test { name: _ } | RunnableKind::TestMod { path: _ } => {
|
||||
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: "Run Test".into(),
|
||||
command: "ra-lsp.run-single".into(),
|
||||
arguments: Some(vec![to_value(r).unwrap()]),
|
||||
}),
|
||||
data: None,
|
||||
};
|
||||
|
||||
lenses.push(lens);
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
}
|
||||
|
||||
return Ok(Some(lenses));
|
||||
}
|
||||
|
||||
pub fn handle_document_highlight(
|
||||
world: ServerWorld,
|
||||
params: req::TextDocumentPositionParams,
|
||||
|
@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
|
||||
use url_serde;
|
||||
|
||||
pub use languageserver_types::{
|
||||
notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CompletionParams,
|
||||
CompletionResponse, DocumentOnTypeFormattingParams, DocumentSymbolParams,
|
||||
notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens, CodeLensParams,
|
||||
CompletionParams, CompletionResponse, DocumentOnTypeFormattingParams, DocumentSymbolParams,
|
||||
DocumentSymbolResponse, ExecuteCommandParams, Hover, InitializeResult,
|
||||
PublishDiagnosticsParams, ReferenceParams, SignatureHelp, TextDocumentEdit,
|
||||
TextDocumentPositionParams, TextEdit, WorkspaceEdit, WorkspaceSymbolParams,
|
||||
|
@ -103,3 +103,19 @@ export async function handle() {
|
||||
return await vscode.tasks.executeTask(task);
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleSingle(runnable: Runnable) {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor == null || editor.document.languageId !== 'rust') {
|
||||
return;
|
||||
}
|
||||
|
||||
const task = createTask(runnable);
|
||||
task.group = vscode.TaskGroup.Build;
|
||||
task.presentationOptions = {
|
||||
reveal: vscode.TaskRevealKind.Always,
|
||||
panel: vscode.TaskPanelKind.Dedicated,
|
||||
};
|
||||
|
||||
return vscode.tasks.executeTask(task);
|
||||
}
|
@ -55,6 +55,9 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
);
|
||||
overrideCommand('type', commands.onEnter.handle);
|
||||
|
||||
// Unlike the above this does not send requests to the language server
|
||||
registerCommand('ra-lsp.run-single', commands.runnables.handleSingle);
|
||||
|
||||
// Notifications are events triggered by the language server
|
||||
const allNotifications: Iterable<
|
||||
[string, lc.GenericNotificationHandler]
|
||||
|
Loading…
x
Reference in New Issue
Block a user