diff --git a/apps/vscode/src/core/hover.ts b/apps/vscode/src/core/hover.ts index b0825488..6f1a484a 100644 --- a/apps/vscode/src/core/hover.ts +++ b/apps/vscode/src/core/hover.ts @@ -15,17 +15,18 @@ import { Hover, MarkdownString, MarkedString, Position, SignatureHelp, commands } from "vscode"; -import { VirtualDocUri, adjustedPosition, unadjustedRange } from "../vdoc/vdoc"; +import { adjustedPosition, unadjustedRange } from "../vdoc/vdoc"; import { EmbeddedLanguage } from "../vdoc/languages"; +import { Uri } from "vscode"; export async function getHover( - vdocUri: VirtualDocUri, + uri: Uri, language: EmbeddedLanguage, position: Position ) { const hovers = await commands.executeCommand( "vscode.executeHoverProvider", - vdocUri.uri, + uri, adjustedPosition(language, position) ); if (hovers && hovers.length > 0) { @@ -45,14 +46,14 @@ export async function getHover( } export async function getSignatureHelpHover( - vdocUri: VirtualDocUri, + uri: Uri, language: EmbeddedLanguage, position: Position, triggerCharacter?: string ) { return await commands.executeCommand( "vscode.executeSignatureHelpProvider", - vdocUri.uri, + uri, adjustedPosition(language, position), triggerCharacter ); diff --git a/apps/vscode/src/host/hooks.ts b/apps/vscode/src/host/hooks.ts index d78da787..fca9b9f0 100644 --- a/apps/vscode/src/host/hooks.ts +++ b/apps/vscode/src/host/hooks.ts @@ -20,7 +20,7 @@ import { ExtensionHost, HostWebviewPanel, HostStatementRangeProvider, HostHelpTo import { CellExecutor, cellExecutorForLanguage, executableLanguages, isKnitrDocument, pythonWithReticulate } from './executors'; import { ExecuteQueue } from './execute-queue'; import { MarkdownEngine } from '../markdown/engine'; -import { virtualDoc, virtualDocUri, adjustedPosition, unadjustedRange } from "../vdoc/vdoc"; +import { virtualDoc, virtualDocUri, adjustedPosition, unadjustedRange, withVirtualDocUri } from "../vdoc/vdoc"; import { EmbeddedLanguage } from '../vdoc/languages'; declare global { @@ -210,22 +210,16 @@ class EmbeddedHelpTopicProvider implements HostHelpTopicProvider { position: vscode.Position, token: vscode.CancellationToken): Promise { const vdoc = await virtualDoc(document, position, this._engine); + if (vdoc) { - const vdocUri = await virtualDocUri(vdoc, document.uri, "helpTopic"); - try { + return await withVirtualDocUri(vdoc, document.uri, "helpTopic", async (uri: vscode.Uri) => { return await vscode.commands.executeCommand( "positron.executeHelpTopicProvider", - vdocUri.uri, + uri, adjustedPosition(vdoc.language, position), vdoc.language ); - } catch (error) { - return undefined; - } finally { - if (vdocUri.cleanup) { - await vdocUri.cleanup(); - } - } + }); } else { return undefined; } diff --git a/apps/vscode/src/lsp/client.ts b/apps/vscode/src/lsp/client.ts index 0203ab07..8d4f8d97 100644 --- a/apps/vscode/src/lsp/client.ts +++ b/apps/vscode/src/lsp/client.ts @@ -227,7 +227,7 @@ function embeddedHoverProvider(engine: MarkdownEngine) { // execute hover try { - return getHover(vdocUri, vdoc.language, position); + return getHover(vdocUri.uri, vdoc.language, position); } catch (error) { console.log(error); } finally { @@ -254,7 +254,7 @@ function embeddedSignatureHelpProvider(engine: MarkdownEngine) { if (vdoc) { const vdocUri = await virtualDocUri(vdoc, document.uri, "signature"); try { - return getSignatureHelpHover(vdocUri, vdoc.language, position, context.triggerCharacter); + return getSignatureHelpHover(vdocUri.uri, vdoc.language, position, context.triggerCharacter); } catch (error) { return undefined; } finally { diff --git a/apps/vscode/src/providers/assist/render-assist.ts b/apps/vscode/src/providers/assist/render-assist.ts index a067d969..46e0159d 100644 --- a/apps/vscode/src/providers/assist/render-assist.ts +++ b/apps/vscode/src/providers/assist/render-assist.ts @@ -110,13 +110,13 @@ export async function renderCodeViewAssist( const language = embeddedLanguage(context.language); if (language) { const vdoc = virtualDocForCode(context.code, language); - const vdocUri = await virtualDocUri(vdoc, Uri.file(context.filepath), "hover"); - return await withVirtualDocUri(vdocUri, async () => { + const parentUri = Uri.file(context.filepath); + return await withVirtualDocUri(vdoc, parentUri, "hover", async (uri: Uri) => { try { const position = new Position(context.selection.start.line, context.selection.start.character); // check for hover - const hover = await getHover(vdocUri, language, position); + const hover = await getHover(uri, language, position); if (hover) { const assist = getAssistFromHovers([hover], asWebviewUri); if (assist) { @@ -129,7 +129,7 @@ export async function renderCodeViewAssist( } // check for signature tip - const signatureHover = await getSignatureHelpHover(vdocUri, language, position); + const signatureHover = await getSignatureHelpHover(uri, language, position); if (signatureHover) { return getAssistFromSignatureHelp(signatureHover); } diff --git a/apps/vscode/src/providers/format.ts b/apps/vscode/src/providers/format.ts index d36b07be..acb0c691 100644 --- a/apps/vscode/src/providers/format.ts +++ b/apps/vscode/src/providers/format.ts @@ -178,8 +178,7 @@ async function executeFormatDocumentProvider( document: TextDocument, options: FormattingOptions ): Promise { - const vdocUri = await virtualDocUri(vdoc, document.uri, "format"); - const edits = await withVirtualDocUri(vdocUri, async (uri: Uri) => { + const edits = await withVirtualDocUri(vdoc, document.uri, "format", async (uri: Uri) => { return await commands.executeCommand( "vscode.executeFormatDocumentProvider", uri, diff --git a/apps/vscode/src/vdoc/vdoc-completion.ts b/apps/vscode/src/vdoc/vdoc-completion.ts index 84310c74..a8f2b0fd 100644 --- a/apps/vscode/src/vdoc/vdoc-completion.ts +++ b/apps/vscode/src/vdoc/vdoc-completion.ts @@ -24,10 +24,7 @@ export async function vdocCompletions( language: EmbeddedLanguage, parentUri: Uri ) { - - const vdocUri = await virtualDocUri(vdoc, parentUri, "completion"); - - const completions = await withVirtualDocUri(vdocUri, async (uri: Uri) => { + const completions = await withVirtualDocUri(vdoc, parentUri, "completion", async (uri: Uri) => { return await commands.executeCommand( "vscode.executeCompletionItemProvider", uri, diff --git a/apps/vscode/src/vdoc/vdoc.ts b/apps/vscode/src/vdoc/vdoc.ts index 33f36791..bfe942a9 100644 --- a/apps/vscode/src/vdoc/vdoc.ts +++ b/apps/vscode/src/vdoc/vdoc.ts @@ -122,12 +122,19 @@ export type VirtualDocAction = export type VirtualDocUri = { uri: Uri, cleanup?: () => Promise }; -export async function withVirtualDocUri(virtualDocUri: VirtualDocUri, f: (uri: Uri) => Promise) { +export async function withVirtualDocUri( + vdoc: VirtualDoc, + parentUri: Uri, + action: VirtualDocAction, + f: (uri: Uri) => Promise +) { + const vdocUri = await virtualDocUri(vdoc, parentUri, action); + try { - return await f(virtualDocUri.uri); + return await f(vdocUri.uri); } finally { - if (virtualDocUri.cleanup) { - virtualDocUri.cleanup(); + if (vdocUri.cleanup) { + vdocUri.cleanup(); } } }