diff --git a/README.md b/README.md index ea5a7fed..6cc1528d 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ You can use these commands by `:CocCommand XYZ`. - `rust-analyzer.expandMacro`: Expand macro recursively - `rust-analyzer.joinLines`: Join lines - `rust-analyzer.matchingBrace`: Find matching brace +- `rust-analyzer.openDocs`: Open docs under cursor - `rust-analyzer.parentModule`: Locate parent module - `rust-analyzer.reload`: Restart rust-analyzer server - `rust-analyzer.run`: List available runnables of current file diff --git a/package.json b/package.json index 3be16c1d..5fef23ee 100644 --- a/package.json +++ b/package.json @@ -461,6 +461,11 @@ "title": "Find matching brace", "category": "Rust Analyzer" }, + { + "command": "rust-analyzer.openDocs", + "title": "Open docs under cursor", + "category": "Rust Analyzer" + }, { "command": "rust-analyzer.parentModule", "title": "Locate parent module", diff --git a/src/commands.ts b/src/commands.ts index 377143ea..134d9521 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -362,3 +362,19 @@ export function resolveCodeAction(ctx: Ctx): Cmd { await applySnippetWorkspaceEdit(edit); }; } + +export function openDocs(ctx: Ctx): Cmd { + return async () => { + const { document, position } = await workspace.getCurrentState(); + if (!isRustDocument(document)) return; + + const param: TextDocumentPositionParams = { + textDocument: { uri: document.uri }, + position, + }; + const doclink = await ctx.client.sendRequest(ra.openDocs, param); + if (doclink) { + await commands.executeCommand('vscode.open', Uri.parse(doclink)); + } + }; +} diff --git a/src/index.ts b/src/index.ts index 8a63e573..eef0efb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,6 +41,7 @@ export async function activate(context: ExtensionContext): Promise { ctx.registerCommand('expandMacro', cmds.expandMacro); ctx.registerCommand('joinLines', cmds.joinLines); ctx.registerCommand('matchingBrace', cmds.matchingBrace); + ctx.registerCommand('openDocs', cmds.openDocs); ctx.registerCommand('parentModule', cmds.parentModule); ctx.registerCommand('run', cmds.run); ctx.registerCommand('runSingle', cmds.runSingle); diff --git a/src/lsp_ext.ts b/src/lsp_ext.ts index da3ba45a..1871571d 100644 --- a/src/lsp_ext.ts +++ b/src/lsp_ext.ts @@ -117,3 +117,5 @@ export interface CommandLinkGroup { title?: string; commands: CommandLink[]; } + +export const openDocs = new lc.RequestType('experimental/externalDocs');