Skip to content

Commit

Permalink
only set notebook open context key if it uses a jupyter kernel (#16226)
Browse files Browse the repository at this point in the history
  • Loading branch information
amunger authored Nov 18, 2024
1 parent 5253973 commit 270fd6f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@
{
"type": "webview",
"id": "jupyterViewVariables",
"name": "Variables",
"name": "Jupyter Variables",
"when": "jupyter.hasNativeNotebookOrInteractiveWindowOpen"
}
]
Expand Down
4 changes: 2 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
"jupyter.command.jupyter.showDataViewer.title": "View Value in Data Viewer",
"jupyter.command.jupyter.viewOutput.title": "Show Output",
"jupyter.command.jupyter.clearSavedJupyterUris.title": "Clear Jupyter Remote Server List",
"jupyter.command.jupyter.openVariableView.title": "Open Variables View",
"jupyter.command.jupyter.openVariableView.shorttitle": "Variables",
"jupyter.command.jupyter.openVariableView.title": "Open Jupyter Variables View",
"jupyter.command.jupyter.openVariableView.shorttitle": "Jupyter Variables",
"jupyter.command.jupyter.openOutlineView.title": "Show Table Of Contents (Outline View)",
"jupyter.command.jupyter.openOutlineView.shorttitle": "Outline",
"jupyter.command.manageAccessToKernels": "Manage Access To Jupyter Kernels",
Expand Down
33 changes: 12 additions & 21 deletions src/standalone/context/activeEditorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { inject, injectable, optional } from 'inversify';
import { NotebookEditor, TextEditor, window, workspace } from 'vscode';
import { NotebookDocument, NotebookEditor, TextEditor, window, workspace } from 'vscode';
import { IKernel, IKernelProvider, isRemoteConnection } from '../../kernels/types';
import { IExtensionSyncActivationService } from '../../platform/activation/types';
import { EditorContexts, PYTHON_LANGUAGE } from '../../platform/common/constants';
Expand Down Expand Up @@ -98,16 +98,6 @@ export class ActiveEditorContextService implements IExtensionSyncActivationServi
if (window.activeTextEditor?.document.languageId === PYTHON_LANGUAGE) {
this.onDidChangeActiveTextEditor(window.activeTextEditor);
}
window.onDidChangeNotebookEditorSelection(
this.updateNativeNotebookInteractiveWindowOpenContext,
this,
this.disposables
);
workspace.onDidOpenNotebookDocument(
this.updateNativeNotebookInteractiveWindowOpenContext,
this,
this.disposables
);
workspace.onDidCloseNotebookDocument(
this.updateNativeNotebookInteractiveWindowOpenContext,
this,
Expand All @@ -124,7 +114,6 @@ export class ActiveEditorContextService implements IExtensionSyncActivationServi
}
private onDidChangeActiveInteractiveWindow(e?: IInteractiveWindow) {
this.interactiveContext.set(!!e).catch(noop);
this.updateNativeNotebookInteractiveWindowOpenContext();
this.updateMergedContexts();
this.updateContextOfActiveInteractiveWindowKernel();
}
Expand All @@ -137,19 +126,20 @@ export class ActiveEditorContextService implements IExtensionSyncActivationServi
.catch(noop);
this.updateContextOfActiveNotebookKernel(e);
this.updateContextOfActiveInteractiveWindowKernel();
this.updateNativeNotebookInteractiveWindowOpenContext();
this.updateNativeNotebookCellContext();
this.updateMergedContexts();
}
private updateNativeNotebookInteractiveWindowOpenContext() {
this.hasNativeNotebookOrInteractiveWindowOpen
.set(
workspace.notebookDocuments.some(
(nb) => nb.notebookType === JupyterNotebookView || nb.notebookType === InteractiveWindowView
)
)
.catch(noop);

private ownedOpenNotebooks = new Set<NotebookDocument>();
private updateNativeNotebookInteractiveWindowOpenContext(e: NotebookDocument, jupyterKernelSelected?: boolean) {
if (jupyterKernelSelected) {
this.ownedOpenNotebooks.add(e);
} else {
this.ownedOpenNotebooks.delete(e);
}
this.hasNativeNotebookOrInteractiveWindowOpen.set(this.ownedOpenNotebooks.size > 0).catch(noop);
}

private updateContextOfActiveNotebookKernel(activeEditor?: NotebookEditor) {
const kernel =
activeEditor && activeEditor.notebook.notebookType === JupyterNotebookView
Expand Down Expand Up @@ -195,6 +185,7 @@ export class ActiveEditorContextService implements IExtensionSyncActivationServi
this.interactiveProvider?.getActiveOrAssociatedInteractiveWindow()?.notebookDocument;
if (document && isJupyterNotebook(document) && this.controllers.getSelected(document)) {
this.isJupyterKernelSelected.set(true).catch(noop);
this.updateNativeNotebookInteractiveWindowOpenContext(document, true);
} else {
this.isJupyterKernelSelected.set(false).catch(noop);
}
Expand Down

0 comments on commit 270fd6f

Please sign in to comment.