Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help topic provider with doc #601

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions apps/vscode/src/core/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hover[]>(
"vscode.executeHoverProvider",
vdocUri.uri,
uri,
adjustedPosition(language, position)
);
if (hovers && hovers.length > 0) {
Expand All @@ -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<SignatureHelp>(
"vscode.executeSignatureHelpProvider",
vdocUri.uri,
uri,
adjustedPosition(language, position),
triggerCharacter
);
Expand Down
16 changes: 5 additions & 11 deletions apps/vscode/src/host/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -210,22 +210,16 @@ class EmbeddedHelpTopicProvider implements HostHelpTopicProvider {
position: vscode.Position,
token: vscode.CancellationToken): Promise<string | undefined> {
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<string>(
"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;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/vscode/src/lsp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions apps/vscode/src/providers/assist/render-assist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Assist | undefined>(vdocUri, async () => {
const parentUri = Uri.file(context.filepath);
return await withVirtualDocUri<Assist | undefined>(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) {
Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions apps/vscode/src/providers/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ async function executeFormatDocumentProvider(
document: TextDocument,
options: FormattingOptions
): Promise<TextEdit[] | undefined> {
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<TextEdit[]>(
"vscode.executeFormatDocumentProvider",
uri,
Expand Down
5 changes: 1 addition & 4 deletions apps/vscode/src/vdoc/vdoc-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletionList>(
"vscode.executeCompletionItemProvider",
uri,
Expand Down
15 changes: 11 additions & 4 deletions apps/vscode/src/vdoc/vdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,19 @@ export type VirtualDocAction =

export type VirtualDocUri = { uri: Uri, cleanup?: () => Promise<void> };

export async function withVirtualDocUri<T>(virtualDocUri: VirtualDocUri, f: (uri: Uri) => Promise<T>) {
export async function withVirtualDocUri<T>(
vdoc: VirtualDoc,
parentUri: Uri,
action: VirtualDocAction,
f: (uri: Uri) => Promise<T>
) {
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();
}
}
}
Expand Down