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 new R extension quickpick to select an interpreter #6213

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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: 1 addition & 1 deletion extensions/positron-r/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"r.command.useTest.title": "Add (or visit) a test file",
"r.command.packageCheck.title": "Check R Package",
"r.command.packageDocument.title": "Document R Package",
"r.command.selectInterpreter.title": "Select R Interpreter",
"r.command.selectInterpreter.title": "Select Interpreter",
"r.command.rmarkdownRender.title": "Render Document With R Markdown",
"r.menu.createNewFile.title": "R File",
"r.menu.insertPipe.title": "Insert the pipe operator",
Expand Down
6 changes: 4 additions & 2 deletions extensions/positron-r/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { getRPackageName } from './contexts';
import { getRPackageTasks } from './tasks';
import { randomUUID } from 'crypto';
import { RSessionManager } from './session-manager';
import { quickPickRuntime } from './runtime-quickpick';
import { MINIMUM_RENV_VERSION, MINIMUM_R_VERSION } from './constants';
import { RRuntimeManager } from './runtime-manager';

export async function registerCommands(context: vscode.ExtensionContext) {
export async function registerCommands(context: vscode.ExtensionContext, runtimeManager: RRuntimeManager) {

context.subscriptions.push(

Expand Down Expand Up @@ -157,7 +159,7 @@ export async function registerCommands(context: vscode.ExtensionContext) {
}),

vscode.commands.registerCommand('r.selectInterpreter', async () => {
await vscode.commands.executeCommand('workbench.action.languageRuntime.select', 'r');
await quickPickRuntime(runtimeManager);
}),

// Commands used to source the current file
Expand Down
5 changes: 3 additions & 2 deletions extensions/positron-r/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(LOGGER.onDidChangeLogLevel(onDidChangeLogLevel));
onDidChangeLogLevel(LOGGER.logLevel);

positron.runtime.registerLanguageRuntimeManager('r', new RRuntimeManager(context));
const rRuntimeManager = new RRuntimeManager(context);
positron.runtime.registerLanguageRuntimeManager(rRuntimeManager);

// Set contexts.
setContexts(context);

// Register commands.
registerCommands(context);
registerCommands(context, rRuntimeManager);

// Register formatter.
registerFormatter(context);
Expand Down
13 changes: 11 additions & 2 deletions extensions/positron-r/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export interface RBinary {
reasons: ReasonDiscovered[]
}

/**
* The source for the R runtime, in the order that we display these sources in the quick pick.
*/
export enum RRuntimeSource {
system = 'System',
user = 'User',
homebrew = 'Homebrew',
Comment on lines +36 to +39
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this enum to use here and also in the quickpick, for display purposes.

}

/**
* Discovers R language runtimes for Positron; implements positron.LanguageRuntimeDiscoverer.
*
Expand Down Expand Up @@ -185,9 +194,9 @@ export async function makeMetadata(
// it's a Homebrew installation if it does)
const isHomebrewInstallation = rInst.binpath.includes('/homebrew/');

const runtimeSource = isHomebrewInstallation ? 'Homebrew' :
const runtimeSource = isHomebrewInstallation ? RRuntimeSource.homebrew :
isUserInstallation ?
'User' : 'System';
RRuntimeSource.user : RRuntimeSource.system;

// Short name shown to users (when disambiguating within a language)
const runtimeShortName = includeArch ? `${rInst.version} (${rInst.arch})` : rInst.version;
Expand Down
15 changes: 14 additions & 1 deletion extensions/positron-r/src/runtime-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ import { createJupyterKernelSpec } from './kernel-spec';

export class RRuntimeManager implements positron.LanguageRuntimeManager {

constructor(private readonly _context: vscode.ExtensionContext) { }
private readonly onDidDiscoverRuntimeEmitter = new vscode.EventEmitter<positron.LanguageRuntimeMetadata>();

constructor(private readonly _context: vscode.ExtensionContext) {
this.onDidDiscoverRuntime = this.onDidDiscoverRuntimeEmitter.event;
}

/**
* An event that fires when a new R language runtime is discovered.
*/
onDidDiscoverRuntime: vscode.Event<positron.LanguageRuntimeMetadata>;

discoverAllRuntimes(): AsyncGenerator<positron.LanguageRuntimeMetadata> {
return rRuntimeDiscoverer();
}

registerLanguageRuntime(runtime: positron.LanguageRuntimeMetadata): void {
this.onDidDiscoverRuntimeEmitter.fire(runtime);
}

async recommendedWorkspaceRuntime(): Promise<positron.LanguageRuntimeMetadata | undefined> {
// TODO: If the workspace contains an R project, we could recommend an
// R runtime from e.g. the `DESCRIPTION` file or an renv lockfile.
Expand Down
97 changes: 97 additions & 0 deletions extensions/positron-r/src/runtime-quickpick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2025 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/

import * as positron from 'positron';
import * as vscode from 'vscode';
import { rRuntimeDiscoverer, RRuntimeSource } from './provider';
import { RRuntimeManager } from './runtime-manager';

class RuntimeQuickPickItem implements vscode.QuickPickItem {

label: string;
description: string;
runtime: positron.LanguageRuntimeMetadata;

constructor(
public runtimeMetadata: positron.LanguageRuntimeMetadata,
) {
this.label = runtimeMetadata.runtimeName;
this.description = runtimeMetadata.runtimePath;
this.runtime = runtimeMetadata;
}
}

export async function quickPickRuntime(runtimeManager: RRuntimeManager) {

const runtime = await new Promise<positron.LanguageRuntimeMetadata | undefined>(
async (resolve) => {
const disposables: vscode.Disposable[] = [];

// Set up the quick pick
const input = vscode.window.createQuickPick<RuntimeQuickPickItem | vscode.QuickPickItem>();
input.title = vscode.l10n.t('Select Interpreter');
input.canSelectMany = false;
input.matchOnDescription = true;

// R discovery is fast so we just do it before rendering the quick pick
const runtimePicks: RuntimeQuickPickItem[] = [];
const discoverer = rRuntimeDiscoverer();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Setting up the quickpick triggers discovery again. Since this is so fast for R, I think this will be more aligned with folks' expectations than a not-entirely-prominent refresh button.

for await (const runtime of discoverer) {
runtimePicks.push(new RuntimeQuickPickItem(runtime));
}
// Update the quick pick items with the source
const runtimeSourceOrder: string[] = Object.values(RRuntimeSource);
runtimePicks.sort((a, b) => {
return runtimeSourceOrder.indexOf(a.runtime.runtimeSource) - runtimeSourceOrder.indexOf(b.runtime.runtimeSource);
});
const picks = new Array<vscode.QuickPickItem | RuntimeQuickPickItem>();
for (const source of runtimeSourceOrder) {
const separatorItem: vscode.QuickPickItem = { label: source, kind: vscode.QuickPickItemKind.Separator };
picks.push(separatorItem);
for (const item of runtimePicks) {
if (item.runtime.runtimeSource === source) {
picks.push(item);
}
}
}

input.items = picks;

// If we have a preferred runtime, select it
const preferredRuntime = await positron.runtime.getPreferredRuntime('r');
if (preferredRuntime) {
input.placeholder = vscode.l10n.t('Selected Interpreter: {0}', preferredRuntime.runtimeName);
const activeItem = runtimePicks.find(
(item) => item.runtimeMetadata.runtimeId === preferredRuntime.runtimeId
);
if (activeItem) {
input.activeItems = [activeItem];
}
}

disposables.push(
input.onDidAccept(() => {
const activeItem = input.activeItems[0] as RuntimeQuickPickItem;
resolve(activeItem.runtime);
input.hide();
}),
input.onDidHide(() => {
resolve(undefined);
input.dispose();
}),
);
input.show();
});

// If we did in fact get a runtime from the user, select and start it
if (runtime) {
const registeredRuntimes = await positron.runtime.getRegisteredRuntimes();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of getting all the registered runtimes and checking whether ours is in it, we could just always re-register the selected one? I don't think it generates errors to register a runtime again.

const runtimeIsRegistered = registeredRuntimes.filter((r) => r.runtimeId === runtime.runtimeId);
if (runtimeIsRegistered.length === 0) {
runtimeManager.registerLanguageRuntime(runtime);
}
positron.runtime.selectLanguageRuntime(runtime.runtimeId);
}
};
Loading