Skip to content

Commit cdad3f1

Browse files
Handle both API versions (#15932) (#15936)
1 parent 2ec8cab commit cdad3f1

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/client/jupyter/languageserver/notebookConcatDocument.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ import { NotebookConcatTextDocument, NotebookCell, NotebookDocument } from 'vsco
2121
import { IVSCodeNotebook } from '../../common/application/types';
2222
import { IDisposable } from '../../common/types';
2323
import { PYTHON_LANGUAGE } from '../../common/constants';
24+
import { SafeNotebookDocument } from './safeNotebookDocument';
2425

2526
const NotebookConcatPrefix = '_NotebookConcat_';
2627

2728
/**
2829
* This helper class is used to present a converted document to an LS
2930
*/
3031
export class NotebookConcatDocument implements TextDocument, IDisposable {
32+
public get notebook(): SafeNotebookDocument {
33+
return this._notebook;
34+
}
35+
3136
public get notebookUri(): Uri {
3237
return this.notebook.uri;
3338
}
@@ -113,8 +118,14 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
113118

114119
private onCellsChangedEmitter = new EventEmitter<TextDocumentChangeEvent>();
115120

116-
constructor(public notebook: NotebookDocument, notebookApi: IVSCodeNotebook, selector: DocumentSelector) {
121+
private _notebook: SafeNotebookDocument;
122+
123+
constructor(notebook: NotebookDocument, notebookApi: IVSCodeNotebook, selector: DocumentSelector) {
117124
const dir = path.dirname(notebook.uri.fsPath);
125+
// Create a safe notebook document so that we can handle both >= 1.56 vscode API and < 1.56
126+
// when vscode stable is 1.56 and both Python release and insiders can update to that engine version we
127+
// can remove this and just use NotebookDocument directly
128+
this._notebook = new SafeNotebookDocument(notebook);
118129
// Note: Has to be different than the prefix for old notebook editor (HiddenFileFormat) so
119130
// that the caller doesn't remove diagnostics for this document.
120131
this.dummyFilePath = path.join(dir, `${NotebookConcatPrefix}${uuid().replace(/-/g, '')}.py`);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)