Skip to content

Commit

Permalink
Only register the reticulate runtime manager when at least one R runt…
Browse files Browse the repository at this point in the history
…ime exists.
  • Loading branch information
dfalbel committed Sep 19, 2024
1 parent b4653e5 commit 158ba52
Showing 1 changed file with 67 additions and 8 deletions.
75 changes: 67 additions & 8 deletions extensions/positron-reticulate/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,78 @@ class ReticulateRuntimeMetadata implements positron.LanguageRuntimeMetadata {

export class ReticulateProvider {
_client: positron.RuntimeClientInstance | undefined = undefined;
constructor(readonly manager: ReticulateRuntimeManager) { }
manager: ReticulateRuntimeManager | undefined;
registrationHook: vscode.Disposable | undefined;

constructor(readonly context: vscode.ExtensionContext) {
// Check if the reticulate runtime manager has been registered before
// in this workspace. If it happened, we register the reticulate manager
// automatically, no need to wait for R.
if (this.context.workspaceState.get<boolean>('reticulate_registered', false)) {
this.registerReticulateRuntimeManager();
} else {
// Launch an async task that might register the reticulate runtime manager
// if conditions are match.
this.maybeRegisterReticulateManager();
}
}

async maybeRegisterReticulateManager() {
// Get a fixed list of all current runtimes.
const runtimes = await positron.runtime.getRegisteredRuntimes();

// Hook that will register the reticulate runtime if an R
// runtime is found.
this.registrationHook = positron.runtime.onDidRegisterRuntime((metadata) => {
if (!this.manager && metadata.languageId === 'r') {
this.registerReticulateRuntimeManager();
}
});

this.context.subscriptions.push(this.registrationHook);

// Walk trough the list of runtimes looking for an R runtime,
// if one exists we register the reticulate runtime manager.
// We also dispose the above hook if we get to register the
// runtime.
for (const runtime of runtimes) {
if (!this.manager && runtime.languageId === 'r') {
this.registerReticulateRuntimeManager();
return;
}
}
}

registerReticulateRuntimeManager() {
this.manager = new ReticulateRuntimeManager(this.context);
this.context.subscriptions.push(positron.runtime.registerLanguageRuntimeManager(this.manager));

// Dispose the registration hook so we don't keep checking for new runtimes
// once we already registered ours.
if (this.registrationHook) {
this.registrationHook.dispose();
this.registrationHook = undefined;
}

// Once the reticulate manager has been registered, we store a project flag that will
// always load the manager later on.
this.context.workspaceState.update('reticulate_registered', true);
}

async registerClient(client: positron.RuntimeClientInstance) {
if (this._client) {
this._client.dispose();
}

this._client = client;

await this.manager.discoverRuntimes();
if (!this.manager) {
// No ReticulateRuntimeManager!
// We won't be able to start the reticulate sesssion.
console.error('No reticulate runtime manager. We will not be able to start the reticulate session.');
return;
}

await positron.runtime.selectLanguageRuntime('reticulate');

this.manager._session?.onDidEndSession(() => {
Expand Down Expand Up @@ -462,11 +525,7 @@ let CONTEXT: vscode.ExtensionContext;
*/
export function activate(context: vscode.ExtensionContext) {
CONTEXT = context;

const manager = new ReticulateRuntimeManager(context);
context.subscriptions.push(positron.runtime.registerLanguageRuntimeManager(manager));

const reticulateProvider = new ReticulateProvider(manager);
const reticulateProvider = new ReticulateProvider(context);

context.subscriptions.push(
positron.runtime.registerClientHandler({
Expand All @@ -479,6 +538,6 @@ export function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(reticulateProvider);

return manager;
return reticulateProvider;
}

0 comments on commit 158ba52

Please sign in to comment.