Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/3 Code Health/15885.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update notebook code to not use deprecated .cells function on NotebookDocument.
33 changes: 19 additions & 14 deletions src/client/jupyter/languageserver/notebookConcatDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
const { NotebookCellKind } = require('vscode');
// Return Python if we have python cells.
if (
this.notebook.cells.some((item) => item.document.languageId.toLowerCase() === PYTHON_LANGUAGE.toLowerCase())
this.notebook
.getCells()
.some((item) => item.document.languageId.toLowerCase() === PYTHON_LANGUAGE.toLowerCase())
) {
return PYTHON_LANGUAGE;
}
Expand All @@ -58,7 +60,7 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
// in which case the language server will never kick in.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (
this.notebook.cells.find(
this.notebook.getCells().find(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(item) => ((item as any).cellKind || item.kind) === NotebookCellKind.Code,
)?.document?.languageId || PYTHON_LANGUAGE
Expand All @@ -83,7 +85,10 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
}

public get lineCount(): number {
return this.notebook.cells.map((c) => c.document.lineCount).reduce((p, c) => p + c);
return this.notebook
.getCells()
.map((c) => c.document.lineCount)
.reduce((p, c) => p + c);
}

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

// Get the cell at this location
const cell = this.notebook.cells.find((c) => c.document.uri.toString() === location.uri.toString());
const cell = this.notebook.getCells().find((c) => c.document.uri.toString() === location.uri.toString());
return cell!.document.lineAt(location.range.start);
}

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

// Get the cell at this location
const cell = this.notebook.cells.find((c) => c.document.uri.toString() === location.uri.toString());
const cell = this.notebook.getCells().find((c) => c.document.uri.toString() === location.uri.toString());
return cell!.document.getWordRangeAtPosition(location.range.start, regexp);
}

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

public getCellAtPosition(position: Position): NotebookCell | undefined {
const location = this.concatDocument.locationAt(position);
return this.notebook.cells.find((c) => c.document.uri === location.uri);
return this.notebook.getCells().find((c) => c.document.uri === location.uri);
}

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

private onDidChange() {
this._version += 1;
const newUris = this.notebook.cells.map((c) => c.document.uri.toString());
const newUris = this.notebook.getCells().map((c) => c.document.uri.toString());
const oldUris = this.cellTracking.map((c) => c.uri.toString());

// See if number of cells or cell positions changed
if (this.cellTracking.length < this.notebook.cells.length) {
if (this.cellTracking.length < this.notebook.cellCount) {
this.raiseCellInsertions(oldUris);
} else if (this.cellTracking.length > this.notebook.cells.length) {
} else if (this.cellTracking.length > this.notebook.cellCount) {
this.raiseCellDeletions(newUris, oldUris);
} else if (!isEqual(oldUris, newUris)) {
this.raiseCellMovement();
Expand All @@ -216,8 +221,8 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
}

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

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

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

// Length should be old length
Expand Down
2 changes: 1 addition & 1 deletion src/client/jupyter/languageserver/notebookConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class NotebookConverter implements Disposable {
if (wrapper) {
// Diagnostics are supposed to be per file and are updated each time
// Make sure to clear out old ones first
wrapper.notebook.cells.forEach((c: NotebookCell) => {
wrapper.notebook.getCells().forEach((c: NotebookCell) => {
result.set(c.document.uri, []);
});

Expand Down
2 changes: 1 addition & 1 deletion src/test/insiders/languageServer.insiders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ suite('Insiders Test: Language Server', () => {
for (let i = 0; i < 5; i += 1) {
const locations = await vscode.commands.executeCommand<vscode.Location[]>(
'vscode.executeDefinitionProvider',
notebookDocument.cells[2].document.uri, // Second cell should have a function with the decorator on it
notebookDocument.cellAt(2).document.uri, // Second cell should have a function with the decorator on it
startPosition,
);
if (locations && locations.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/smoke/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function openNotebookAndWaitForLS(file: string): Promise<vscode.Not
// to fetch data for completion, hover.etc.
await vscode.commands.executeCommand(
'vscode.executeCompletionItemProvider',
notebook.document.cells[0].document.uri,
notebook.document.cellAt(0).document.uri,
new vscode.Position(0, 0),
);
// For for LS to get extracted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ suite('TensorBoard code lens provider', () => {
assert(window.activeTextEditor, 'No active editor');
const codeLenses = await commands.executeCommand<CodeLens[]>(
'vscode.executeCodeLensProvider',
notebook.document.cells[0].document.uri,
notebook.document.cellAt(0).document.uri,
);
assert.ok(codeLenses?.length && codeLenses.length > 0, 'Code lens provider did not provide codelenses');
});
Expand Down Expand Up @@ -134,7 +134,7 @@ suite('TensorBoard code lens provider', () => {
assert(window.activeTextEditor, 'No active editor');
const codeLenses = await commands.executeCommand<CodeLens[]>(
'vscode.executeCodeLensProvider',
notebook.document.cells[0].document.uri,
notebook.document.cellAt(0).document.uri,
);
assert.ok(codeLenses?.length && codeLenses.length > 0, 'Code lens provider did not provide codelenses');
});
Expand Down
Loading