From f91a1ed4124742dc575577eeff2439abd88032b4 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 18 Mar 2021 10:42:50 -0700 Subject: [PATCH 1/3] Fix for #4965 --- src/client/datascience/openNotebookBanner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/datascience/openNotebookBanner.ts b/src/client/datascience/openNotebookBanner.ts index 3d47a232187..90d4210a421 100644 --- a/src/client/datascience/openNotebookBanner.ts +++ b/src/client/datascience/openNotebookBanner.ts @@ -30,7 +30,7 @@ export class OpenNotebookBanner implements IExtensionSingleActivationService { private async openedNotebook(editor: INotebookEditor) { if ( !this.pythonExtensionChecker.isPythonExtensionInstalled && - editor.model.metadata && + editor.model.metadata?.kernelspec && isPythonNotebook(editor.model.metadata) && !isUntitledFile(editor.file) ) { From e0c21d649a758dd6a930edaee70a7a004f468424 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 18 Mar 2021 11:36:08 -0700 Subject: [PATCH 2/3] Misc changes --- .../notebook/defaultCellLanguageService.ts | 8 ++++- .../datascience/notebook/helpers/helpers.ts | 7 ++--- .../notebook/kernelWithMetadata.ts | 30 +++++++++++++++---- .../notebookStorage/vscNotebookModel.ts | 5 +--- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/client/datascience/notebook/defaultCellLanguageService.ts b/src/client/datascience/notebook/defaultCellLanguageService.ts index a0bc6dd0067..2bf2bc533b3 100644 --- a/src/client/datascience/notebook/defaultCellLanguageService.ts +++ b/src/client/datascience/notebook/defaultCellLanguageService.ts @@ -7,6 +7,7 @@ import type { nbformat } from '@jupyterlab/coreutils/lib/nbformat'; import { inject, injectable, named } from 'inversify'; import { Memento, NotebookCellKind, NotebookDocument } from 'vscode'; import { IExtensionSingleActivationService } from '../../activation/types'; +import { IPythonExtensionChecker } from '../../api/types'; import { IVSCodeNotebook } from '../../common/application/types'; import { PYTHON_LANGUAGE } from '../../common/constants'; import { traceWarning } from '../../common/logger'; @@ -27,6 +28,7 @@ export class NotebookCellLanguageService implements IExtensionSingleActivationSe constructor( @inject(IVSCodeNotebook) private readonly vscNotebook: IVSCodeNotebook, @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry, + @inject(IPythonExtensionChecker) private readonly pythonExtensionChecker: IPythonExtensionChecker, @inject(IMemento) @named(GLOBAL_MEMENTO) private readonly globalMemento: Memento ) {} /** @@ -38,7 +40,11 @@ export class NotebookCellLanguageService implements IExtensionSingleActivationSe metadata?.language_info?.name || (metadata?.kernelspec as IJupyterKernelSpec | undefined)?.language || this.lastSavedNotebookCellLanguage; - return translateKernelLanguageToMonaco(jupyterLanguage || PYTHON_LANGUAGE); + + // Default to python language only if the Python extension is installed. + const defaultLanguage = this.pythonExtensionChecker.isPythonExtensionInstalled ? PYTHON_LANGUAGE : 'plaintext'; + // Note, what ever language is returned here, when the user selects a kernel, the cells (of blank documents) get updated based on that kernel selection. + return translateKernelLanguageToMonaco(jupyterLanguage || defaultLanguage); } public async activate() { this.vscNotebook.onDidSaveNotebookDocument(this.onDidSaveNotebookDocument, this, this.disposables); diff --git a/src/client/datascience/notebook/helpers/helpers.ts b/src/client/datascience/notebook/helpers/helpers.ts index 653e17e6a96..01e115b46af 100644 --- a/src/client/datascience/notebook/helpers/helpers.ts +++ b/src/client/datascience/notebook/helpers/helpers.ts @@ -107,11 +107,8 @@ export function isPythonNotebook(metadata?: nbformat.INotebookMetadata) { if (metadata?.language_info?.name && metadata.language_info.name !== PYTHON_LANGUAGE) { return false; } - if (kernelSpec?.language && kernelSpec.language !== PYTHON_LANGUAGE) { - return false; - } - // All other notebooks are python notebooks. - return true; + // Valid notebooks will have a language information in the metadata. + return kernelSpec?.language === PYTHON_LANGUAGE; } /** * No need to update the notebook metadata just yet. diff --git a/src/client/datascience/notebook/kernelWithMetadata.ts b/src/client/datascience/notebook/kernelWithMetadata.ts index a1eccf28331..e56c3852aef 100644 --- a/src/client/datascience/notebook/kernelWithMetadata.ts +++ b/src/client/datascience/notebook/kernelWithMetadata.ts @@ -3,7 +3,7 @@ // eslint-disable-next-line @typescript-eslint/no-require-imports import { join } from 'path'; -import { Uri, NotebookCell, NotebookDocument, NotebookKernel as VSCNotebookKernel } from 'vscode'; +import { Uri, NotebookCell, NotebookDocument, NotebookKernel as VSCNotebookKernel, notebook } from 'vscode'; import { ICommandManager, IVSCodeNotebook } from '../../common/application/types'; import { disposeAllDisposables } from '../../common/helpers'; import { traceInfo } from '../../common/logger'; @@ -16,6 +16,7 @@ import { KernelSocketInformation } from '../types'; import { traceCellMessage, trackKernelInfoInNotebookMetadata } from './helpers/helpers'; export class VSCodeNotebookKernelMetadata implements VSCNotebookKernel { + private notebookKernels = new WeakMap(); get preloads(): Uri[] { return [ Uri.file(join(this.context.extensionPath, 'out', 'ipywidgets', 'dist', 'ipywidgets.js')), @@ -64,9 +65,22 @@ export class VSCodeNotebookKernelMetadata implements VSCNotebookKernel { this.commandManager.executeCommand(Commands.NotebookEditorInterruptKernel).then(noop, noop); } private updateKernelInfoInNotebookWhenAvailable(kernel: IKernel, doc: NotebookDocument) { + if (this.notebookKernels.get(doc) === kernel) { + return; + } + this.notebookKernels.set(doc, kernel); let kernelSocket: KernelSocketInformation | undefined; const handlerDisposables: IDisposable[] = []; - + // If the notebook is closed, dispose everything. + notebook.onDidCloseNotebookDocument( + (e) => { + if (e === doc) { + disposeAllDisposables(handlerDisposables); + } + }, + this, + handlerDisposables + ); const saveKernelInfo = () => { const kernelId = kernelSocket?.options.id; if (!kernelId) { @@ -74,8 +88,6 @@ export class VSCodeNotebookKernelMetadata implements VSCNotebookKernel { } traceInfo(`Updating preferred kernel for remote notebook ${kernelId}`); this.preferredRemoteKernelIdProvider.storePreferredRemoteKernelId(doc.uri, kernelId).catch(noop); - - disposeAllDisposables(handlerDisposables); }; const kernelDisposedDisposable = kernel.onDisposed(() => disposeAllDisposables(handlerDisposables)); @@ -92,8 +104,14 @@ export class VSCodeNotebookKernelMetadata implements VSCNotebookKernel { return; } trackKernelInfoInNotebookMetadata(doc, kernel.info); - if (kernel.info.status === 'ok' && this.selection.kind === 'startUsingKernelSpec') { - saveKernelInfo(); + if (this.selection.kind === 'startUsingKernelSpec') { + if (kernel.info.status === 'ok') { + saveKernelInfo(); + } else { + disposeAllDisposables(handlerDisposables); + } + } else { + disposeAllDisposables(handlerDisposables); } }); diff --git a/src/client/datascience/notebookStorage/vscNotebookModel.ts b/src/client/datascience/notebookStorage/vscNotebookModel.ts index 2032b699dc1..16c9d0aef65 100644 --- a/src/client/datascience/notebookStorage/vscNotebookModel.ts +++ b/src/client/datascience/notebookStorage/vscNotebookModel.ts @@ -83,15 +83,12 @@ export class VSCodeNotebookModel extends BaseNotebookModel { return this.document ? this.document.cells.length : this.notebookJson.cells?.length ?? 0; } public getNotebookData() { - if (!this.preferredLanguage) { - throw new Error('Preferred Language not initialized'); - } return notebookModelToVSCNotebookData( this.isTrusted, this.notebookContentWithoutCells, this.file, this.notebookJson.cells || [], - this.preferredLanguage, + this.preferredLanguage || 'plaintext', this.originalJson ); } From e18ba58f7663a0a796fccfd437be6c818bc12edc Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 18 Mar 2021 18:48:49 -0700 Subject: [PATCH 3/3] Misc --- src/client/datascience/jupyter/kernels/helpers.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/datascience/jupyter/kernels/helpers.ts b/src/client/datascience/jupyter/kernels/helpers.ts index 130dba68623..bb7ca088d60 100644 --- a/src/client/datascience/jupyter/kernels/helpers.ts +++ b/src/client/datascience/jupyter/kernels/helpers.ts @@ -339,8 +339,9 @@ export function findPreferredKernel( // If still not found, look for a match based on notebook metadata and interpreter if (index < 0) { + const hasLanguageInfo = notebookMetadata?.language_info?.name ? true : false; const nbMetadataLanguage = - !notebookMetadata || isPythonNotebook(notebookMetadata) + !notebookMetadata || isPythonNotebook(notebookMetadata) || !hasLanguageInfo ? PYTHON_LANGUAGE : ( (notebookMetadata?.kernelspec?.language as string) || notebookMetadata?.language_info?.name