Skip to content

Commit a5c8a36

Browse files
Update Notebook API for the python extension side of things (#15884)
1 parent 676ef37 commit a5c8a36

File tree

8 files changed

+1010
-846
lines changed

8 files changed

+1010
-846
lines changed

news/3 Code Health/15885.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update notebook code to not use deprecated .cells function on NotebookDocument.

src/client/jupyter/languageserver/notebookConcatDocument.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
4949
const { NotebookCellKind } = require('vscode');
5050
// Return Python if we have python cells.
5151
if (
52-
this.notebook.cells.some((item) => item.document.languageId.toLowerCase() === PYTHON_LANGUAGE.toLowerCase())
52+
this.notebook
53+
.getCells()
54+
.some((item) => item.document.languageId.toLowerCase() === PYTHON_LANGUAGE.toLowerCase())
5355
) {
5456
return PYTHON_LANGUAGE;
5557
}
@@ -58,7 +60,7 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
5860
// in which case the language server will never kick in.
5961
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6062
return (
61-
this.notebook.cells.find(
63+
this.notebook.getCells().find(
6264
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6365
(item) => ((item as any).cellKind || item.kind) === NotebookCellKind.Code,
6466
)?.document?.languageId || PYTHON_LANGUAGE
@@ -83,7 +85,10 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
8385
}
8486

8587
public get lineCount(): number {
86-
return this.notebook.cells.map((c) => c.document.lineCount).reduce((p, c) => p + c);
88+
return this.notebook
89+
.getCells()
90+
.map((c) => c.document.lineCount)
91+
.reduce((p, c) => p + c);
8792
}
8893

8994
public get onCellsChanged(): Event<TextDocumentChangeEvent> {
@@ -141,7 +146,7 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
141146
const location = this.concatDocument.locationAt(position);
142147

143148
// Get the cell at this location
144-
const cell = this.notebook.cells.find((c) => c.document.uri.toString() === location.uri.toString());
149+
const cell = this.notebook.getCells().find((c) => c.document.uri.toString() === location.uri.toString());
145150
return cell!.document.lineAt(location.range.start);
146151
}
147152

@@ -163,7 +168,7 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
163168
const location = this.concatDocument.locationAt(position);
164169

165170
// Get the cell at this location
166-
const cell = this.notebook.cells.find((c) => c.document.uri.toString() === location.uri.toString());
171+
const cell = this.notebook.getCells().find((c) => c.document.uri.toString() === location.uri.toString());
167172
return cell!.document.getWordRangeAtPosition(location.range.start, regexp);
168173
}
169174

@@ -177,12 +182,12 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
177182

178183
public getCellAtPosition(position: Position): NotebookCell | undefined {
179184
const location = this.concatDocument.locationAt(position);
180-
return this.notebook.cells.find((c) => c.document.uri === location.uri);
185+
return this.notebook.getCells().find((c) => c.document.uri === location.uri);
181186
}
182187

183188
private updateCellTracking() {
184189
this.cellTracking = [];
185-
this.notebook.cells.forEach((c) => {
190+
this.notebook.getCells().forEach((c) => {
186191
// Compute end position from number of lines in a cell
187192
const cellText = c.document.getText();
188193
const lines = cellText.splitLines({ trim: false });
@@ -197,13 +202,13 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
197202

198203
private onDidChange() {
199204
this._version += 1;
200-
const newUris = this.notebook.cells.map((c) => c.document.uri.toString());
205+
const newUris = this.notebook.getCells().map((c) => c.document.uri.toString());
201206
const oldUris = this.cellTracking.map((c) => c.uri.toString());
202207

203208
// See if number of cells or cell positions changed
204-
if (this.cellTracking.length < this.notebook.cells.length) {
209+
if (this.cellTracking.length < this.notebook.cellCount) {
205210
this.raiseCellInsertions(oldUris);
206-
} else if (this.cellTracking.length > this.notebook.cells.length) {
211+
} else if (this.cellTracking.length > this.notebook.cellCount) {
207212
this.raiseCellDeletions(newUris, oldUris);
208213
} else if (!isEqual(oldUris, newUris)) {
209214
this.raiseCellMovement();
@@ -216,8 +221,8 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
216221
}
217222

218223
public getEndPosition(): Position {
219-
if (this.notebook.cells.length > 0) {
220-
const finalCell = this.notebook.cells[this.notebook.cells.length - 1];
224+
if (this.notebook.cellCount > 0) {
225+
const finalCell = this.notebook.cellAt(this.notebook.cellCount - 1);
221226
const start = this.getPositionOfCell(finalCell.document.uri);
222227
const lines = finalCell.document.getText().splitLines({ trim: false });
223228
return new Position(start.line + lines.length, 0);
@@ -227,7 +232,7 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
227232

228233
private raiseCellInsertions(oldUris: string[]) {
229234
// One or more cells were added. Add a change event for each
230-
const insertions = this.notebook.cells.filter((c) => !oldUris.includes(c.document.uri.toString()));
235+
const insertions = this.notebook.getCells().filter((c) => !oldUris.includes(c.document.uri.toString()));
231236

232237
const changes = insertions.map((insertion) => {
233238
// Figure out the position of the item. This is where we're inserting the cell
@@ -265,7 +270,7 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
265270
// Figure out the position of the item in the new list
266271
const position =
267272
index < newUris.length
268-
? this.getPositionOfCell(this.notebook.cells[index].document.uri)
273+
? this.getPositionOfCell(this.notebook.cellAt(index).document.uri)
269274
: this.getEndPosition();
270275

271276
// Length should be old length

src/client/jupyter/languageserver/notebookConverter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class NotebookConverter implements Disposable {
126126
if (wrapper) {
127127
// Diagnostics are supposed to be per file and are updated each time
128128
// Make sure to clear out old ones first
129-
wrapper.notebook.cells.forEach((c: NotebookCell) => {
129+
wrapper.notebook.getCells().forEach((c: NotebookCell) => {
130130
result.set(c.document.uri, []);
131131
});
132132

src/test/insiders/languageServer.insiders.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ suite('Insiders Test: Language Server', () => {
9090
for (let i = 0; i < 5; i += 1) {
9191
const locations = await vscode.commands.executeCommand<vscode.Location[]>(
9292
'vscode.executeDefinitionProvider',
93-
notebookDocument.cells[2].document.uri, // Second cell should have a function with the decorator on it
93+
notebookDocument.cellAt(2).document.uri, // Second cell should have a function with the decorator on it
9494
startPosition,
9595
);
9696
if (locations && locations.length > 0) {

src/test/smoke/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function openNotebookAndWaitForLS(file: string): Promise<vscode.Not
6161
// to fetch data for completion, hover.etc.
6262
await vscode.commands.executeCommand(
6363
'vscode.executeCompletionItemProvider',
64-
notebook.document.cells[0].document.uri,
64+
notebook.document.cellAt(0).document.uri,
6565
new vscode.Position(0, 0),
6666
);
6767
// For for LS to get extracted.

src/test/tensorBoard/nbextensionCodeLensProvider.insiders.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ suite('TensorBoard code lens provider', () => {
7777
assert(window.activeTextEditor, 'No active editor');
7878
const codeLenses = await commands.executeCommand<CodeLens[]>(
7979
'vscode.executeCodeLensProvider',
80-
notebook.document.cells[0].document.uri,
80+
notebook.document.cellAt(0).document.uri,
8181
);
8282
assert.ok(codeLenses?.length && codeLenses.length > 0, 'Code lens provider did not provide codelenses');
8383
});
@@ -134,7 +134,7 @@ suite('TensorBoard code lens provider', () => {
134134
assert(window.activeTextEditor, 'No active editor');
135135
const codeLenses = await commands.executeCommand<CodeLens[]>(
136136
'vscode.executeCodeLensProvider',
137-
notebook.document.cells[0].document.uri,
137+
notebook.document.cellAt(0).document.uri,
138138
);
139139
assert.ok(codeLenses?.length && codeLenses.length > 0, 'Code lens provider did not provide codelenses');
140140
});

0 commit comments

Comments
 (0)