diff --git a/README.md b/README.md index a248ce3b..09693f69 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,7 @@ You can use these commands by `:CocCommand XYZ`. | rust-analyzer.cancelFlycheck | Cancel running flychecks | | rust-analyzer.clearFlycheck | Clear flycheck diagnostics | | rust-analyzer.rebuildProcMacros | Rebuild proc macros and build scripts | +| rust-analyzer.interpretFunction | Interpret Function | ## License diff --git a/package.json b/package.json index 4703e5a0..8e4cb721 100644 --- a/package.json +++ b/package.json @@ -1300,6 +1300,11 @@ "title": "Rebuild proc macros and build scripts", "category": "rust-analyzer" }, + { + "command": "rust-analyzer.interpretFunction", + "title": "Interpret Function", + "category": "rust-analyzer" + }, { "command": "rust-analyzer.explainError", "title": "Explain the currently hovered diagnostic", diff --git a/src/commands.ts b/src/commands.ts index 4ff1ded5..465e3514 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -722,6 +722,27 @@ export function viewMir(ctx: Ctx): Cmd { return viewXir(ctx, 'MIR'); } +export function interpretFunction(ctx: Ctx): Cmd { + return async () => { + const { document, position } = await workspace.getCurrentState(); + if (!isRustDocument(document)) return; + + const param: TextDocumentPositionParams = { + textDocument: { uri: document.uri }, + position, + }; + const ret = await ctx.client.sendRequest(ra.interpretFunction, param); + if (!ret) return; + const nvim = workspace.nvim; + nvim.pauseNotification(); + nvim.command(`edit +setl\\ buftype=nofile [interpretFunction]`, true); + nvim.command('setl nobuflisted bufhidden=wipe', true); + nvim.call('append', [0, ret.split('\n')], true); + nvim.command(`exe 1`, true); + await nvim.resumeNotification(true); + }; +} + export function viewFileText(ctx: Ctx): Cmd { return async () => { const { document } = await workspace.getCurrentState(); diff --git a/src/index.ts b/src/index.ts index 8875dbb1..8d2f375f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,6 +74,7 @@ export async function activate(context: ExtensionContext): Promise { ctx.registerCommand('clearFlycheck', cmds.clearFlycheck); ctx.registerCommand('analyzerStatus', cmds.analyzerStatus); ctx.registerCommand('viewCrateGraph', cmds.viewCrateGraph); + ctx.registerCommand('interpretFunction', cmds.interpretFunction); ctx.registerCommand('rebuildProcMacros', cmds.rebuildProcMacros); ctx.registerCommand('shuffleCrateGraph', cmds.shuffleCrateGraph); ctx.registerCommand('viewFullCrateGraph', cmds.viewFullCrateGraph);