Skip to content

Mark code that was directly injected into the R runtime by the R runtime with a special ID; detect and add that code to the console input history #8277

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

Merged
merged 1 commit into from
Jun 25, 2025
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
7 changes: 4 additions & 3 deletions extensions/positron-r/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as vscode from 'vscode';
import * as positron from 'positron';
import { PromiseHandles } from './util';
import { generateDirectInjectionId, PromiseHandles } from './util';
import { checkInstalled } from './session';
import { getRPackageName } from './contexts';
import { getRPackageTasks } from './tasks';
Expand Down Expand Up @@ -110,8 +110,9 @@ export async function registerCommands(context: vscode.ExtensionContext, runtime
return;
}

// Temporary measure - generate a direct injection ID so this code execution will be added to the console history.
session.execute(`library(${packageName})`,
randomUUID(),
generateDirectInjectionId(),
positron.RuntimeCodeExecutionMode.Interactive,
positron.RuntimeErrorBehavior.Continue);
}
Expand Down Expand Up @@ -218,7 +219,7 @@ export async function registerCommands(context: vscode.ExtensionContext, runtime
if (isInstalled) {
const session = await positron.runtime.getForegroundSession();
if (session) {
session.execute(`renv::init()`, randomUUID(), positron.RuntimeCodeExecutionMode.Interactive, positron.RuntimeErrorBehavior.Continue);
session.execute(`renv::init()`, generateDirectInjectionId(), positron.RuntimeCodeExecutionMode.Interactive, positron.RuntimeErrorBehavior.Continue);
} else {
console.debug('[r.renvInit] no session available');
}
Expand Down
4 changes: 3 additions & 1 deletion extensions/positron-r/src/hyperlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as positron from 'positron';
import * as vscode from 'vscode';
import { randomUUID } from 'crypto';
import { RSession } from './session';
import { generateDirectInjectionId } from './util.js';

export async function handleRCode(runtime: RSession, code: string): Promise<void> {
const match = matchRunnable(code);
Expand Down Expand Up @@ -64,9 +65,10 @@ async function handleManuallyRunnable(_runtime: RSession, code: string) {
}

function handleAutomaticallyRunnable(runtime: RSession, code: string) {
// Temporary measure - generate a direct injection ID so this code execution will be added to the console history.
runtime.execute(
code,
randomUUID(),
generateDirectInjectionId(),
positron.RuntimeCodeExecutionMode.Transient,
positron.RuntimeErrorBehavior.Continue
);
Expand Down
9 changes: 9 additions & 0 deletions extensions/positron-r/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as fs from 'fs';
import { LOGGER } from './extension';
import { randomUUID } from 'crypto';

export class PromiseHandles<T> {
resolve!: (value: T | Promise<T>) => void;
Expand Down Expand Up @@ -90,3 +91,11 @@ export function removeSurroundingQuotes(x: string): string {

return x;
}

/**
* Generates a unique ID for direct injection of code into the runtime.
* @returns A unique ID for direct injection of code into the runtime.
*/
export function generateDirectInjectionId(): string {
return `direct-injection-${randomUUID()}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -753,15 +753,13 @@ suite('positron API - executeCode', () => {
'print("event")', // code
false, // focus
false, // allowIncomplete
positron.RuntimeCodeExecutionMode.Interactive,
positron.RuntimeErrorBehavior.Stop
);

// Assert that the event matches the expected values
assert.ok(event, 'Event should be fired');
assert.strictEqual(event.languageId, 'test', 'Language ID should match');
assert.strictEqual(event.code, 'print("event")', 'Code should match');
assert.strictEqual(event.attribution.source, positron.CodeAttributionSource.Interactive,
assert.strictEqual(event.attribution.source, positron.CodeAttributionSource.Extension,
'Correctly attributed to execution via an extension');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2039,11 +2039,10 @@ class PositronConsoleInstance extends Disposable implements IPositronConsoleInst
)
);

// Detect incoming code that was not sent by the console input (that is not prefixed
// by 'fragment-'). When this happens, fire the onDidExecuteCode event so the code gets
// added to the console history. In the fullness of time, it would be ideal for the
// runtime to emit an event for this, but for now we can detect it.
if (!languageRuntimeMessageInput.parent_id.startsWith('fragment-')) {
// As a temporary measure, to be replaced soon with an event from the runtime, detect code
// that was injected into the runtime directly (not via the console input). When this happens,
// fire the onDidExecuteCode event so the code gets added to the console history.
if (languageRuntimeMessageInput.parent_id.startsWith('direct-injection-')) {
// Get the session's language ID. It will always be defined for a runtime session.
const languageId = this.session?.runtimeMetadata?.languageId;
if (!languageId) {
Expand Down