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

Use Positron's statement range provider for Python docs, when available #589

Merged
merged 3 commits into from
Oct 28, 2024
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
2 changes: 2 additions & 0 deletions apps/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.117.0 (unreleased)

- Improved statement execution for Python `.qmd` files in Positron (<https://github.com/quarto-dev/quarto/pull/589>)

## 1.116.0 (Release on 2024-10-08)

- Fix issue with raw html blocks being removed from document by Visual Editor (<https://github.com/quarto-dev/quarto/issues/552>)
Expand Down
91 changes: 49 additions & 42 deletions apps/vscode/src/host/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import { ExtensionHost, HostWebviewPanel, HostStatementRangeProvider } from '.';
import { CellExecutor, cellExecutorForLanguage, executableLanguages, isKnitrDocument, pythonWithReticulate } from './executors';
import { ExecuteQueue } from './execute-queue';
import { MarkdownEngine } from '../markdown/engine';
import { virtualDoc, virtualDocUri, adjustedPosition } from "../vdoc/vdoc";
import { virtualDoc, virtualDocUri, adjustedPosition, unadjustedRange } from "../vdoc/vdoc";
import { EmbeddedLanguage } from '../vdoc/languages';

declare global {
function acquirePositronApi() : hooks.PositronApi;
function acquirePositronApi(): hooks.PositronApi;
}

let api : hooks.PositronApi | null | undefined;
let api: hooks.PositronApi | null | undefined;

export function hooksApi() : hooks.PositronApi | null {
export function hooksApi(): hooks.PositronApi | null {
if (api === undefined) {
try {
api = acquirePositronApi();
Expand All @@ -43,20 +44,20 @@ export function hasHooks() {
return !!hooksApi();
}

export function hooksExtensionHost() : ExtensionHost {
export function hooksExtensionHost(): ExtensionHost {
return {
// supported executable languages (we delegate to the default for langugaes
// w/o runtimes so we support all languages)
executableLanguages,

cellExecutorForLanguage: async (language: string, document: vscode.TextDocument, engine: MarkdownEngine, silent?: boolean)
cellExecutorForLanguage: async (language: string, document: vscode.TextDocument, engine: MarkdownEngine, silent?: boolean)
: Promise<CellExecutor | undefined> => {
switch(language) {
switch (language) {
// use hooks for known runtimes
case "python":
case "r":
return {
execute: async (blocks: string[], _editorUri?: vscode.Uri) : Promise<void> => {
execute: async (blocks: string[], _editorUri?: vscode.Uri): Promise<void> => {
const runtime = hooksApi()?.runtime;

if (runtime === undefined) {
Expand All @@ -68,7 +69,7 @@ export function hooksExtensionHost() : ExtensionHost {
language = "r";
blocks = blocks.map(pythonWithReticulate);
}

// Our callback executes each block sequentially
const callback = async () => {
for (const block of blocks) {
Expand All @@ -78,9 +79,9 @@ export function hooksExtensionHost() : ExtensionHost {

await ExecuteQueue.instance.add(language, callback);
},
executeSelection: async () : Promise<void> => {
await vscode.commands.executeCommand('workbench.action.positronConsole.executeCode', {languageId: language});
}
executeSelection: async (): Promise<void> => {
await vscode.commands.executeCommand('workbench.action.positronConsole.executeCode', { languageId: language });
}
};

// delegate for other languages
Expand All @@ -92,16 +93,16 @@ export function hooksExtensionHost() : ExtensionHost {
registerStatementRangeProvider: (engine: MarkdownEngine): vscode.Disposable => {
const hooks = hooksApi();
if (hooks) {
return hooks.languages.registerStatementRangeProvider('quarto',
return hooks.languages.registerStatementRangeProvider('quarto',
new EmbeddedStatementRangeProvider(engine));
}
return new vscode.Disposable(() => {});
return new vscode.Disposable(() => { });
},

createPreviewPanel: (
viewType: string,
viewType: string,
title: string,
preserveFocus?: boolean,
preserveFocus?: boolean,
options?: vscode.WebviewPanelOptions & vscode.WebviewOptions
): HostWebviewPanel => {

Expand All @@ -117,7 +118,7 @@ export function hooksExtensionHost() : ExtensionHost {
portMapping: options?.portMapping
}
)!;

// adapt to host interface
return new HookWebviewPanel(panel);
}
Expand All @@ -126,7 +127,7 @@ export function hooksExtensionHost() : ExtensionHost {


class HookWebviewPanel implements HostWebviewPanel {
constructor(private readonly panel_: hooks.PreviewPanel) {}
constructor(private readonly panel_: hooks.PreviewPanel) { }

get webview() { return this.panel_.webview; };
get visible() { return this.panel_.visible; };
Expand All @@ -139,43 +140,49 @@ class HookWebviewPanel implements HostWebviewPanel {
}

class EmbeddedStatementRangeProvider implements HostStatementRangeProvider {
private readonly _engine: MarkdownEngine;
private readonly _engine: MarkdownEngine;

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

async provideStatementRange(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken): Promise<hooks.StatementRange | undefined> {
const vdoc = await virtualDoc(document, position, this._engine);
if (vdoc) {
const vdocUri = await virtualDocUri(vdoc, document.uri, "statementRange");
try {
return getStatementRange(vdocUri.uri, adjustedPosition(vdoc.language, position));
} catch (error) {
return undefined;
} finally {
if (vdocUri.cleanup) {
await vdocUri.cleanup();
}
}
} else {
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken): Promise<hooks.StatementRange | undefined> {
const vdoc = await virtualDoc(document, position, this._engine);
if (vdoc) {
const vdocUri = await virtualDocUri(vdoc, document.uri, "statementRange");
try {
return getStatementRange(
vdocUri.uri,
adjustedPosition(vdoc.language, position),
vdoc.language
);
} catch (error) {
return undefined;
} finally {
if (vdocUri.cleanup) {
await vdocUri.cleanup();
}
}
};
} else {
return undefined;
}
};
}

async function getStatementRange(
uri: vscode.Uri,
position: vscode.Position,
language: EmbeddedLanguage
) {
return await vscode.commands.executeCommand<hooks.StatementRange>(
const result = await vscode.commands.executeCommand<hooks.StatementRange>(
"vscode.executeStatementRangeProvider",
uri,
position
);
return { range: unadjustedRange(language, result.range), code: result.code };
}
Loading