Skip to content

Remove cell index property and use build in prop #14239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2020
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
23 changes: 9 additions & 14 deletions src/client/datascience/jupyter/kernels/cellExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ export class CellExecution {
return this._completed;
}

private get cellIndex() {
return this.cell.notebook.cells.indexOf(this.cell);
}

private static sentExecuteCellTelemetry?: boolean;

private readonly oldCellRunState?: NotebookCellRunState;
Expand Down Expand Up @@ -130,7 +126,7 @@ export class CellExecution {
// Ensure we clear the cell state and trigger a change.
await clearCellForExecution(this.editor, this.cell);
await this.editor.edit((edit) => {
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
edit.replaceCellMetadata(this.cell.index, {
...this.cell.metadata,
runStartTime: new Date().getTime()
});
Expand Down Expand Up @@ -176,7 +172,7 @@ export class CellExecution {
private async completedWithErrors(error: Partial<Error>) {
this.sendPerceivedCellExecute();
await this.editor.edit((edit) =>
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
edit.replaceCellMetadata(this.cell.index, {
...this.cell.metadata,
lastRunDuration: this.stopWatch.elapsedTime
})
Expand Down Expand Up @@ -211,9 +207,8 @@ export class CellExecution {
statusMessage = getCellStatusMessageBasedOnFirstCellErrorOutput(this.cell.outputs);
}

const cellIndex = this.editor.document.cells.indexOf(this.cell);
await this.editor.edit((edit) =>
edit.replaceCellMetadata(cellIndex, {
edit.replaceCellMetadata(this.cell.index, {
...this.cell.metadata,
runState,
statusMessage
Expand Down Expand Up @@ -250,7 +245,7 @@ export class CellExecution {
? vscodeNotebookEnums.NotebookCellRunState.Idle
: this.oldCellRunState;
await this.editor.edit((edit) =>
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
edit.replaceCellMetadata(this.cell.index, {
...this.cell.metadata,
runStartTime: undefined,
runState
Expand All @@ -268,7 +263,7 @@ export class CellExecution {
*/
private async enqueue() {
await this.editor.edit((edit) =>
edit.replaceCellMetadata(this.cell.notebook.cells.indexOf(this.cell), {
edit.replaceCellMetadata(this.cell.index, {
...this.cell.metadata,
runState: vscodeNotebookEnums.NotebookCellRunState.Running
})
Expand Down Expand Up @@ -441,7 +436,7 @@ export class CellExecution {
}

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

Expand Down Expand Up @@ -531,7 +526,7 @@ export class CellExecution {
existing.data['text/plain'] = formatStreamText(
concatMultilineString(`${existing.data['text/plain']}${msg.content.text}`)
);
edit.replaceCellOutput(this.cellIndex, [...exitingCellOutput]); // This is necessary to get VS code to update (for now)
edit.replaceCellOutput(this.cell.index, [...exitingCellOutput]); // This is necessary to get VS code to update (for now)
} else {
const originalText = formatStreamText(concatMultilineString(msg.content.text));
// Create a new stream entry
Expand All @@ -540,7 +535,7 @@ export class CellExecution {
name: msg.content.name,
text: originalText
};
edit.replaceCellOutput(this.cellIndex, [...exitingCellOutput, cellOutputToVSCCellOutput(output)]);
edit.replaceCellOutput(this.cell.index, [...exitingCellOutput, cellOutputToVSCCellOutput(output)]);
}
});
}
Expand All @@ -563,7 +558,7 @@ export class CellExecution {
clearState.update(true);
} else {
// Clear all outputs and start over again.
await this.editor.edit((edit) => edit.replaceCellOutput(this.cellIndex, []));
await this.editor.edit((edit) => edit.replaceCellOutput(this.cell.index, []));
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/client/datascience/notebook/helpers/executionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ export async function updateCellWithErrorStatus(
cell: NotebookCell,
ex: Partial<Error>
) {
const cellIndex = cell.notebook.cells.indexOf(cell);
await notebookEditor.edit((edit) => {
edit.replaceCellMetadata(cellIndex, {
edit.replaceCellMetadata(cell.index, {
...cell.metadata,
runState: vscodeNotebookEnums.NotebookCellRunState.Error
});
edit.replaceCellOutput(cellIndex, [translateErrorOutput(createErrorOutput(ex))]);
edit.replaceCellOutput(cell.index, [translateErrorOutput(createErrorOutput(ex))]);
});
}

Expand All @@ -89,9 +88,8 @@ export async function updateCellExecutionCount(
executionCount: number
): Promise<boolean> {
if (cell.metadata.executionOrder !== executionCount && executionCount) {
const cellIndex = editor.document.cells.indexOf(cell);
await editor.edit((edit) =>
edit.replaceCellMetadata(cellIndex, {
edit.replaceCellMetadata(cell.index, {
...cell.metadata,
executionOrder: executionCount
})
Expand All @@ -116,6 +114,5 @@ export async function updateCellOutput(editor: NotebookEditor, cell: NotebookCel
if (cell.outputs.length === newOutput.length && fastDeepEqual(cell.outputs, newOutput)) {
return;
}
const cellIndex = cell.notebook.cells.indexOf(cell);
await editor.edit((edit) => edit.replaceCellOutput(cellIndex, newOutput));
await editor.edit((edit) => edit.replaceCellOutput(cell.index, newOutput));
}
11 changes: 4 additions & 7 deletions src/client/datascience/notebook/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,15 @@ export function createIOutputFromCellOutputs(cellOutputs: CellOutput[]): nbforma
}

export async function clearCellForExecution(editor: NotebookEditor, cell: NotebookCell) {
const cellIndex = cell.notebook.cells.indexOf(cell);
await editor.edit((edit) => {
edit.replaceCellMetadata(cellIndex, {
edit.replaceCellMetadata(cell.index, {
...cell.metadata,
statusMessage: undefined,
executionOrder: undefined,
lastRunDuration: undefined,
runStartTime: undefined
});
edit.replaceCellOutput(cellIndex, []);
edit.replaceCellOutput(cell.index, []);
});
await updateCellExecutionTimes(editor, cell);
}
Expand All @@ -366,8 +365,6 @@ export async function updateCellExecutionTimes(
cell: NotebookCell,
times?: { startTime?: number; lastRunDuration?: number }
) {
const cellIndex = cell.notebook.cells.indexOf(cell);

if (!times || !times.lastRunDuration || !times.startTime) {
// Based on feedback from VSC, its best to clone these objects when updating them.
const cellMetadata = cloneDeep(cell.metadata);
Expand All @@ -382,7 +379,7 @@ export async function updateCellExecutionTimes(
}
if (updated) {
await editor.edit((edit) =>
edit.replaceCellMetadata(cellIndex, {
edit.replaceCellMetadata(cell.index, {
...cellMetadata
})
);
Expand All @@ -401,7 +398,7 @@ export async function updateCellExecutionTimes(
customMetadata.metadata.vscode.start_execution_time = startTimeISO;
const lastRunDuration = times.lastRunDuration ?? cell.metadata.lastRunDuration;
await editor.edit((edit) =>
edit.replaceCellMetadata(cellIndex, {
edit.replaceCellMetadata(cell.index, {
...cell.metadata,
custom: customMetadata,
lastRunDuration
Expand Down
18 changes: 7 additions & 11 deletions src/test/datascience/notebook/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export async function waitForExecutionCompletedSuccessfully(cell: NotebookCell)
await waitForCondition(
async () => assertHasExecutionCompletedSuccessfully(cell),
1_000,
`Cell ${cell.notebook.cells.indexOf(cell) + 1} did not complete successfully`
`Cell ${cell.index + 1} did not complete successfully`
);
}
export function assertExecutionOrderInVSCCell(cell: NotebookCell, executionOrder?: number) {
Expand All @@ -236,7 +236,7 @@ export async function waitForExecutionOrderInVSCCell(cell: NotebookCell, executi
await waitForCondition(
async () => assertExecutionOrderInVSCCell(cell, executionOrder),
1_000,
`Execution count not '${executionOrder}' for Cell ${cell.notebook.cells.indexOf(cell) + 1}`
`Execution count not '${executionOrder}' for Cell ${cell.index + 1}`
);
}
export async function waitForExecutionOrderInCell(cell: NotebookCell, executionOrder: number | undefined) {
Expand All @@ -248,7 +248,7 @@ export async function waitForExecutionOrderInCell(cell: NotebookCell, executionO
return cell.metadata.executionOrder === executionOrder;
},
15_000,
`Execution count not '${executionOrder}' for Cell ${cell.notebook.cells.indexOf(cell)}`
`Execution count not '${executionOrder}' for Cell ${cell.index}`
);
}
export function assertHasExecutionCompletedWithErrors(cell: NotebookCell) {
Expand All @@ -258,7 +258,7 @@ export function assertHasExecutionCompletedWithErrors(cell: NotebookCell) {
);
}
export function assertHasOutputInVSCell(cell: NotebookCell) {
assert.ok(cell.outputs.length, `No output in Cell ${cell.notebook.cells.indexOf(cell) + 1}`);
assert.ok(cell.outputs.length, `No output in Cell ${cell.index + 1}`);
}
export function assertHasOutputInICell(cell: ICell, model: INotebookModel) {
assert.ok((cell.data.outputs as nbformat.IOutput[]).length, `No output in ICell ${model.cells.indexOf(cell) + 1}`);
Expand All @@ -285,7 +285,7 @@ export async function waitForTextOutputInVSCode(
await waitForCondition(
async () => assertHasTextOutputInVSCode(cell, text, index, isExactMatch),
timeout,
`Output does not contain provided text '${text}' for Cell ${cell.notebook.cells.indexOf(cell) + 1}`
`Output does not contain provided text '${text}' for Cell ${cell.index + 1}`
);
}
export function assertNotHasTextOutputInVSCode(cell: NotebookCell, text: string, index: number, isExactMatch = true) {
Expand Down Expand Up @@ -313,7 +313,7 @@ export async function waitForVSCCellHasEmptyOutput(cell: NotebookCell) {
await waitForCondition(
async () => cell.outputs.length === 0,
1_000,
`Cell ${cell.notebook.cells.indexOf(cell) + 1} output did not get cleared`
`Cell ${cell.index + 1} output did not get cleared`
);
}
export async function waitForCellHasEmptyOutput(cell: ICell, model: INotebookModel) {
Expand All @@ -324,11 +324,7 @@ export async function waitForCellHasEmptyOutput(cell: ICell, model: INotebookMod
);
}
export async function waitForVSCCellIsRunning(cell: NotebookCell) {
await waitForCondition(
async () => assertVSCCellIsRunning(cell),
1_000,
`Cell ${cell.notebook.cells.indexOf(cell) + 1} did not start`
);
await waitForCondition(async () => assertVSCCellIsRunning(cell), 1_000, `Cell ${cell.index + 1} did not start`);
}
export function assertVSCCellIsNotRunning(cell: NotebookCell) {
assert.notEqual(cell.metadata.runState, vscodeNotebookEnums.NotebookCellRunState.Running);
Expand Down