diff options
author | vsrs <[email protected]> | 2021-02-27 17:04:43 +0000 |
---|---|---|
committer | vsrs <[email protected]> | 2021-02-27 17:04:43 +0000 |
commit | 669e11764430be3a098d6c8fe875d8efbb3547a3 (patch) | |
tree | 652dc3728183ea67624116b27f36008677ae9f2a | |
parent | 31f5f816e3747c1a0972d2f0aca25ded9980cd36 (diff) |
Add LSP request and VSCode command
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 18 | ||||
-rw-r--r-- | crates/rust-analyzer/src/lsp_ext.rs | 20 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop.rs | 1 | ||||
-rw-r--r-- | editors/code/package.json | 5 | ||||
-rw-r--r-- | editors/code/src/commands.ts | 51 | ||||
-rw-r--r-- | editors/code/src/lsp_ext.ts | 9 | ||||
-rw-r--r-- | editors/code/src/main.ts | 1 |
7 files changed, 96 insertions, 9 deletions
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index f198b1f25..53b0d3e41 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -607,6 +607,24 @@ pub(crate) fn handle_runnables( | |||
607 | Ok(res) | 607 | Ok(res) |
608 | } | 608 | } |
609 | 609 | ||
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 | |||
610 | pub(crate) fn handle_completion( | 628 | pub(crate) fn handle_completion( |
611 | snap: GlobalStateSnapshot, | 629 | snap: GlobalStateSnapshot, |
612 | params: lsp_types::CompletionParams, | 630 | params: lsp_types::CompletionParams, |
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 0d2c8f7ff..fe11903f9 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs | |||
@@ -177,6 +177,26 @@ pub struct CargoRunnable { | |||
177 | pub expect_test: Option<bool>, | 177 | pub expect_test: Option<bool>, |
178 | } | 178 | } |
179 | 179 | ||
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 | |||
180 | pub enum InlayHints {} | 200 | pub enum InlayHints {} |
181 | 201 | ||
182 | impl Request for InlayHints { | 202 | impl Request for InlayHints { |
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 2829d5970..9f86b8c0d 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -500,6 +500,7 @@ impl GlobalState { | |||
500 | .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro) | 500 | .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro) |
501 | .on::<lsp_ext::ParentModule>(handlers::handle_parent_module) | 501 | .on::<lsp_ext::ParentModule>(handlers::handle_parent_module) |
502 | .on::<lsp_ext::Runnables>(handlers::handle_runnables) | 502 | .on::<lsp_ext::Runnables>(handlers::handle_runnables) |
503 | .on::<lsp_ext::RelatedTests>(handlers::handle_related_tests) | ||
503 | .on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints) | 504 | .on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints) |
504 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action) | 505 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action) |
505 | .on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve) | 506 | .on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve) |
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 @@ | |||
202 | "command": "rust-analyzer.openCargoToml", | 202 | "command": "rust-analyzer.openCargoToml", |
203 | "title": "Open Cargo.toml", | 203 | "title": "Open Cargo.toml", |
204 | "category": "Rust Analyzer" | 204 | "category": "Rust Analyzer" |
205 | }, | ||
206 | { | ||
207 | "command": "rust-analyzer.peekTests", | ||
208 | "title": "Peek related tests", | ||
209 | "category": "Rust Analyzer" | ||
205 | } | 210 | } |
206 | ], | 211 | ], |
207 | "keybindings": [ | 212 | "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 | |||
9 | import { AstInspector } from './ast_inspector'; | 9 | import { AstInspector } from './ast_inspector'; |
10 | import { isRustDocument, sleep, isRustEditor } from './util'; | 10 | import { isRustDocument, sleep, isRustEditor } from './util'; |
11 | import { startDebugSession, makeDebugConfig } from './debug'; | 11 | import { startDebugSession, makeDebugConfig } from './debug'; |
12 | import { LanguageClient } from 'vscode-languageclient/node'; | ||
12 | 13 | ||
13 | export * from './ast_inspector'; | 14 | export * from './ast_inspector'; |
14 | export * from './run'; | 15 | export * from './run'; |
@@ -456,17 +457,20 @@ export function reloadWorkspace(ctx: Ctx): Cmd { | |||
456 | return async () => ctx.client.sendRequest(ra.reloadWorkspace); | 457 | return async () => ctx.client.sendRequest(ra.reloadWorkspace); |
457 | } | 458 | } |
458 | 459 | ||
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 | |||
459 | export function showReferences(ctx: Ctx): Cmd { | 471 | export function showReferences(ctx: Ctx): Cmd { |
460 | return async (uri: string, position: lc.Position, locations: lc.Location[]) => { | 472 | return async (uri: string, position: lc.Position, locations: lc.Location[]) => { |
461 | const client = ctx.client; | 473 | await showReferencesImpl(ctx.client, uri, position, locations); |
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 | } | ||
470 | }; | 474 | }; |
471 | } | 475 | } |
472 | 476 | ||
@@ -555,6 +559,35 @@ export function run(ctx: Ctx): Cmd { | |||
555 | }; | 559 | }; |
556 | } | 560 | } |
557 | 561 | ||
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 | |||
558 | export function runSingle(ctx: Ctx): Cmd { | 591 | export function runSingle(ctx: Ctx): Cmd { |
559 | return async (runnable: ra.Runnable) => { | 592 | return async (runnable: ra.Runnable) => { |
560 | const editor = ctx.activeRustEditor; | 593 | 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 { | |||
72 | } | 72 | } |
73 | export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("experimental/runnables"); | 73 | export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("experimental/runnables"); |
74 | 74 | ||
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 | |||
75 | export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; | 84 | export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; |
76 | 85 | ||
77 | export namespace InlayHint { | 86 | 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) { | |||
113 | ctx.registerCommand('newDebugConfig', commands.newDebugConfig); | 113 | ctx.registerCommand('newDebugConfig', commands.newDebugConfig); |
114 | ctx.registerCommand('openDocs', commands.openDocs); | 114 | ctx.registerCommand('openDocs', commands.openDocs); |
115 | ctx.registerCommand('openCargoToml', commands.openCargoToml); | 115 | ctx.registerCommand('openCargoToml', commands.openCargoToml); |
116 | ctx.registerCommand('peekTests', commands.peekTests); | ||
116 | 117 | ||
117 | defaultOnEnter.dispose(); | 118 | defaultOnEnter.dispose(); |
118 | ctx.registerCommand('onEnter', commands.onEnter); | 119 | ctx.registerCommand('onEnter', commands.onEnter); |