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

Add Positron help topic provider #599

Merged
merged 6 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions apps/vscode/src/@types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ declare module 'positron' {
selector: vscode.DocumentSelector,
provider: StatementRangeProvider
): vscode.Disposable;
registerHelpTopicProvider(
selector: vscode.DocumentSelector,
provider: HelpTopicProvider
): vscode.Disposable;
}

export interface StatementRangeProvider {
Expand All @@ -35,6 +39,14 @@ declare module 'positron' {
): vscode.ProviderResult<StatementRange>;
}

export interface HelpTopicProvider {
provideHelpTopic(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): vscode.ProviderResult<string>;
}

export interface StatementRange {
readonly range: vscode.Range;
readonly code?: string;
Expand Down
47 changes: 46 additions & 1 deletion apps/vscode/src/host/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import * as vscode from 'vscode';
import * as hooks from 'positron';

import { ExtensionHost, HostWebviewPanel, HostStatementRangeProvider } from '.';
import { ExtensionHost, HostWebviewPanel, HostStatementRangeProvider, HostHelpTopicProvider } from '.';
import { CellExecutor, cellExecutorForLanguage, executableLanguages, isKnitrDocument, pythonWithReticulate } from './executors';
import { ExecuteQueue } from './execute-queue';
import { MarkdownEngine } from '../markdown/engine';
Expand Down Expand Up @@ -99,6 +99,15 @@ export function hooksExtensionHost(): ExtensionHost {
return new vscode.Disposable(() => { });
},

registerHelpTopicProvider: (engine: MarkdownEngine): vscode.Disposable => {
const hooks = hooksApi();
if (hooks) {
return hooks.languages.registerHelpTopicProvider('quarto',
new EmbeddedHelpTopicProvider(engine));
}
return new vscode.Disposable(() => { });
},

createPreviewPanel: (
viewType: string,
title: string,
Expand Down Expand Up @@ -186,3 +195,39 @@ async function getStatementRange(
);
return { range: unadjustedRange(language, result.range), code: result.code };
}

class EmbeddedHelpTopicProvider implements HostHelpTopicProvider {
private readonly _engine: MarkdownEngine;

constructor(
readonly engine: MarkdownEngine,
) {
this._engine = engine;
}

async provideHelpTopic(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken): Promise<string | undefined> {
const vdoc = await virtualDoc(document, position, this._engine);
juliasilge marked this conversation as resolved.
Show resolved Hide resolved
if (vdoc) {
const vdocUri = await virtualDocUri(vdoc, document.uri, "helpTopic");
try {
return await vscode.commands.executeCommand<string>(
"positron.executeHelpTopicProvider",
vdocUri.uri,
adjustedPosition(vdoc.language, position),
vdoc.language
);
} catch (error) {
return undefined;
} finally {
if (vdocUri.cleanup) {
await vdocUri.cleanup();
}
}
} else {
return undefined;
}
};
}
18 changes: 17 additions & 1 deletion apps/vscode/src/host/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export interface HostStatementRange {
readonly code?: string;
}

export interface HostHelpTopicProvider {
provideHelpTopic(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): vscode.ProviderResult<string>;
}

export interface ExtensionHost {

// code execution
Expand All @@ -63,6 +71,11 @@ export interface ExtensionHost {
engine: MarkdownEngine,
): vscode.Disposable;

// help topic provider
registerHelpTopicProvider(
engine: MarkdownEngine,
): vscode.Disposable;

// preview
createPreviewPanel(
viewType: string,
Expand Down Expand Up @@ -99,10 +112,13 @@ function defaultExtensionHost(): ExtensionHost {
return languages.filter(language => knitr || !visualMode || (language !== "python"));
},
cellExecutorForLanguage,
// in the default extension host, this is a noop:
// in the default extension host, both of these are just a noop:
registerStatementRangeProvider: (engine: MarkdownEngine): vscode.Disposable => {
return new vscode.Disposable(() => { });
},
registerHelpTopicProvider: (engine: MarkdownEngine): vscode.Disposable => {
return new vscode.Disposable(() => { });
},
createPreviewPanel,
};
}
1 change: 1 addition & 0 deletions apps/vscode/src/lsp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export async function activateLsp(
middleware.provideSignatureHelp = embeddedSignatureHelpProvider(engine);
}
extensionHost().registerStatementRangeProvider(engine);
extensionHost().registerHelpTopicProvider(engine);

// create client options
const initializationOptions: LspInitializationOptions = {
Expand Down
3 changes: 2 additions & 1 deletion apps/vscode/src/vdoc/vdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export type VirtualDocAction =
"signature" |
"definition" |
"format" |
"statementRange";
"statementRange" |
"helpTopic";

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

Expand Down