Skip to content
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

Fix updating outputs of another cell using a NotebookCellExecution #151573

Merged
merged 1 commit into from
Jun 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,80 @@ const apiTestContentProvider: vscode.NotebookContentProvider = {

assert.strictEqual(executionWasCancelled, true);
});

test('appendOutput to different cell', async function () {
const notebook = await openRandomNotebookDocument();
const editor = await vscode.window.showNotebookDocument(notebook);
const cell0 = editor.notebook.cellAt(0);
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
const cell1 = editor.notebook.cellAt(1);

const nextCellKernel = new class extends Kernel {
constructor() {
super('nextCellKernel', 'Append to cell kernel');
}

override async _runCell(cell: vscode.NotebookCell) {
const task = this.controller.createNotebookCellExecution(cell);
task.start();
await task.appendOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output')
])], cell1);
await task.appendOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output 2')
])], cell1);
task.end(true);
}
};
testDisposables.push(nextCellKernel.controller);

await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async (event) => {
await assertKernel(nextCellKernel, notebook);
await vscode.commands.executeCommand('notebook.cell.execute');
await event;
assert.strictEqual(cell0.outputs.length, 0, 'should not change cell 0');
assert.strictEqual(cell1.outputs.length, 2, 'should update cell 1');
assert.strictEqual(cell1.outputs[0].items.length, 1);
assert.deepStrictEqual(new TextDecoder().decode(cell1.outputs[0].items[0].data), 'my output');
});
});

test('replaceOutput to different cell', async function () {
const notebook = await openRandomNotebookDocument();
const editor = await vscode.window.showNotebookDocument(notebook);
const cell0 = editor.notebook.cellAt(0);
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
const cell1 = editor.notebook.cellAt(1);

const nextCellKernel = new class extends Kernel {
constructor() {
super('nextCellKernel', 'Replace to cell kernel');
}

override async _runCell(cell: vscode.NotebookCell) {
const task = this.controller.createNotebookCellExecution(cell);
task.start();
await task.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output')
])], cell1);
await task.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text('my output 2')
])], cell1);
task.end(true);
}
};
testDisposables.push(nextCellKernel.controller);

await withEvent<vscode.NotebookDocumentChangeEvent>(vscode.workspace.onDidChangeNotebookDocument, async (event) => {
await assertKernel(nextCellKernel, notebook);
await vscode.commands.executeCommand('notebook.cell.execute');
await event;
assert.strictEqual(cell0.outputs.length, 0, 'should not change cell 0');
assert.strictEqual(cell1.outputs.length, 1, 'should update cell 1');
assert.strictEqual(cell1.outputs[0].items.length, 1);
assert.deepStrictEqual(new TextDecoder().decode(cell1.outputs[0].items[0].data), 'my output 2');
});
});
});

(vscode.env.uiKind === vscode.UIKind.Web ? suite.skip : suite)('statusbar', () => {
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/browser/mainThreadNotebookDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export namespace NotebookDto {
if (data.editType === CellExecutionUpdateType.Output) {
return {
editType: data.editType,
cellHandle: data.cellHandle,
append: data.append,
outputs: data.outputs.map(fromNotebookOutputDto)
};
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ export interface INotebookProxyKernelDto {

export interface ICellExecuteOutputEditDto {
editType: CellExecutionUpdateType.Output;
cellHandle: number;
append?: boolean;
outputs: NotebookOutputDto[];
}
Expand Down
13 changes: 13 additions & 0 deletions src/vs/workbench/api/common/extHostNotebookKernels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,17 @@ class NotebookCellExecutionTask extends Disposable {
}
}

private cellIndexToHandle(cellOrCellIndex: vscode.NotebookCell | undefined): number {
let cell: ExtHostCell | undefined = this._cell;
if (cellOrCellIndex) {
cell = this._cell.notebook.getCellFromApiCell(cellOrCellIndex);
}
if (!cell) {
throw new Error('INVALID cell');
}
return cell.handle;
}

private validateAndConvertOutputs(items: vscode.NotebookCellOutput[]): NotebookOutputDto[] {
return items.map(output => {
const newOutput = NotebookCellOutput.ensureUniqueMimeTypes(output.items, true);
Expand All @@ -456,10 +467,12 @@ class NotebookCellExecutionTask extends Disposable {
}

private async updateOutputs(outputs: vscode.NotebookCellOutput | vscode.NotebookCellOutput[], cell: vscode.NotebookCell | undefined, append: boolean): Promise<void> {
const handle = this.cellIndexToHandle(cell);
const outputDtos = this.validateAndConvertOutputs(asArray(outputs));
return this.updateSoon(
{
editType: CellExecutionUpdateType.Output,
cellHandle: handle,
append,
outputs: outputDtos
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1459,11 +1459,11 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
}
hasPendingChangeContentHeight = true;

DOM.scheduleAtNextAnimationFrame(() => {
this._localStore.add(DOM.scheduleAtNextAnimationFrame(() => {
hasPendingChangeContentHeight = false;
this._updateScrollHeight();
this._onDidChangeContentHeight.fire(this._list.getScrollHeight());
}, 100);
}, 100));
}));

this._localStore.add(this._list.onDidRemoveOutputs(outputs => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function updateToEdit(update: ICellExecuteUpdate, cellHandle: number): ICellEdit
if (update.editType === CellExecutionUpdateType.Output) {
return {
editType: CellEditType.Output,
handle: cellHandle,
handle: update.cellHandle,
append: update.append,
outputs: update.outputs,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum CellExecutionUpdateType {

export interface ICellExecuteOutputEdit {
editType: CellExecutionUpdateType.Output;
cellHandle: number;
append?: boolean;
outputs: IOutputDto[];
}
Expand Down