-
Notifications
You must be signed in to change notification settings - Fork 92
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
3a0f1a7
7a31d94
07c9a19
3f46b0e
cb57fed
f4e72e1
005c8d0
f9b68e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
}; |
There was a problem hiding this comment.
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.