mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
Add LSP request and VSCode command
This commit is contained in:
parent
31f5f816e3
commit
669e117644
7 changed files with 96 additions and 9 deletions
|
@ -607,6 +607,24 @@ pub(crate) fn handle_runnables(
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn handle_related_tests(
|
||||||
|
snap: GlobalStateSnapshot,
|
||||||
|
params: lsp_ext::RelatedTestsParams,
|
||||||
|
) -> Result<Vec<lsp_ext::TestInfo>> {
|
||||||
|
let _p = profile::span("handle_related_tests");
|
||||||
|
let position = from_proto::file_position(&snap, params.text_document_position)?;
|
||||||
|
|
||||||
|
let tests = snap.analysis.related_tests(position, None)?;
|
||||||
|
let mut res = Vec::new();
|
||||||
|
for it in tests {
|
||||||
|
if let Ok(runnable) = to_proto::runnable(&snap, it) {
|
||||||
|
res.push(lsp_ext::TestInfo { runnable })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn handle_completion(
|
pub(crate) fn handle_completion(
|
||||||
snap: GlobalStateSnapshot,
|
snap: GlobalStateSnapshot,
|
||||||
params: lsp_types::CompletionParams,
|
params: lsp_types::CompletionParams,
|
||||||
|
|
|
@ -177,6 +177,26 @@ pub struct CargoRunnable {
|
||||||
pub expect_test: Option<bool>,
|
pub expect_test: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum RelatedTests {}
|
||||||
|
|
||||||
|
impl Request for RelatedTests {
|
||||||
|
type Params = RelatedTestsParams;
|
||||||
|
type Result = Vec<TestInfo>;
|
||||||
|
const METHOD: &'static str = "rust-analyzer/relatedTests";
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct RelatedTestsParams {
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub text_document_position: lsp_types::TextDocumentPositionParams,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct TestInfo {
|
||||||
|
pub runnable: Runnable,
|
||||||
|
}
|
||||||
|
|
||||||
pub enum InlayHints {}
|
pub enum InlayHints {}
|
||||||
|
|
||||||
impl Request for InlayHints {
|
impl Request for InlayHints {
|
||||||
|
|
|
@ -500,6 +500,7 @@ impl GlobalState {
|
||||||
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
|
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
|
||||||
.on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
|
.on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
|
||||||
.on::<lsp_ext::Runnables>(handlers::handle_runnables)
|
.on::<lsp_ext::Runnables>(handlers::handle_runnables)
|
||||||
|
.on::<lsp_ext::RelatedTests>(handlers::handle_related_tests)
|
||||||
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)
|
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)
|
||||||
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
|
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
|
||||||
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
|
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
|
||||||
|
|
|
@ -202,6 +202,11 @@
|
||||||
"command": "rust-analyzer.openCargoToml",
|
"command": "rust-analyzer.openCargoToml",
|
||||||
"title": "Open Cargo.toml",
|
"title": "Open Cargo.toml",
|
||||||
"category": "Rust Analyzer"
|
"category": "Rust Analyzer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "rust-analyzer.peekTests",
|
||||||
|
"title": "Peek related tests",
|
||||||
|
"category": "Rust Analyzer"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"keybindings": [
|
"keybindings": [
|
||||||
|
|
|
@ -9,6 +9,7 @@ import { RunnableQuickPick, selectRunnable, createTask, createArgs } from './run
|
||||||
import { AstInspector } from './ast_inspector';
|
import { AstInspector } from './ast_inspector';
|
||||||
import { isRustDocument, sleep, isRustEditor } from './util';
|
import { isRustDocument, sleep, isRustEditor } from './util';
|
||||||
import { startDebugSession, makeDebugConfig } from './debug';
|
import { startDebugSession, makeDebugConfig } from './debug';
|
||||||
|
import { LanguageClient } from 'vscode-languageclient/node';
|
||||||
|
|
||||||
export * from './ast_inspector';
|
export * from './ast_inspector';
|
||||||
export * from './run';
|
export * from './run';
|
||||||
|
@ -456,9 +457,7 @@ export function reloadWorkspace(ctx: Ctx): Cmd {
|
||||||
return async () => ctx.client.sendRequest(ra.reloadWorkspace);
|
return async () => ctx.client.sendRequest(ra.reloadWorkspace);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showReferences(ctx: Ctx): Cmd {
|
async function showReferencesImpl(client: LanguageClient, uri: string, position: lc.Position, locations: lc.Location[]) {
|
||||||
return async (uri: string, position: lc.Position, locations: lc.Location[]) => {
|
|
||||||
const client = ctx.client;
|
|
||||||
if (client) {
|
if (client) {
|
||||||
await vscode.commands.executeCommand(
|
await vscode.commands.executeCommand(
|
||||||
'editor.action.showReferences',
|
'editor.action.showReferences',
|
||||||
|
@ -467,6 +466,11 @@ export function showReferences(ctx: Ctx): Cmd {
|
||||||
locations.map(client.protocol2CodeConverter.asLocation),
|
locations.map(client.protocol2CodeConverter.asLocation),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function showReferences(ctx: Ctx): Cmd {
|
||||||
|
return async (uri: string, position: lc.Position, locations: lc.Location[]) => {
|
||||||
|
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 {
|
export function runSingle(ctx: Ctx): Cmd {
|
||||||
return async (runnable: ra.Runnable) => {
|
return async (runnable: ra.Runnable) => {
|
||||||
const editor = ctx.activeRustEditor;
|
const editor = ctx.activeRustEditor;
|
||||||
|
|
|
@ -72,6 +72,15 @@ export interface Runnable {
|
||||||
}
|
}
|
||||||
export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("experimental/runnables");
|
export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("experimental/runnables");
|
||||||
|
|
||||||
|
export interface RelatedTestsParams extends lc.TextDocumentPositionParams {
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TestInfo {
|
||||||
|
runnable: Runnable;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const relatedTests = new lc.RequestType<RelatedTestsParams, TestInfo[], void>("rust-analyzer/relatedTests");
|
||||||
|
|
||||||
export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint;
|
export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint;
|
||||||
|
|
||||||
export namespace InlayHint {
|
export namespace InlayHint {
|
||||||
|
|
|
@ -113,6 +113,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
|
||||||
ctx.registerCommand('newDebugConfig', commands.newDebugConfig);
|
ctx.registerCommand('newDebugConfig', commands.newDebugConfig);
|
||||||
ctx.registerCommand('openDocs', commands.openDocs);
|
ctx.registerCommand('openDocs', commands.openDocs);
|
||||||
ctx.registerCommand('openCargoToml', commands.openCargoToml);
|
ctx.registerCommand('openCargoToml', commands.openCargoToml);
|
||||||
|
ctx.registerCommand('peekTests', commands.peekTests);
|
||||||
|
|
||||||
defaultOnEnter.dispose();
|
defaultOnEnter.dispose();
|
||||||
ctx.registerCommand('onEnter', commands.onEnter);
|
ctx.registerCommand('onEnter', commands.onEnter);
|
||||||
|
|
Loading…
Reference in a new issue