From dc888cc125847e51df2045aa8ff3c81cba03330e Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Sat, 26 Jun 2021 13:11:41 -0700 Subject: [PATCH] Remove SafeNotebookDocument --- .../languageserver/notebookConcatDocument.ts | 7 +- .../languageserver/notebookConverter.ts | 13 +-- .../languageserver/safeNotebookDocument.ts | 91 ------------------- 3 files changed, 8 insertions(+), 103 deletions(-) delete mode 100644 src/client/jupyter/languageserver/safeNotebookDocument.ts diff --git a/src/client/jupyter/languageserver/notebookConcatDocument.ts b/src/client/jupyter/languageserver/notebookConcatDocument.ts index edae4464753e..9738ab848675 100644 --- a/src/client/jupyter/languageserver/notebookConcatDocument.ts +++ b/src/client/jupyter/languageserver/notebookConcatDocument.ts @@ -21,7 +21,6 @@ import { NotebookConcatTextDocument, NotebookCell, NotebookDocument } from 'vsco import { IVSCodeNotebook } from '../../common/application/types'; import { IDisposable } from '../../common/types'; import { PYTHON_LANGUAGE } from '../../common/constants'; -import { SafeNotebookDocument } from './safeNotebookDocument'; const NotebookConcatPrefix = '_NotebookConcat_'; @@ -29,7 +28,7 @@ const NotebookConcatPrefix = '_NotebookConcat_'; * This helper class is used to present a converted document to an LS */ export class NotebookConcatDocument implements TextDocument, IDisposable { - public get notebook(): SafeNotebookDocument { + public get notebook(): NotebookDocument { return this._notebook; } @@ -118,14 +117,14 @@ export class NotebookConcatDocument implements TextDocument, IDisposable { private onCellsChangedEmitter = new EventEmitter(); - private _notebook: SafeNotebookDocument; + private _notebook: NotebookDocument; constructor(notebook: NotebookDocument, notebookApi: IVSCodeNotebook, selector: DocumentSelector) { const dir = path.dirname(notebook.uri.fsPath); // Create a safe notebook document so that we can handle both >= 1.56 vscode API and < 1.56 // when vscode stable is 1.56 and both Python release and insiders can update to that engine version we // can remove this and just use NotebookDocument directly - this._notebook = new SafeNotebookDocument(notebook); + this._notebook = notebook; // Note: Has to be different than the prefix for old notebook editor (HiddenFileFormat) so // that the caller doesn't remove diagnostics for this document. this.dummyFilePath = path.join(dir, `${NotebookConcatPrefix}${uuid().replace(/-/g, '')}.py`); diff --git a/src/client/jupyter/languageserver/notebookConverter.ts b/src/client/jupyter/languageserver/notebookConverter.ts index 61fdf202fdaa..89be7c02ca51 100644 --- a/src/client/jupyter/languageserver/notebookConverter.ts +++ b/src/client/jupyter/languageserver/notebookConverter.ts @@ -34,7 +34,6 @@ import { NotebookCell, NotebookConcatTextDocument, NotebookDocument } from 'vsco import { IVSCodeNotebook } from '../../common/application/types'; import { IFileSystem } from '../../common/platform/types'; import { NotebookConcatDocument } from './notebookConcatDocument'; -import { SafeNotebookDocument } from './safeNotebookDocument'; /* Used by code actions. Disabled for now. function toRange(rangeLike: Range): Range { @@ -649,17 +648,15 @@ export class NotebookConverter implements Disposable { } private onDidOpenNotebook(doc: NotebookDocument) { - const safeDoc = new SafeNotebookDocument(doc); - if (this.notebookFilter.test(safeDoc.fileName)) { - this.getTextDocumentWrapper(safeDoc.uri); + if (this.notebookFilter.test(doc.uri.fsPath)) { + this.getTextDocumentWrapper(doc.uri); } } private onDidCloseNotebook(doc: NotebookDocument) { - const safeDoc = new SafeNotebookDocument(doc); - if (this.notebookFilter.test(safeDoc.fileName)) { - const key = NotebookConverter.getDocumentKey(safeDoc.uri); - const wrapper = this.getTextDocumentWrapper(safeDoc.uri); + if (this.notebookFilter.test(doc.uri.fsPath)) { + const key = NotebookConverter.getDocumentKey(doc.uri); + const wrapper = this.getTextDocumentWrapper(doc.uri); this.activeDocuments.delete(key); this.activeDocumentsOutgoingMap.delete(NotebookConverter.getDocumentKey(wrapper.uri)); } diff --git a/src/client/jupyter/languageserver/safeNotebookDocument.ts b/src/client/jupyter/languageserver/safeNotebookDocument.ts deleted file mode 100644 index 1c4f747c9eb2..000000000000 --- a/src/client/jupyter/languageserver/safeNotebookDocument.ts +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. -import { NotebookCell, NotebookRange, NotebookDocumentMetadata, Uri } from 'vscode'; -import { NotebookDocument } from 'vscode-proposed'; - -export interface ISafeNotebookDocument extends NotebookDocument {} - -// The old API for NotebookDocument for vscode engine version < 1.56 -interface IOldNotebookDocument { - readonly cells: ReadonlyArray; - readonly fileName: string; -} - -// In the Python extension we often need to support different changing -// VS Code api versions. This class adds a layer of indirection so that we -// can handle changes to the NotebookDocument class in the API -// When python extension ships with engine version 1.56 for stable and insiders we can remove -// this class and just use NotebookDocument directly -export class SafeNotebookDocument implements ISafeNotebookDocument { - constructor(private notebook: NotebookDocument) {} - - // Functions changed to handle multiple APIs - public getCells(range?: NotebookRange): NotebookCell[] { - if ('getCells' in this.notebook) { - return this.notebook.getCells(range); - } - // Old API with .cells - return [...(this.notebook as IOldNotebookDocument).cells]; - } - - public cellAt(index: number): NotebookCell { - if ('cellAt' in this.notebook) { - return this.notebook.cellAt(index); - } - - // Old API with .cells - return (this.notebook as IOldNotebookDocument).cells[index]; - } - - public get cellCount(): number { - if ('cellCount' in this.notebook) { - return this.notebook.cellCount; - } - - // Old API with .cells - return (this.notebook as IOldNotebookDocument).cells.length; - } - - public get fileName(): string { - if ('fileName' in this.notebook) { - // .fileName is only in the old API - return (this.notebook as IOldNotebookDocument).fileName; - } - - // In the new API use the URI - return this.notebook.uri.fsPath; - } - - // Functions directly implemented by NotebookDocument - public get uri(): Uri { - return this.notebook.uri; - } - - public get version(): number { - return this.notebook.version; - } - - public get isDirty(): boolean { - return this.notebook.isDirty; - } - - public get isUntitled(): boolean { - return this.notebook.isUntitled; - } - - public get isClosed(): boolean { - return this.notebook.isClosed; - } - - public get metadata(): NotebookDocumentMetadata { - return this.notebook.metadata; - } - - public get viewType(): string { - return this.notebook.viewType; - } - - public save(): Thenable { - return this.notebook.save(); - } -}