Skip to content

Commit 007594e

Browse files
authored
Remove cell index property and use build in prop (#14239)
* Remove cell index property and use build in prop * oops
1 parent 24bf9c5 commit 007594e

File tree

4 files changed

+24
-39
lines changed

4 files changed

+24
-39
lines changed

src/client/datascience/jupyter/kernels/cellExecution.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ export class CellExecution {
7474
return this._completed;
7575
}
7676

77-
private get cellIndex() {
78-
return this.cell.notebook.cells.indexOf(this.cell);
79-
}
80-
8177
private static sentExecuteCellTelemetry?: boolean;
8278

8379
private readonly oldCellRunState?: NotebookCellRunState;
@@ -125,7 +121,7 @@ export class CellExecution {
125121
// Ensure we clear the cell state and trigger a change.
126122
await clearCellForExecution(this.editor, this.cell);
127123
await this.editor.edit((edit) => {
128-
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
124+
edit.replaceCellMetadata(this.cell.index, {
129125
...this.cell.metadata,
130126
runStartTime: new Date().getTime()
131127
});
@@ -169,7 +165,7 @@ export class CellExecution {
169165
private async completedWithErrors(error: Partial<Error>) {
170166
this.sendPerceivedCellExecute();
171167
await this.editor.edit((edit) =>
172-
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
168+
edit.replaceCellMetadata(this.cell.index, {
173169
...this.cell.metadata,
174170
lastRunDuration: this.stopWatch.elapsedTime
175171
})
@@ -201,9 +197,8 @@ export class CellExecution {
201197
statusMessage = getCellStatusMessageBasedOnFirstCellErrorOutput(this.cell.outputs);
202198
}
203199

204-
const cellIndex = this.editor.document.cells.indexOf(this.cell);
205200
await this.editor.edit((edit) =>
206-
edit.replaceCellMetadata(cellIndex, {
201+
edit.replaceCellMetadata(this.cell.index, {
207202
...this.cell.metadata,
208203
runState,
209204
statusMessage
@@ -238,7 +233,7 @@ export class CellExecution {
238233
? vscodeNotebookEnums.NotebookCellRunState.Idle
239234
: this.oldCellRunState;
240235
await this.editor.edit((edit) =>
241-
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
236+
edit.replaceCellMetadata(this.cell.index, {
242237
...this.cell.metadata,
243238
runStartTime: undefined,
244239
runState
@@ -254,7 +249,7 @@ export class CellExecution {
254249
*/
255250
private async enqueue() {
256251
await this.editor.edit((edit) =>
257-
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
252+
edit.replaceCellMetadata(this.cell.index, {
258253
...this.cell.metadata,
259254
runState: vscodeNotebookEnums.NotebookCellRunState.Running
260255
})
@@ -414,7 +409,7 @@ export class CellExecution {
414409
}
415410

416411
// Append to the data (we would push here but VS code requires a recreation of the array)
417-
edit.replaceCellOutput(this.cell.notebook.cells.indexOf(this.cell), existingOutput.concat(converted));
412+
edit.replaceCellOutput(this.cell.index, existingOutput.concat(converted));
418413
});
419414
}
420415

@@ -504,7 +499,7 @@ export class CellExecution {
504499
existing.data['text/plain'] = formatStreamText(
505500
concatMultilineString(`${existing.data['text/plain']}${msg.content.text}`)
506501
);
507-
edit.replaceCellOutput(this.cellIndex, [...exitingCellOutput]); // This is necessary to get VS code to update (for now)
502+
edit.replaceCellOutput(this.cell.index, [...exitingCellOutput]); // This is necessary to get VS code to update (for now)
508503
} else {
509504
const originalText = formatStreamText(concatMultilineString(msg.content.text));
510505
// Create a new stream entry
@@ -513,7 +508,7 @@ export class CellExecution {
513508
name: msg.content.name,
514509
text: originalText
515510
};
516-
edit.replaceCellOutput(this.cellIndex, [...exitingCellOutput, cellOutputToVSCCellOutput(output)]);
511+
edit.replaceCellOutput(this.cell.index, [...exitingCellOutput, cellOutputToVSCCellOutput(output)]);
517512
}
518513
});
519514
}
@@ -536,7 +531,7 @@ export class CellExecution {
536531
clearState.update(true);
537532
} else {
538533
// Clear all outputs and start over again.
539-
await this.editor.edit((edit) => edit.replaceCellOutput(this.cellIndex, []));
534+
await this.editor.edit((edit) => edit.replaceCellOutput(this.cell.index, []));
540535
}
541536
}
542537

src/client/datascience/notebook/helpers/executionHelpers.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ export async function updateCellWithErrorStatus(
6565
cell: NotebookCell,
6666
ex: Partial<Error>
6767
) {
68-
const cellIndex = cell.notebook.cells.indexOf(cell);
6968
await notebookEditor.edit((edit) => {
70-
edit.replaceCellMetadata(cellIndex, {
69+
edit.replaceCellMetadata(cell.index, {
7170
...cell.metadata,
7271
runState: vscodeNotebookEnums.NotebookCellRunState.Error
7372
});
74-
edit.replaceCellOutput(cellIndex, [translateErrorOutput(createErrorOutput(ex))]);
73+
edit.replaceCellOutput(cell.index, [translateErrorOutput(createErrorOutput(ex))]);
7574
});
7675
}
7776

@@ -84,9 +83,8 @@ export async function updateCellExecutionCount(
8483
executionCount: number
8584
): Promise<void> {
8685
if (cell.metadata.executionOrder !== executionCount && executionCount) {
87-
const cellIndex = editor.document.cells.indexOf(cell);
8886
await editor.edit((edit) =>
89-
edit.replaceCellMetadata(cellIndex, {
87+
edit.replaceCellMetadata(cell.index, {
9088
...cell.metadata,
9189
executionOrder: executionCount
9290
})
@@ -109,6 +107,5 @@ export async function updateCellOutput(editor: NotebookEditor, cell: NotebookCel
109107
if (cell.outputs.length === newOutput.length && fastDeepEqual(cell.outputs, newOutput)) {
110108
return;
111109
}
112-
const cellIndex = cell.notebook.cells.indexOf(cell);
113-
await editor.edit((edit) => edit.replaceCellOutput(cellIndex, newOutput));
110+
await editor.edit((edit) => edit.replaceCellOutput(cell.index, newOutput));
114111
}

src/client/datascience/notebook/helpers/helpers.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,15 @@ export function createIOutputFromCellOutputs(cellOutputs: CellOutput[]): nbforma
343343
}
344344

345345
export async function clearCellForExecution(editor: NotebookEditor, cell: NotebookCell) {
346-
const cellIndex = cell.notebook.cells.indexOf(cell);
347346
await editor.edit((edit) => {
348-
edit.replaceCellMetadata(cellIndex, {
347+
edit.replaceCellMetadata(cell.index, {
349348
...cell.metadata,
350349
statusMessage: undefined,
351350
executionOrder: undefined,
352351
lastRunDuration: undefined,
353352
runStartTime: undefined
354353
});
355-
edit.replaceCellOutput(cellIndex, []);
354+
edit.replaceCellOutput(cell.index, []);
356355
});
357356
await updateCellExecutionTimes(editor, cell);
358357
}
@@ -366,8 +365,6 @@ export async function updateCellExecutionTimes(
366365
cell: NotebookCell,
367366
times?: { startTime?: number; lastRunDuration?: number }
368367
) {
369-
const cellIndex = cell.notebook.cells.indexOf(cell);
370-
371368
if (!times || !times.lastRunDuration || !times.startTime) {
372369
// Based on feedback from VSC, its best to clone these objects when updating them.
373370
const cellMetadata = cloneDeep(cell.metadata);
@@ -382,7 +379,7 @@ export async function updateCellExecutionTimes(
382379
}
383380
if (updated) {
384381
await editor.edit((edit) =>
385-
edit.replaceCellMetadata(cellIndex, {
382+
edit.replaceCellMetadata(cell.index, {
386383
...cellMetadata
387384
})
388385
);
@@ -401,7 +398,7 @@ export async function updateCellExecutionTimes(
401398
customMetadata.metadata.vscode.start_execution_time = startTimeISO;
402399
const lastRunDuration = times.lastRunDuration ?? cell.metadata.lastRunDuration;
403400
await editor.edit((edit) =>
404-
edit.replaceCellMetadata(cellIndex, {
401+
edit.replaceCellMetadata(cell.index, {
405402
...cell.metadata,
406403
custom: customMetadata,
407404
lastRunDuration

src/test/datascience/notebook/helper.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export async function waitForExecutionCompletedSuccessfully(cell: NotebookCell)
229229
await waitForCondition(
230230
async () => assertHasExecutionCompletedSuccessfully(cell),
231231
1_000,
232-
`Cell ${cell.notebook.cells.indexOf(cell) + 1} did not complete successfully`
232+
`Cell ${cell.index + 1} did not complete successfully`
233233
);
234234
}
235235
export function assertExecutionOrderInVSCCell(cell: NotebookCell, executionOrder?: number) {
@@ -240,7 +240,7 @@ export async function waitForExecutionOrderInVSCCell(cell: NotebookCell, executi
240240
await waitForCondition(
241241
async () => assertExecutionOrderInVSCCell(cell, executionOrder),
242242
1_000,
243-
`Execution count not '${executionOrder}' for Cell ${cell.notebook.cells.indexOf(cell) + 1}`
243+
`Execution count not '${executionOrder}' for Cell ${cell.index + 1}`
244244
);
245245
}
246246
export async function waitForExecutionOrderInCell(cell: NotebookCell, executionOrder: number | undefined) {
@@ -252,7 +252,7 @@ export async function waitForExecutionOrderInCell(cell: NotebookCell, executionO
252252
return cell.metadata.executionOrder === executionOrder;
253253
},
254254
15_000,
255-
`Execution count not '${executionOrder}' for Cell ${cell.notebook.cells.indexOf(cell)}`
255+
`Execution count not '${executionOrder}' for Cell ${cell.index}`
256256
);
257257
}
258258
export function assertHasExecutionCompletedWithErrors(cell: NotebookCell) {
@@ -262,7 +262,7 @@ export function assertHasExecutionCompletedWithErrors(cell: NotebookCell) {
262262
);
263263
}
264264
export function assertHasOutputInVSCell(cell: NotebookCell) {
265-
assert.ok(cell.outputs.length, `No output in Cell ${cell.notebook.cells.indexOf(cell) + 1}`);
265+
assert.ok(cell.outputs.length, `No output in Cell ${cell.index + 1}`);
266266
}
267267
export function assertHasOutputInICell(cell: ICell, model: INotebookModel) {
268268
assert.ok((cell.data.outputs as nbformat.IOutput[]).length, `No output in ICell ${model.cells.indexOf(cell) + 1}`);
@@ -289,7 +289,7 @@ export async function waitForTextOutputInVSCode(
289289
await waitForCondition(
290290
async () => assertHasTextOutputInVSCode(cell, text, index, isExactMatch),
291291
timeout,
292-
`Output does not contain provided text '${text}' for Cell ${cell.notebook.cells.indexOf(cell) + 1}`
292+
`Output does not contain provided text '${text}' for Cell ${cell.index + 1}`
293293
);
294294
}
295295
export function assertNotHasTextOutputInVSCode(cell: NotebookCell, text: string, index: number, isExactMatch = true) {
@@ -317,7 +317,7 @@ export async function waitForVSCCellHasEmptyOutput(cell: NotebookCell) {
317317
await waitForCondition(
318318
async () => cell.outputs.length === 0,
319319
1_000,
320-
`Cell ${cell.notebook.cells.indexOf(cell) + 1} output did not get cleared`
320+
`Cell ${cell.index + 1} output did not get cleared`
321321
);
322322
}
323323
export async function waitForCellHasEmptyOutput(cell: ICell, model: INotebookModel) {
@@ -328,11 +328,7 @@ export async function waitForCellHasEmptyOutput(cell: ICell, model: INotebookMod
328328
);
329329
}
330330
export async function waitForVSCCellIsRunning(cell: NotebookCell) {
331-
await waitForCondition(
332-
async () => assertVSCCellIsRunning(cell),
333-
1_000,
334-
`Cell ${cell.notebook.cells.indexOf(cell) + 1} did not start`
335-
);
331+
await waitForCondition(async () => assertVSCCellIsRunning(cell), 1_000, `Cell ${cell.index + 1} did not start`);
336332
}
337333
export function assertVSCCellIsNotRunning(cell: NotebookCell) {
338334
assert.notEqual(cell.metadata.runState, vscodeNotebookEnums.NotebookCellRunState.Running);

0 commit comments

Comments
 (0)