Skip to content

Use Positron's active runtime when inserting a code cell#951

Open
juliasilge wants to merge 4 commits intomainfrom
positron-active-runtime-insert-code-cell
Open

Use Positron's active runtime when inserting a code cell#951
juliasilge wants to merge 4 commits intomainfrom
positron-active-runtime-insert-code-cell

Conversation

@juliasilge
Copy link
Copy Markdown
Collaborator

Closes #480

When using Insert Code Cell in source mode, the extension determines the language for the new cell using a priority chain:

  1. Cursor is inside an existing code cell: use that cell's language and move the cursor to just after the block.
  2. Cursor is outside any code cell, but the document has executable blocks: use the language of the nearest block (the last one before the cursor, or the first one after if there are none before).
  3. No executable blocks in the document, and running in Positron: (new, this PR) call positron.runtime.getForegroundSession() to get the active runtime's language. If it's one of the supported Quarto languages (python, r, julia, ojs, sql, bash, mermaid, dot), that language is used.
  4. None of the above: fall back to an interactive snippet picker listing all supported languages.

Steps 1 to 3 all result in the cursor being placed directly inside the new cell (no picker). Step 4 requires the user to make a selection first.

This is a Positron-only improvement; in VS Code, step 3 is skipped and the picker appears for any empty document.

cellRanges: vscode.Range[]
): Thenable<void>;

getForegroundSession(): Thenable<LanguageRuntimeSession | undefined>;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm adding this here to the PositronRuntime interface in along with a minimal LanguageRuntimeSession type.

Comment thread apps/vscode/src/providers/insert.ts Outdated
// if no language found in document, fall back to Positron's active runtime
const kSupportedLanguages = ['python', 'r', 'julia', 'ojs', 'sql', 'bash', 'mermaid', 'dot'];
if (!language) {
const api = tryAcquirePositronApi() as unknown as PositronApi | undefined;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using tryAcquirePositronApi() from @posit-dev/positron here to try to get us moved over to the npm package more, but AFAICT I still have to cast to PositronApi to get typed access to the new method. I feel like we don't quite have the npm package set up perfectly yet to be used in an extension like this one that is supposed to work in both Positron and VS Code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided this wasn't the right call and changed over in 69f7a7e.

import * as assert from "assert";
import { examplesOutUri, openAndShowExamplesOutTextDocument, WORKSPACE_PATH } from "./test-utils";

suite("Insert Code Cell", function () {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests only test the VS Code behavior, not what would happen in Positron in an empty document. We don't yet have a good way to write tests against Positron APIs.

@juliasilge juliasilge requested a review from DavisVaughan April 16, 2026 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consult Positron's active language runtime to order code cell types in the Insert Anything tool

1 participant