-
Notifications
You must be signed in to change notification settings - Fork 289
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
Select Kernel Source Per Document Experiment #11632
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f99e50
Initial port of prototype to main branch behind flag
ba2b06f
add in the kernel finder tagging
d6d8156
Some clean up work for kernel picker prototype
9297959
Change registered to property
c044224
set kernel source in contributed finders, not main finder
ca167eb
Merge branch 'main' into dev/ianhu/selectKernelPerDocument
c6c2105
review feedback to keep kernel metadata clear
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
59 changes: 59 additions & 0 deletions
59
src/notebooks/controllers/commands/pickDocumentKernelSourceCommand.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,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
import { inject, injectable } from 'inversify'; | ||
import { NotebookDocument, window } from 'vscode'; | ||
import { IExtensionSingleActivationService } from '../../../platform/activation/types'; | ||
import { ICommandManager } from '../../../platform/common/application/types'; | ||
import { Commands } from '../../../platform/common/constants'; | ||
import { ContextKey } from '../../../platform/common/contextKey'; | ||
import { IConfigurationService, IDisposableRegistry } from '../../../platform/common/types'; | ||
import { noop } from '../../../platform/common/utils/misc'; | ||
import { INotebookKernelSourceSelector } from '../types'; | ||
|
||
// Command that we will place into the kernel picker to determine what the controller source is for this document | ||
@injectable() | ||
export class PickDocumentKernelSourceCommand implements IExtensionSingleActivationService { | ||
private showPickDocumentKernelSourceContext: ContextKey; | ||
constructor( | ||
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry, | ||
@inject(ICommandManager) private readonly commandManager: ICommandManager, | ||
@inject(IConfigurationService) private readonly configService: IConfigurationService, | ||
@inject(INotebookKernelSourceSelector) private readonly kernelSourceSelector: INotebookKernelSourceSelector | ||
) { | ||
// Context keys to control when these commands are shown | ||
this.showPickDocumentKernelSourceContext = new ContextKey( | ||
'jupyter.pickDocumentKernelSourceContext', | ||
this.commandManager | ||
); | ||
} | ||
|
||
public async activate(): Promise<void> { | ||
// Register for config changes | ||
this.disposables.push(this.configService.getSettings().onDidChange(this.updateVisibility)); | ||
|
||
// Register our command to execute | ||
this.disposables.push( | ||
this.commandManager.registerCommand(Commands.PickDocumentKernelSource, this.pickDocumentKernelSource, this) | ||
); | ||
|
||
this.updateVisibility(); | ||
} | ||
|
||
private async pickDocumentKernelSource(_notebook?: NotebookDocument) { | ||
// We want to get the context here from the command, but that needs a fix on the core side: https://github.com/microsoft/vscode/issues/161445 | ||
if (window.activeNotebookEditor && this.configService.getSettings().kernelPickerType === 'Insiders') { | ||
await this.kernelSourceSelector.selectKernelSource(window.activeNotebookEditor.notebook); | ||
} | ||
} | ||
|
||
// Only show this command if we are in our Insiders picker type | ||
private updateVisibility() { | ||
if (this.configService.getSettings().kernelPickerType === 'Insiders') { | ||
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. It seems we are not picking up the context key change in the kernel picker so after changing the setting, a window reload is required for it to work. We might need a fix in the core. |
||
this.showPickDocumentKernelSourceContext.set(true).then(noop, noop); | ||
} else { | ||
this.showPickDocumentKernelSourceContext.set(false).then(noop, noop); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/notebooks/controllers/kernelSource/notebookKernelSourceSelector.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,39 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { NotebookDocument, QuickPickItem } from 'vscode'; | ||
import { IContributedKernelFinderInfo } from '../../../kernels/internalTypes'; | ||
import { IKernelFinder } from '../../../kernels/types'; | ||
import { IApplicationShell } from '../../../platform/common/application/types'; | ||
import { INotebookKernelSourceSelector, INotebookKernelSourceTracker } from '../types'; | ||
|
||
interface KernelFinderQuickPickItem extends QuickPickItem { | ||
kernelFinderInfo: IContributedKernelFinderInfo; | ||
} | ||
|
||
// Provides the UI to select a Kernel Source for a given notebook document | ||
@injectable() | ||
export class NotebookKernelSourceSelector implements INotebookKernelSourceSelector { | ||
constructor( | ||
@inject(IApplicationShell) private readonly applicationShell: IApplicationShell, | ||
@inject(INotebookKernelSourceTracker) private readonly kernelSourceTracker: INotebookKernelSourceTracker, | ||
@inject(IKernelFinder) private readonly kernelFinder: IKernelFinder | ||
) {} | ||
|
||
public async selectKernelSource(notebook: NotebookDocument): Promise<void> { | ||
const quickPickItems = this.kernelFinder.registered.map(this.toQuickPickItem); | ||
const selectedItem = await this.applicationShell.showQuickPick(quickPickItems); | ||
|
||
// If we selected something persist that value | ||
if (selectedItem) { | ||
this.kernelSourceTracker.setKernelSourceForNotebook(notebook, selectedItem.kernelFinderInfo); | ||
} | ||
} | ||
|
||
toQuickPickItem(kernelFinderInfo: IContributedKernelFinderInfo): KernelFinderQuickPickItem { | ||
return { kernelFinderInfo, label: kernelFinderInfo.displayName }; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
src/notebooks/controllers/kernelSource/notebookKernelSourceTracker.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,118 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { NotebookControllerAffinity2, NotebookDocument, workspace } from 'vscode'; | ||
import { IContributedKernelFinderInfo } from '../../../kernels/internalTypes'; | ||
import { IKernelFinder } from '../../../kernels/types'; | ||
import { IExtensionSyncActivationService } from '../../../platform/activation/types'; | ||
import { IDisposableRegistry } from '../../../platform/common/types'; | ||
import { IControllerRegistration, INotebookKernelSourceTracker, IVSCodeNotebookController } from '../types'; | ||
|
||
// Controls which kernel source is associated with each document, and controls hiding and showing kernel sources for them. | ||
@injectable() | ||
export class NotebookKernelSourceTracker implements INotebookKernelSourceTracker, IExtensionSyncActivationService { | ||
private documentSourceMapping: Map<NotebookDocument, IContributedKernelFinderInfo | undefined> = new Map< | ||
NotebookDocument, | ||
IContributedKernelFinderInfo | undefined | ||
>(); | ||
|
||
constructor( | ||
@inject(IDisposableRegistry) private readonly disposableRegistry: IDisposableRegistry, | ||
@inject(IControllerRegistration) private readonly controllerRegistration: IControllerRegistration, | ||
@inject(IKernelFinder) private readonly kernelFinder: IKernelFinder | ||
) {} | ||
|
||
activate(): void { | ||
workspace.onDidOpenNotebookDocument(this.onDidOpenNotebookDocument, this, this.disposableRegistry); | ||
workspace.onDidCloseNotebookDocument(this.onDidCloseNotebookDocument, this, this.disposableRegistry); | ||
this.controllerRegistration.onCreated(this.onCreatedController, this, this.disposableRegistry); | ||
|
||
// Tag all open documents | ||
workspace.notebookDocuments.forEach(this.onDidOpenNotebookDocument.bind(this)); | ||
} | ||
|
||
public getKernelSourceForNotebook(notebook: NotebookDocument): IContributedKernelFinderInfo | undefined { | ||
return this.documentSourceMapping.get(notebook); | ||
} | ||
public setKernelSourceForNotebook(notebook: NotebookDocument, kernelSource: IContributedKernelFinderInfo): void { | ||
this.documentSourceMapping.set(notebook, kernelSource); | ||
|
||
// After setting the kernelsource we now need to change the affinity of the controllers to hide all controllers not from that finder | ||
this.updateControllerAffinity(notebook, kernelSource); | ||
} | ||
|
||
// When a controller is created, see if it shows or hides for all open documents | ||
private onCreatedController(controller: IVSCodeNotebookController) { | ||
this.documentSourceMapping.forEach((finderInfo, notebook) => { | ||
const controllerFinderInfo = this.kernelFinder.getFinderForConnection(controller.connection); | ||
if (controllerFinderInfo && finderInfo && controllerFinderInfo.id === finderInfo.id) { | ||
// Match, associate with controller | ||
this.associateController(notebook, controller); | ||
} else { | ||
this.disassociateController(notebook, controller); | ||
} | ||
}); | ||
} | ||
|
||
private updateControllerAffinity(notebook: NotebookDocument, kernelSource: IContributedKernelFinderInfo) { | ||
// Find the controller associated with the given kernel source | ||
const nonAssociatedControllers = this.controllerRegistration.registered.filter( | ||
(controller) => !this.controllerMatchesKernelSource(controller, kernelSource) | ||
); | ||
const associatedControllers = this.controllerRegistration.registered.filter((controller) => | ||
this.controllerMatchesKernelSource(controller, kernelSource) | ||
); | ||
|
||
// At this point we need to pipe in our suggestion engine, right now everything will end up with default priority | ||
|
||
// Change the visibility on our controllers for that document | ||
nonAssociatedControllers.forEach((controller) => { | ||
this.disassociateController(notebook, controller); | ||
}); | ||
associatedControllers.forEach((controller) => { | ||
this.associateController(notebook, controller); | ||
}); | ||
} | ||
|
||
// Matching function to filter if controllers match a specific source | ||
private controllerMatchesKernelSource( | ||
controller: IVSCodeNotebookController, | ||
kernelSource: IContributedKernelFinderInfo | ||
): boolean { | ||
const controllerFinderInfo = this.kernelFinder.getFinderForConnection(controller.connection); | ||
if (controllerFinderInfo && controllerFinderInfo.id === kernelSource.id) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private associateController(notebook: NotebookDocument, controller: IVSCodeNotebookController) { | ||
controller.controller.updateNotebookAffinity(notebook, NotebookControllerAffinity2.Default); | ||
} | ||
|
||
private disassociateController(notebook: NotebookDocument, controller: IVSCodeNotebookController) { | ||
controller.controller.updateNotebookAffinity(notebook, NotebookControllerAffinity2.Hidden); | ||
} | ||
|
||
private onDidOpenNotebookDocument(notebook: NotebookDocument) { | ||
this.documentSourceMapping.set(notebook, undefined); | ||
|
||
// We need persistance here moving forward, but for now, we just default to a fresh state of | ||
// not having a kernel source selected when we first open a document. | ||
this.controllerRegistration.registered.forEach((controller) => { | ||
this.disassociateController(notebook, controller); | ||
}); | ||
} | ||
|
||
private onDidCloseNotebookDocument(notebook: NotebookDocument) { | ||
// Associate controller back to default on close | ||
this.controllerRegistration.registered.forEach((controller) => { | ||
this.associateController(notebook, controller); | ||
}); | ||
|
||
this.documentSourceMapping.delete(notebook); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Also added this so that the UI could see what kernel finders are registered. I didn't just go off the controllers that we see registered since if you add a kernel source with no controllers you would still want to see it as an option in the UI so you know that you connected to the server correctly.