|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +import { NotebookCell, NotebookCellRange, NotebookDocumentMetadata, Uri } from 'vscode'; |
| 4 | +import { NotebookDocument } from 'vscode-proposed'; |
| 5 | + |
| 6 | +export interface ISafeNotebookDocument extends NotebookDocument {} |
| 7 | + |
| 8 | +// The old API for NotebookDocument for vscode engine version < 1.56 |
| 9 | +interface IOldNotebookDocument { |
| 10 | + readonly cells: ReadonlyArray<NotebookCell>; |
| 11 | +} |
| 12 | + |
| 13 | +// In the Python extension we often need to support different changing |
| 14 | +// VS Code api versions. This class adds a layer of indirection so that we |
| 15 | +// can handle changes to the NotebookDocument class in the API |
| 16 | +// When python extension ships with engine version 1.56 for stable and insiders we can remove |
| 17 | +// this class and just use NotebookDocument directly |
| 18 | +export class SafeNotebookDocument implements ISafeNotebookDocument { |
| 19 | + constructor(private notebook: NotebookDocument) {} |
| 20 | + |
| 21 | + // Functions changed to handle multiple APIs |
| 22 | + public getCells(range?: NotebookCellRange): ReadonlyArray<NotebookCell> { |
| 23 | + if ('getCells' in this.notebook) { |
| 24 | + return this.notebook.getCells(range); |
| 25 | + } |
| 26 | + // Old API with .cells |
| 27 | + return (this.notebook as IOldNotebookDocument).cells; |
| 28 | + } |
| 29 | + |
| 30 | + public cellAt(index: number): NotebookCell { |
| 31 | + if ('cellAt' in this.notebook) { |
| 32 | + return this.notebook.cellAt(index); |
| 33 | + } |
| 34 | + |
| 35 | + // Old API with .cells |
| 36 | + return (this.notebook as IOldNotebookDocument).cells[index]; |
| 37 | + } |
| 38 | + |
| 39 | + public get cellCount(): number { |
| 40 | + if ('cellCount' in this.notebook) { |
| 41 | + return this.notebook.cellCount; |
| 42 | + } |
| 43 | + |
| 44 | + // Old API with .cells |
| 45 | + return (this.notebook as IOldNotebookDocument).cells.length; |
| 46 | + } |
| 47 | + |
| 48 | + // Functions directly implemented by NotebookDocument |
| 49 | + public get uri(): Uri { |
| 50 | + return this.notebook.uri; |
| 51 | + } |
| 52 | + |
| 53 | + public get version(): number { |
| 54 | + return this.notebook.version; |
| 55 | + } |
| 56 | + |
| 57 | + public get fileName(): string { |
| 58 | + return this.notebook.fileName; |
| 59 | + } |
| 60 | + |
| 61 | + public get isDirty(): boolean { |
| 62 | + return this.notebook.isDirty; |
| 63 | + } |
| 64 | + |
| 65 | + public get isUntitled(): boolean { |
| 66 | + return this.notebook.isUntitled; |
| 67 | + } |
| 68 | + |
| 69 | + public get isClosed(): boolean { |
| 70 | + return this.notebook.isClosed; |
| 71 | + } |
| 72 | + |
| 73 | + public get metadata(): NotebookDocumentMetadata { |
| 74 | + return this.notebook.metadata; |
| 75 | + } |
| 76 | + |
| 77 | + public get viewType(): string { |
| 78 | + return this.notebook.viewType; |
| 79 | + } |
| 80 | + |
| 81 | + public save(): Thenable<boolean> { |
| 82 | + return this.notebook.save(); |
| 83 | + } |
| 84 | +} |
0 commit comments