Skip to content

Commit

Permalink
delay showing install python commands until after controllers are loa…
Browse files Browse the repository at this point in the history
…ded (microsoft#11430)

* delay showing install python commands until after controllers are loaded

* small fix up

* move context creation to contstructor

Co-authored-by: Ian Huff <ianhuff@Ians-MacBook-Pro.local>
  • Loading branch information
IanMatthewHuff and Ian Huff committed Sep 22, 2022
1 parent b6a8f16 commit 06ca869
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions news/2 Fixes/10960.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make sure the "Install Python" and "Install Python Extension" commands only show up after we have loaded our controllers.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -978,11 +978,11 @@
"notebook/kernelSource": [
{
"command": "jupyter.installPythonExtensionViaKernelPicker",
"when": "jupyter.showInstallPythonExtensionCommand"
"when": "jupyter.showInstallPythonExtensionCommand && jupyter.controllersLoaded"
},
{
"command": "jupyter.installPythonViaKernelPicker",
"when": "jupyter.showInstallPythonCommand"
"when": "jupyter.showInstallPythonCommand && jupyter.controllersLoaded"
},
{
"command": "jupyter.switchToLocalKernels",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class InstallPythonControllerCommands implements IExtensionSingleActivati
}

private async onDidChangeActiveNotebookEditor(editor: NotebookEditor | undefined) {
if (!this.isWeb && editor) {
if (!this.isWeb && editor && editor.notebook.notebookType === JupyterNotebookView) {
// Make sure we are only showing these for python notebooks or undefined notebooks
const lang = getLanguageOfNotebookDocument(editor.notebook);
if (!lang || lang === PYTHON_LANGUAGE) {
Expand Down
21 changes: 18 additions & 3 deletions src/notebooks/controllers/controllerLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { isPythonNotebook } from '../../kernels/helpers';
import { IKernelFinder, KernelConnectionMetadata } from '../../kernels/types';
import { IExtensionSyncActivationService } from '../../platform/activation/types';
import { IPythonExtensionChecker } from '../../platform/api/types';
import { IVSCodeNotebook } from '../../platform/common/application/types';
import { ICommandManager, IVSCodeNotebook } from '../../platform/common/application/types';
import { isCancellationError } from '../../platform/common/cancellation';
import { InteractiveWindowView, JupyterNotebookView } from '../../platform/common/constants';
import { ContextKey } from '../../platform/common/contextKey';
import { IDisposableRegistry } from '../../platform/common/types';
import { getNotebookMetadata } from '../../platform/common/utils';
import { noop } from '../../platform/common/utils/misc';
Expand All @@ -30,14 +31,20 @@ export class ControllerLoader implements IControllerLoader, IExtensionSyncActiva
// Promise to resolve when we have loaded our controllers
private controllersPromise: Promise<void>;
private loadCancellationToken: vscode.CancellationTokenSource | undefined;
private controllersLoadedContext: ContextKey;
constructor(
@inject(IVSCodeNotebook) private readonly notebook: IVSCodeNotebook,
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
@inject(IKernelFinder) private readonly kernelFinder: IKernelFinder,
@inject(IPythonExtensionChecker) private readonly extensionChecker: IPythonExtensionChecker,
@inject(IInterpreterService) private readonly interpreters: IInterpreterService,
@inject(IControllerRegistration) private readonly registration: IControllerRegistration
) {}
@inject(IControllerRegistration) private readonly registration: IControllerRegistration,
@inject(ICommandManager) private readonly commandManager: ICommandManager
) {
// This context key is set to true when controllers have completed their intitial load or are done loading after a refresh
// It's set to false on activation and when a refresh is requested.
this.controllersLoadedContext = new ContextKey('jupyter.controllersLoaded', this.commandManager);
}

public activate(): void {
// Make sure to reload whenever we do something that changes state
Expand All @@ -57,6 +64,8 @@ export class ControllerLoader implements IControllerLoader, IExtensionSyncActiva
this.notebook.notebookDocuments.forEach((notebook) => this.onDidOpenNotebookDocument(notebook).catch(noop));

this.loadControllers(true).ignoreErrors();

this.controllersLoadedContext.set(false).then(noop, noop);
}
public loadControllers(refresh?: boolean | undefined): Promise<void> {
if (!this.controllersPromise || refresh) {
Expand Down Expand Up @@ -117,6 +126,9 @@ export class ControllerLoader implements IControllerLoader, IExtensionSyncActiva
}

private async loadControllersImpl(cancelToken: vscode.CancellationToken) {
// First off set our context to false as we are about to refresh
await this.controllersLoadedContext.set(false);

let connections = await this.kernelFinder.listKernels(undefined, cancelToken);

if (cancelToken.isCancellationRequested) {
Expand Down Expand Up @@ -149,6 +161,9 @@ export class ControllerLoader implements IControllerLoader, IExtensionSyncActiva

// Indicate a refresh
this.refreshedEmitter.fire();

// Set that we have loaded controllers
await this.controllersLoadedContext.set(true);
}

private createNotebookControllers(
Expand Down

0 comments on commit 06ca869

Please sign in to comment.