From 669e11764430be3a098d6c8fe875d8efbb3547a3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 27 Feb 2021 20:04:43 +0300 Subject: Add LSP request and VSCode command --- editors/code/package.json | 5 +++++ editors/code/src/commands.ts | 51 ++++++++++++++++++++++++++++++++++++-------- editors/code/src/lsp_ext.ts | 9 ++++++++ editors/code/src/main.ts | 1 + 4 files changed, 57 insertions(+), 9 deletions(-) (limited to 'editors') diff --git a/editors/code/package.json b/editors/code/package.json index e3e0ebff0..43ea1225a 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -202,6 +202,11 @@ "command": "rust-analyzer.openCargoToml", "title": "Open Cargo.toml", "category": "Rust Analyzer" + }, + { + "command": "rust-analyzer.peekTests", + "title": "Peek related tests", + "category": "Rust Analyzer" } ], "keybindings": [ diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 283b9a160..3512fefdf 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -9,6 +9,7 @@ import { RunnableQuickPick, selectRunnable, createTask, createArgs } from './run import { AstInspector } from './ast_inspector'; import { isRustDocument, sleep, isRustEditor } from './util'; import { startDebugSession, makeDebugConfig } from './debug'; +import { LanguageClient } from 'vscode-languageclient/node'; export * from './ast_inspector'; export * from './run'; @@ -456,17 +457,20 @@ export function reloadWorkspace(ctx: Ctx): Cmd { return async () => ctx.client.sendRequest(ra.reloadWorkspace); } +async function showReferencesImpl(client: LanguageClient, uri: string, position: lc.Position, locations: lc.Location[]) { + if (client) { + await vscode.commands.executeCommand( + 'editor.action.showReferences', + vscode.Uri.parse(uri), + client.protocol2CodeConverter.asPosition(position), + locations.map(client.protocol2CodeConverter.asLocation), + ); + } +} + export function showReferences(ctx: Ctx): Cmd { return async (uri: string, position: lc.Position, locations: lc.Location[]) => { - const client = ctx.client; - if (client) { - await vscode.commands.executeCommand( - 'editor.action.showReferences', - vscode.Uri.parse(uri), - client.protocol2CodeConverter.asPosition(position), - locations.map(client.protocol2CodeConverter.asLocation), - ); - } + await showReferencesImpl(ctx.client, uri, position, locations); }; } @@ -555,6 +559,35 @@ export function run(ctx: Ctx): Cmd { }; } +export function peekTests(ctx: Ctx): Cmd { + const client = ctx.client; + + return async () => { + const editor = ctx.activeRustEditor; + if (!editor || !client) return; + + const uri = editor.document.uri.toString(); + const position = client.code2ProtocolConverter.asPosition( + editor.selection.active, + ); + + const tests = await client.sendRequest(ra.relatedTests, { + textDocument: { uri: uri }, + position: position, + }); + + const locations: lc.Location[] = tests.map( it => { + return { + uri: it.runnable.location!.targetUri, + range: it.runnable.location!.targetSelectionRange + }; + }); + + await showReferencesImpl(client, uri, position, locations); + }; +} + + export function runSingle(ctx: Ctx): Cmd { return async (runnable: ra.Runnable) => { const editor = ctx.activeRustEditor; diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 2de1e427d..11d4d5f00 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -72,6 +72,15 @@ export interface Runnable { } export const runnables = new lc.RequestType("experimental/runnables"); +export interface RelatedTestsParams extends lc.TextDocumentPositionParams { +} + +export interface TestInfo { + runnable: Runnable; +} + +export const relatedTests = new lc.RequestType("rust-analyzer/relatedTests"); + export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; export namespace InlayHint { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 00393d6e8..f1a2020aa 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -113,6 +113,7 @@ async function tryActivate(context: vscode.ExtensionContext) { ctx.registerCommand('newDebugConfig', commands.newDebugConfig); ctx.registerCommand('openDocs', commands.openDocs); ctx.registerCommand('openCargoToml', commands.openCargoToml); + ctx.registerCommand('peekTests', commands.peekTests); defaultOnEnter.dispose(); ctx.registerCommand('onEnter', commands.onEnter); -- cgit v1.2.3 From 45d4e6b639b627ef9926ecfbc150cdfe8c292ae1 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 27 Feb 2021 21:07:23 +0300 Subject: Add progress reporting --- editors/code/package.json | 9 ++++++++- editors/code/src/commands.ts | 33 +++++++++++++++++---------------- 2 files changed, 25 insertions(+), 17 deletions(-) (limited to 'editors') diff --git a/editors/code/package.json b/editors/code/package.json index 43ea1225a..7ee5d82ad 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1164,7 +1164,14 @@ "command": "rust-analyzer.openCargoToml", "when": "inRustProject" } + ], + "editor/context": [ + { + "command": "rust-analyzer.peekTests", + "when": "inRustProject", + "group": "navigation@1000" + } ] } } -} +} \ No newline at end of file diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 3512fefdf..d43db7307 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -566,24 +566,25 @@ export function peekTests(ctx: Ctx): Cmd { const editor = ctx.activeRustEditor; if (!editor || !client) return; - const uri = editor.document.uri.toString(); - const position = client.code2ProtocolConverter.asPosition( - editor.selection.active, - ); - - const tests = await client.sendRequest(ra.relatedTests, { - textDocument: { uri: uri }, - position: position, - }); + await vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: "Looking for tests...", + cancellable: false, + }, async (_progress, _token) => { + const uri = editor.document.uri.toString(); + const position = client.code2ProtocolConverter.asPosition( + editor.selection.active, + ); - const locations: lc.Location[] = tests.map( it => { - return { - uri: it.runnable.location!.targetUri, - range: it.runnable.location!.targetSelectionRange - }; - }); + const tests = await client.sendRequest(ra.relatedTests, { + textDocument: { uri: uri }, + position: position, + }); + const locations: lc.Location[] = tests.map(it => + lc.Location.create(it.runnable.location!.targetUri, it.runnable.location!.targetSelectionRange)); - await showReferencesImpl(client, uri, position, locations); + await showReferencesImpl(client, uri, position, locations); + }); }; } -- cgit v1.2.3 From f234b80520ed63b168475be38086d053567f4c1e Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 27 Feb 2021 21:57:58 +0300 Subject: Remove erroneous comment --- editors/code/src/lsp_ext.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors') diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 11d4d5f00..f0e4be279 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -72,7 +72,7 @@ export interface Runnable { } export const runnables = new lc.RequestType("experimental/runnables"); -export interface RelatedTestsParams extends lc.TextDocumentPositionParams { +export interface RelatedTestsParams extends lc.TextDocumentPositionParams { } export interface TestInfo { -- cgit v1.2.3 From daa2637486755f012c738d3516ff7cb3d3dcd234 Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 11 Mar 2021 17:39:41 +0300 Subject: Apply review suggestions --- editors/code/src/lsp_ext.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'editors') diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index f0e4be279..52de29e04 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -72,14 +72,11 @@ export interface Runnable { } export const runnables = new lc.RequestType("experimental/runnables"); -export interface RelatedTestsParams extends lc.TextDocumentPositionParams { -} - export interface TestInfo { runnable: Runnable; } -export const relatedTests = new lc.RequestType("rust-analyzer/relatedTests"); +export const relatedTests = new lc.RequestType("rust-analyzer/relatedTests"); export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; -- cgit v1.2.3