-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto select kernel using Positron foreground session (#2505)
* auto select kernel using positron foreground session Extract a notebook controller manager, which updates a notebook's affinity for each controller on notebook open. Fix another bug in the notebook editor widget, causing new untitled notebooks to store the incorrect `selectedKernelId` and therefore auto select the incorrect kernel for a notebook. * logging * imports * address review comments
- Loading branch information
Showing
4 changed files
with
134 additions
and
23 deletions.
There are no files selected for viewing
36 changes: 21 additions & 15 deletions
36
extensions/positron-notebook-controllers/src/extension.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,39 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (C) 2023 Posit Software, PBC. All rights reserved. | ||
* Copyright (C) 2023-2024 Posit Software, PBC. All rights reserved. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as positron from 'positron'; | ||
import * as vscode from 'vscode'; | ||
import { initializeLogging, trace } from './logging'; | ||
import { NotebookController } from './notebookController'; | ||
import { initializeLogging } from './logging'; | ||
import { NotebookControllerManager } from './notebookControllerManager'; | ||
|
||
export async function activate(context: vscode.ExtensionContext): Promise<void> { | ||
initializeLogging(); | ||
|
||
const knownLanguageIds = new Set<string>(); | ||
// Ensure that a notebook controller is registered for a given language. | ||
const ensureNotebookController = (languageId: string) => { | ||
if (!knownLanguageIds.has(languageId)) { | ||
const controller = new NotebookController(languageId); | ||
knownLanguageIds.add(languageId); | ||
context.subscriptions.push(controller); | ||
trace(`Registered notebook controller for language: ${languageId}`); | ||
} | ||
}; | ||
const manager = new NotebookControllerManager(); | ||
context.subscriptions.push(manager); | ||
|
||
// Register notebook controllers for newly registered runtimes. | ||
context.subscriptions.push(positron.runtime.onDidRegisterRuntime((runtime) => { | ||
ensureNotebookController(runtime.languageId); | ||
if (!manager.controllers.has(runtime.languageId)) { | ||
manager.createNotebookController(runtime.languageId); | ||
} | ||
})); | ||
|
||
// Register notebook controllers for existing runtimes. | ||
for (const runtime of await positron.runtime.getRegisteredRuntimes()) { | ||
ensureNotebookController(runtime.languageId); | ||
if (!manager.controllers.has(runtime.languageId)) { | ||
manager.createNotebookController(runtime.languageId); | ||
} | ||
} | ||
|
||
// Update notebook affinity when a notebook is opened. | ||
context.subscriptions.push(vscode.workspace.onDidOpenNotebookDocument(async (notebook) => { | ||
manager.updateNotebookAffinity(notebook); | ||
})); | ||
|
||
// Update notebook affinity for notebooks that are already opened. | ||
for (const notebook of vscode.workspace.notebookDocuments) { | ||
manager.updateNotebookAffinity(notebook); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
extensions/positron-notebook-controllers/src/notebookControllerManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (C) 2024 Posit Software, PBC. All rights reserved. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { trace } from './logging'; | ||
import { NotebookController } from './notebookController'; | ||
|
||
/** | ||
* Manages notebook controllers. | ||
*/ | ||
export class NotebookControllerManager implements vscode.Disposable { | ||
/** Notebook controllers keyed by languageId. */ | ||
public readonly controllers = new Map<string, NotebookController>(); | ||
|
||
/** | ||
* Create a notebook controller for a language. | ||
* | ||
* @param languageId The language ID for which to create a notebook controller. | ||
*/ | ||
public createNotebookController(languageId: string): void { | ||
if (!this.controllers.has(languageId)) { | ||
const controller = new NotebookController(languageId); | ||
this.controllers.set(languageId, controller); | ||
trace(`Registered notebook controller for language: ${languageId}`); | ||
} | ||
} | ||
|
||
/** | ||
* Update a notebook's affinity for all known controllers. | ||
* | ||
* Positron automates certain decisions if a notebook only has a single 'preferred' controller. | ||
* | ||
* @param notebook The notebook whose affinity to update. | ||
* @returns Promise that resolves when the notebook's affinity has been updated across all controllers. | ||
*/ | ||
public async updateNotebookAffinity(notebook: vscode.NotebookDocument): Promise<void> { | ||
if (notebook.uri.scheme === 'untitled') { | ||
// If the notebook is untitled, wait for its data to be updated. This works around the fact | ||
// that `vscode.openNotebookDocument(notebookType, content)` first creates a notebook | ||
// (triggering `onDidOpenNotebookDocument`), and later updates its content (triggering | ||
// `onDidChangeNotebookDocument`). | ||
const event = await new Promise<vscode.NotebookDocumentChangeEvent | undefined>((resolve) => { | ||
// Apply a short timeout to avoid waiting indefinitely. | ||
const timeout = setTimeout(() => { | ||
disposable.dispose(); | ||
resolve(undefined); | ||
}, 50); | ||
|
||
const disposable = vscode.workspace.onDidChangeNotebookDocument((e) => { | ||
if (e.notebook === notebook) { | ||
clearTimeout(timeout); | ||
disposable.dispose(); | ||
resolve(e); | ||
} | ||
}); | ||
}); | ||
|
||
if (event) { | ||
notebook = event.notebook; | ||
} | ||
} | ||
|
||
// Detect the notebook's language. | ||
// First try the notebook metadata. | ||
const metadata = notebook.metadata?.custom?.metadata; | ||
const languageId = metadata?.language_info?.name | ||
?? metadata?.kernelspec?.language | ||
// Fall back to the first cell's language. | ||
?? notebook.getCells()?.[0].document.languageId; | ||
|
||
// Get the preferred controller for the language. | ||
const preferredController = languageId && this.controllers.get(languageId); | ||
|
||
// Set the affinity across all known controllers. | ||
for (const controller of this.controllers.values()) { | ||
const affinity = controller === preferredController | ||
? vscode.NotebookControllerAffinity.Preferred | ||
: vscode.NotebookControllerAffinity.Default; | ||
controller.controller.updateNotebookAffinity(notebook, affinity); | ||
trace(`Updated notebook affinity for language: ${languageId}, notebook: ${notebook.uri.path}, affinity: ${affinity}`); | ||
} | ||
} | ||
|
||
dispose(): void { | ||
this.controllers.forEach(c => c.dispose()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters