Skip to content
Open
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
1 change: 1 addition & 0 deletions apps/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.140.0 (Unreleased)

- In Positron with inline output off, cells in any language that has a Positron runtime now run in its console, with **Run Cell**, **Run Above**, **Run Next Cell** and the usual shortcuts. Before, only R, Python, Julia and C# cells could run, so cells for runtimes that extensions add, such as `{ggsql}` and `{sas}`, could not. A language counts when an extension declares it in the `languageRuntimes` contribution of its `package.json`, as Positron decides for inline output, or registers a runtime for it. The cell option lines of these languages are not sent to the console (<https://github.com/quarto-dev/quarto/pull/1237>).
- Fixed a bug where hovering over any text after a citation on the same line showed that citation's reference. The hover now shows only over the `@` and the citation key (<https://github.com/quarto-dev/quarto/pull/1225>).
- In the visual editor, code cells show completions and snippets automatically again while you type. Previously, they did not show when `editor.quickSuggestions` used its new default value, `offWhenInlineCompletions` (<https://github.com/quarto-dev/quarto/pull/1216>).
- Fixed a bug where the language server could hang when hovering over text on a line that contains single-line display math such as `$$x$$` (<https://github.com/quarto-dev/quarto/pull/1212>).
Expand Down
307 changes: 202 additions & 105 deletions apps/vscode/src/host/positron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,205 @@ function isConfigurationSet<T>(
);
}

/**
* The languages that extensions declare runtimes for in the `languageRuntimes`
* contribution of their `package.json`, e.g. `sas` from the SAS extension or
* `ggsql` from the ggsql extension.
*/
export function contributedRuntimeLanguages(
extensions: readonly { packageJSON: unknown }[]
): Set<string> {
const languages = new Set<string>();
for (const extension of extensions) {
const packageJSON = extension.packageJSON as
{ contributes?: { languageRuntimes?: unknown } } | undefined;
const contributions = packageJSON?.contributes?.languageRuntimes;
if (!Array.isArray(contributions)) {
continue;
}
for (const contribution of contributions) {
const languageId = (contribution as { languageId?: unknown } | undefined)?.languageId;
if (typeof languageId === "string" && languageId.length > 0) {
languages.add(languageId);
}
}
}
return languages;
}

/**
* Languages whose cells can run in a Positron console. They come from two
* sources, as in Positron's own check for inline output
* (`hasLanguageRuntimeProvider`):
*
* - the `languageRuntimes` contributions of the installed extensions, which
* are known before those extensions activate or discover any runtime, and
* - the registered runtimes, for an extension that registers a runtime
* without declaring the contribution.
*
* Both are cached because the checks for whether a cell can run are
* synchronous. They are tracked only in Positron, so nothing changes in
* VS Code even when such extensions are installed.
*
* See also https://github.com/posit-dev/positron/issues/17432
*/
let contributedLanguages = new Set<string>();
const registeredLanguages = new Set<string>();
let trackingRuntimes = false;

function trackRuntimeLanguages() {
const runtime = tryAcquirePositronApi()?.runtime;
if (trackingRuntimes || runtime === undefined) {
return;
}
trackingRuntimes = true;

// the contributions are read synchronously, so they are known right away;
// the subscriptions below live as long as the extension host
contributedLanguages = contributedRuntimeLanguages(vscode.extensions.all);
vscode.extensions.onDidChange(() => {
contributedLanguages = contributedRuntimeLanguages(vscode.extensions.all);
});

runtime.getRegisteredRuntimes().then(
(runtimes) => runtimes.forEach((metadata) => registeredLanguages.add(metadata.languageId)),
() => { }
);
runtime.onDidRegisterRuntime((metadata) => registeredLanguages.add(metadata.languageId));
}

/**
* Whether Positron has a runtime for the language, so its cells can run in
* the console.
*/
export function hasPositronRuntime(language: string): boolean {
return contributedLanguages.has(language) || registeredLanguages.has(language);
}

function positronRuntimeLanguages(): string[] {
return [...new Set([...contributedLanguages, ...registeredLanguages])];
}

/**
* Cell executor that runs code in the Positron console of the language.
*/
function positronRuntimeExecutor(
language: string,
document: vscode.TextDocument,
engine: MarkdownEngine,
outputChannel?: vscode.LogOutputChannel
): CellExecutor {
return {
execute: async (blocks: string[], editorUri?: vscode.Uri, executionMetadata?: Record<string, unknown>[]): Promise<void> => {
const runtime = tryAcquirePositronApi()?.runtime;

if (runtime === undefined) {
// Can't do anything without a runtime
return;
}

let executionLanguage = language;
if (
language === "python" &&
isKnitrDocument(document, engine) &&
vscode.workspace.getConfiguration("quarto").get("cells.useReticulate", true)
) {
executionLanguage = "r";
blocks = blocks.map(pythonWithReticulate);
}

// Our callback executes each block sequentially
const callback = async () => {
for (let i = 0; i < blocks.length; i++) {
const metadata = executionMetadata?.[i];

// Track whether the runtime itself reported the failure. A
// failure of the running code (a runtime error or syntax
// error in the cell), an interrupt, or the session exiting are
// all delivered through the execution observer's `onFailed`
// callback before `executeCode` rejects. These are already
// surfaced in the console, so we don't want to surface them
// again. A rejection *without* `onFailed` having fired means
// the code couldn't be submitted to the runtime at all (e.g.
// no runtime is registered for the language), which is a
// genuine problem worth reporting.
let runtimeFailure = false;

try {
await runtime.executeCode(
executionLanguage, // The language ID
blocks[i], // The code string to execute
false, // Whether to focus the console
true, // Whether to allow incomplete code to run
undefined, // The execution mode
undefined, // The error behavior
{ onFailed: () => { runtimeFailure = true; } }, // An execution observer
undefined, // The specific session ID in which to execute
editorUri, // The document URI
metadata
);
} catch (err) {
const message = err instanceof Error ? err.message : JSON.stringify(err);

if (!runtimeFailure) {
// The code couldn't be submitted to the runtime. Log it
// and let it propagate so the user finds out.
outputChannel?.error(`Failed to execute ${executionLanguage} cell: ${message}`);
throw err;
}

// The executed code raised an error (or was interrupted).
// It's already reported in the console, so log it for the
// record but don't let it propagate to the command handler,
// which would surface it again as a notification popup.
// https://github.com/posit-dev/positron/issues/9845
outputChannel?.debug(`Error executing ${executionLanguage} cell: ${message}`);

// Stop executing any subsequent blocks since one failed.
break;
}
}
};

await ExecuteQueue.instance.add(executionLanguage, callback);
},
executeSelection: async (): Promise<void> => {
await vscode.commands.executeCommand('workbench.action.positronConsole.executeCode', { languageId: language });
},
executeAtPosition: async (uri: Uri, position: Position): Promise<Position> => {
try {
return await vscode.commands.executeCommand(
'positron.executeCodeFromPosition',
language,
uri,
position
) as Position;
} catch {
// an error can happen, we think, if the statementRangeProvider errors
console.error('error when using `positron.executeCodeFromPosition`');
}
return position;
},
executeInlineCells: async (documentUri: vscode.Uri, cellRanges: Range[], executionMetadata?: Record<string, unknown>[]): Promise<void> => {
const runtime = tryAcquirePositronApi()?.runtime;

if (runtime === undefined) {
// Can't do anything without a runtime
return;
}

await runtime.executeInlineCell(documentUri, cellRanges, executionMetadata);
}
};
}

export function positronExtensionHost(outputChannel?: vscode.LogOutputChannel): ExtensionHost {
trackRuntimeLanguages();
return {
// supported executable languages (we delegate to the default for langugaes
// w/o runtimes so we support all languages)
executableLanguages,
// w/o runtimes so we support all languages), plus any language with a
// Positron runtime
executableLanguages: () => [...new Set([...executableLanguages(), ...positronRuntimeLanguages()])],

cellExecutorForLanguage: async (language: string, document: vscode.TextDocument, engine: MarkdownEngine, silent?: boolean)
: Promise<CellExecutor | undefined> => {
Expand All @@ -86,111 +280,14 @@ export function positronExtensionHost(outputChannel?: vscode.LogOutputChannel):
case "python":
case "csharp":
case "r":
return {
execute: async (blocks: string[], editorUri?: vscode.Uri, executionMetadata?: Record<string, unknown>[]): Promise<void> => {
const runtime = tryAcquirePositronApi()?.runtime;

if (runtime === undefined) {
// Can't do anything without a runtime
return;
}

let executionLanguage = language;
if (
language === "python" &&
isKnitrDocument(document, engine) &&
vscode.workspace.getConfiguration("quarto").get("cells.useReticulate", true)
) {
executionLanguage = "r";
blocks = blocks.map(pythonWithReticulate);
}

// Our callback executes each block sequentially
const callback = async () => {
for (let i = 0; i < blocks.length; i++) {
const metadata = executionMetadata?.[i];

// Track whether the runtime itself reported the failure. A
// failure of the running code (a runtime error or syntax
// error in the cell), an interrupt, or the session exiting are
// all delivered through the execution observer's `onFailed`
// callback before `executeCode` rejects. These are already
// surfaced in the console, so we don't want to surface them
// again. A rejection *without* `onFailed` having fired means
// the code couldn't be submitted to the runtime at all (e.g.
// no runtime is registered for the language), which is a
// genuine problem worth reporting.
let runtimeFailure = false;

try {
await runtime.executeCode(
executionLanguage, // The language ID
blocks[i], // The code string to execute
false, // Whether to focus the console
true, // Whether to allow incomplete code to run
undefined, // The execution mode
undefined, // The error behavior
{ onFailed: () => { runtimeFailure = true; } }, // An execution observer
undefined, // The specific session ID in which to execute
editorUri, // The document URI
metadata
);
} catch (err) {
const message = err instanceof Error ? err.message : JSON.stringify(err);

if (!runtimeFailure) {
// The code couldn't be submitted to the runtime. Log it
// and let it propagate so the user finds out.
outputChannel?.error(`Failed to execute ${executionLanguage} cell: ${message}`);
throw err;
}

// The executed code raised an error (or was interrupted).
// It's already reported in the console, so log it for the
// record but don't let it propagate to the command handler,
// which would surface it again as a notification popup.
// https://github.com/posit-dev/positron/issues/9845
outputChannel?.debug(`Error executing ${executionLanguage} cell: ${message}`);

// Stop executing any subsequent blocks since one failed.
break;
}
}
};

await ExecuteQueue.instance.add(executionLanguage, callback);
},
executeSelection: async (): Promise<void> => {
await vscode.commands.executeCommand('workbench.action.positronConsole.executeCode', { languageId: language });
},
executeAtPosition: async (uri: Uri, position: Position): Promise<Position> => {
try {
return await vscode.commands.executeCommand(
'positron.executeCodeFromPosition',
language,
uri,
position
) as Position;
} catch {
// an error can happen, we think, if the statementRangeProvider errors
console.error('error when using `positron.executeCodeFromPosition`');
}
return position;
},
executeInlineCells: async (documentUri: vscode.Uri, cellRanges: Range[], executionMetadata?: Record<string, unknown>[]): Promise<void> => {
const runtime = tryAcquirePositronApi()?.runtime;

if (runtime === undefined) {
// Can't do anything without a runtime
return;
}

await runtime.executeInlineCell(documentUri, cellRanges, executionMetadata);
}
};
return positronRuntimeExecutor(language, document, engine, outputChannel);

// delegate for other languages
// use positron api for any other language with a Positron runtime
// (e.g. SAS or ggsql from an extension), and delegate the rest
default:
if (hasPositronRuntime(language)) {
return positronRuntimeExecutor(language, document, engine, outputChannel);
}
return cellExecutorForLanguage(language, document, engine, silent);
}
},
Expand Down
19 changes: 15 additions & 4 deletions apps/vscode/src/providers/cell/executors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import {

import { lines } from "core";

import { cellOptionsForToken, kExecuteEval } from "./options";
import { cellOptionsForToken, kExecuteEval, leadingOptionYaml } from "./options";

import { CellExecutor, ExtensionHost } from "../../host";
import { executableLanguages } from "../../host/executors";
import { isInlineOutputEnabled } from "../../host/positron";
import { hasPositronRuntime, isInlineOutputEnabled } from "../../host/positron";
import { Position } from "vscode";
import { Uri } from "vscode";


export function hasExecutor(_host: ExtensionHost, language: string) {
return executableLanguages().includes(language);
return executableLanguages().includes(language) || hasPositronRuntime(language);
}

export function blockHasExecutor(host: ExtensionHost, token?: Token): token is TokenMath | TokenCodeBlock {
Expand Down Expand Up @@ -60,7 +60,10 @@ export function cellMetadataForBlock(token: TokenMath | TokenCodeBlock): Record<
}

// skip yaml options for execution
export function codeWithoutOptionsFromBlock(token: TokenMath | TokenCodeBlock) {
export function codeWithoutOptionsFromBlock(
token: TokenMath | TokenCodeBlock,
hasRuntime: (language: string) => boolean = hasPositronRuntime
) {
if (isExecutableLanguageBlock(token)) {
const language = languageNameFromBlock(token);
if (hasYamlHashOptions(language)) {
Expand All @@ -73,6 +76,14 @@ export function codeWithoutOptionsFromBlock(token: TokenMath | TokenCodeBlock) {
} else {
return "";
}
} else if (hasRuntime(language)) {
// a language that a Positron runtime runs in the console (e.g. sas or
// ggsql): skip its option lines, written with its own comment
// characters (`*| ... ;` for sas, `#|` for languages without an entry
// in kLangCommentChars, such as ggsql, where `#` isn't a comment)
const blockLines = lines(codeForExecutableLanguageBlock(token));
const optionLines = leadingOptionYaml(language, blockLines).length;
return blockLines.slice(optionLines).join("\n");
} else {
return codeForExecutableLanguageBlock(token);
}
Expand Down
Loading
Loading