Skip to content

Commit 669e117

Browse files
committed
Add LSP request and VSCode command
1 parent 31f5f81 commit 669e117

File tree

7 files changed

+96
-9
lines changed

7 files changed

+96
-9
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,24 @@ pub(crate) fn handle_runnables(
607607
Ok(res)
608608
}
609609

610+
pub(crate) fn handle_related_tests(
611+
snap: GlobalStateSnapshot,
612+
params: lsp_ext::RelatedTestsParams,
613+
) -> Result<Vec<lsp_ext::TestInfo>> {
614+
let _p = profile::span("handle_related_tests");
615+
let position = from_proto::file_position(&snap, params.text_document_position)?;
616+
617+
let tests = snap.analysis.related_tests(position, None)?;
618+
let mut res = Vec::new();
619+
for it in tests {
620+
if let Ok(runnable) = to_proto::runnable(&snap, it) {
621+
res.push(lsp_ext::TestInfo { runnable })
622+
}
623+
}
624+
625+
Ok(res)
626+
}
627+
610628
pub(crate) fn handle_completion(
611629
snap: GlobalStateSnapshot,
612630
params: lsp_types::CompletionParams,

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ pub struct CargoRunnable {
177177
pub expect_test: Option<bool>,
178178
}
179179

180+
pub enum RelatedTests {}
181+
182+
impl Request for RelatedTests {
183+
type Params = RelatedTestsParams;
184+
type Result = Vec<TestInfo>;
185+
const METHOD: &'static str = "rust-analyzer/relatedTests";
186+
}
187+
188+
#[derive(Serialize, Deserialize, Debug)]
189+
#[serde(rename_all = "camelCase")]
190+
pub struct RelatedTestsParams {
191+
#[serde(flatten)]
192+
pub text_document_position: lsp_types::TextDocumentPositionParams,
193+
}
194+
195+
#[derive(Debug, Deserialize, Serialize)]
196+
pub struct TestInfo {
197+
pub runnable: Runnable,
198+
}
199+
180200
pub enum InlayHints {}
181201

182202
impl Request for InlayHints {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ impl GlobalState {
500500
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
501501
.on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
502502
.on::<lsp_ext::Runnables>(handlers::handle_runnables)
503+
.on::<lsp_ext::RelatedTests>(handlers::handle_related_tests)
503504
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)
504505
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
505506
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@
202202
"command": "rust-analyzer.openCargoToml",
203203
"title": "Open Cargo.toml",
204204
"category": "Rust Analyzer"
205+
},
206+
{
207+
"command": "rust-analyzer.peekTests",
208+
"title": "Peek related tests",
209+
"category": "Rust Analyzer"
205210
}
206211
],
207212
"keybindings": [

editors/code/src/commands.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { RunnableQuickPick, selectRunnable, createTask, createArgs } from './run
99
import { AstInspector } from './ast_inspector';
1010
import { isRustDocument, sleep, isRustEditor } from './util';
1111
import { startDebugSession, makeDebugConfig } from './debug';
12+
import { LanguageClient } from 'vscode-languageclient/node';
1213

1314
export * from './ast_inspector';
1415
export * from './run';
@@ -456,17 +457,20 @@ export function reloadWorkspace(ctx: Ctx): Cmd {
456457
return async () => ctx.client.sendRequest(ra.reloadWorkspace);
457458
}
458459

460+
async function showReferencesImpl(client: LanguageClient, uri: string, position: lc.Position, locations: lc.Location[]) {
461+
if (client) {
462+
await vscode.commands.executeCommand(
463+
'editor.action.showReferences',
464+
vscode.Uri.parse(uri),
465+
client.protocol2CodeConverter.asPosition(position),
466+
locations.map(client.protocol2CodeConverter.asLocation),
467+
);
468+
}
469+
}
470+
459471
export function showReferences(ctx: Ctx): Cmd {
460472
return async (uri: string, position: lc.Position, locations: lc.Location[]) => {
461-
const client = ctx.client;
462-
if (client) {
463-
await vscode.commands.executeCommand(
464-
'editor.action.showReferences',
465-
vscode.Uri.parse(uri),
466-
client.protocol2CodeConverter.asPosition(position),
467-
locations.map(client.protocol2CodeConverter.asLocation),
468-
);
469-
}
473+
await showReferencesImpl(ctx.client, uri, position, locations);
470474
};
471475
}
472476

@@ -555,6 +559,35 @@ export function run(ctx: Ctx): Cmd {
555559
};
556560
}
557561

562+
export function peekTests(ctx: Ctx): Cmd {
563+
const client = ctx.client;
564+
565+
return async () => {
566+
const editor = ctx.activeRustEditor;
567+
if (!editor || !client) return;
568+
569+
const uri = editor.document.uri.toString();
570+
const position = client.code2ProtocolConverter.asPosition(
571+
editor.selection.active,
572+
);
573+
574+
const tests = await client.sendRequest(ra.relatedTests, {
575+
textDocument: { uri: uri },
576+
position: position,
577+
});
578+
579+
const locations: lc.Location[] = tests.map( it => {
580+
return {
581+
uri: it.runnable.___location!.targetUri,
582+
range: it.runnable.___location!.targetSelectionRange
583+
};
584+
});
585+
586+
await showReferencesImpl(client, uri, position, locations);
587+
};
588+
}
589+
590+
558591
export function runSingle(ctx: Ctx): Cmd {
559592
return async (runnable: ra.Runnable) => {
560593
const editor = ctx.activeRustEditor;

editors/code/src/lsp_ext.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ export interface Runnable {
7272
}
7373
export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("experimental/runnables");
7474

75+
export interface RelatedTestsParams extends lc.TextDocumentPositionParams {
76+
}
77+
78+
export interface TestInfo {
79+
runnable: Runnable;
80+
}
81+
82+
export const relatedTests = new lc.RequestType<RelatedTestsParams, TestInfo[], void>("rust-analyzer/relatedTests");
83+
7584
export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint;
7685

7786
export namespace InlayHint {

editors/code/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
113113
ctx.registerCommand('newDebugConfig', commands.newDebugConfig);
114114
ctx.registerCommand('openDocs', commands.openDocs);
115115
ctx.registerCommand('openCargoToml', commands.openCargoToml);
116+
ctx.registerCommand('peekTests', commands.peekTests);
116117

117118
defaultOnEnter.dispose();
118119
ctx.registerCommand('onEnter', commands.onEnter);

0 commit comments

Comments
 (0)