fix #116598. broadcast output items change.

This commit is contained in:
rebornix 2021-02-16 14:57:40 -08:00
parent 7459443550
commit 1267767472
4 changed files with 78 additions and 6 deletions

View file

@ -1460,6 +1460,34 @@ suite('Notebook API tests', function () {
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
test('#116598, output items change event.', async function () {
assertInitalState();
const resource = await createRandomFile('', undefined, '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
const edit = new vscode.WorkspaceEdit();
edit.appendNotebookCellOutput(resource, 0, [new vscode.NotebookCellOutput([
new vscode.NotebookCellOutputItem('application/foo', 'bar'),
new vscode.NotebookCellOutputItem('application/json', { data: true }, { metadata: true }),
])]);
await vscode.workspace.applyEdit(edit);
assert.strictEqual(vscode.window.activeNotebookEditor!.document.cells[0].outputs.length, 1);
assert.strictEqual(vscode.window.activeNotebookEditor!.document.cells[0].outputs[0].outputs.length, 2);
const appendEdit = new vscode.WorkspaceEdit();
const newItem = new vscode.NotebookCellOutputItem('text/plain', '1');
appendEdit.appendNotebookCellOutputItems(
resource,
0,
vscode.window.activeNotebookEditor!.document.cells[0].outputs[0].id,
[newItem]
);
await vscode.workspace.applyEdit(appendEdit);
assert.strictEqual(vscode.window.activeNotebookEditor!.document.cells[0].outputs[0].outputs.length, 3);
assert.deepStrictEqual(vscode.window.activeNotebookEditor!.document.cells[0].outputs[0].outputs[2], newItem);
});
// });
// suite('webview', () => {

View file

@ -13,7 +13,7 @@ import { CellKind, INotebookDocumentPropertiesChangeData } from 'vs/workbench/ap
import { ExtHostDocumentsAndEditors, IExtHostModelAddedData } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
import * as extHostTypeConverters from 'vs/workbench/api/common/extHostTypeConverters';
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
import { IMainCellDto, IOutputDto, NotebookCellMetadata, NotebookCellsChangedEventDto, NotebookCellsChangeType, NotebookCellsSplice2, notebookDocumentMetadataDefaults } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { IMainCellDto, IOutputDto, IOutputItemDto, NotebookCellMetadata, NotebookCellsChangedEventDto, NotebookCellsChangeType, NotebookCellsSplice2, notebookDocumentMetadataDefaults } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import * as vscode from 'vscode';
class RawContentChangeEvent {
@ -100,6 +100,17 @@ export class ExtHostCell {
this._outputs = newOutputs;
}
setOutputItems(outputId: string, append: boolean, newOutputItems: IOutputItemDto[]) {
const output = this._outputs.find(op => op.outputId === outputId);
if (output) {
if (append) {
output.outputs = [...output.outputs, ...newOutputItems];
} else {
output.outputs = newOutputItems;
}
}
}
setMetadata(newMetadata: NotebookCellMetadata): void {
this._metadata = extHostTypeConverters.NotebookCellMetadata.to(newMetadata);
}
@ -211,6 +222,8 @@ export class ExtHostNotebookDocument extends Disposable {
this._moveCell(e.index, e.newIdx);
} else if (e.kind === NotebookCellsChangeType.Output) {
this._setCellOutputs(e.index, e.outputs);
} else if (e.kind === NotebookCellsChangeType.OutputItem) {
this._setCellOutputItems(e.index, e.outputId, e.append, e.outputItems);
} else if (e.kind === NotebookCellsChangeType.ChangeLanguage) {
this._changeCellLanguage(e.index, e.language);
} else if (e.kind === NotebookCellsChangeType.ChangeCellMetadata) {
@ -301,6 +314,12 @@ export class ExtHostNotebookDocument extends Disposable {
this._emitter.emitCellOutputsChange({ document: this.notebookDocument, cells: [cell.cell] });
}
private _setCellOutputItems(index: number, outputId: string, append: boolean, outputItems: IOutputItemDto[]): void {
const cell = this._cells[index];
cell.setOutputItems(outputId, append, outputItems);
this._emitter.emitCellOutputsChange({ document: this.notebookDocument, cells: [cell.cell] });
}
private _changeCellLanguage(index: number, language: string): void {
const cell = this._cells[index];
const event: vscode.NotebookCellLanguageChangeEvent = { document: this.notebookDocument, cell: cell.cell, language };

View file

@ -661,6 +661,14 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
const output = cell.outputs[outputIndex];
output.appendData(items);
this._eventEmitter.emit({
kind: NotebookCellsChangeType.OutputItem,
index: this._cells.indexOf(cell),
outputId: output.outputId,
outputItems: items,
append: true,
transient: this.transientOptions.transientOutputs
}, true);
}
private _replaceNotebookCellOutputItems(cellHandle: number, outputId: string, items: IOutputItemDto[]) {
@ -677,6 +685,14 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
const output = cell.outputs[outputIndex];
output.replaceData(items);
this._eventEmitter.emit({
kind: NotebookCellsChangeType.OutputItem,
index: this._cells.indexOf(cell),
outputId: output.outputId,
outputItems: items,
append: false,
transient: this.transientOptions.transientOutputs
}, true);
}
private _moveCellToIdx(index: number, length: number, newIdx: number, synchronous: boolean, pushedToUndoStack: boolean, beforeSelections: number[] | undefined, endSelections: number[] | undefined): boolean {

View file

@ -237,9 +237,10 @@ export enum NotebookCellsChangeType {
Initialize = 6,
ChangeCellMetadata = 7,
Output = 8,
ChangeCellContent = 9,
ChangeDocumentMetadata = 10,
Unknown = 11
OutputItem = 9,
ChangeCellContent = 10,
ChangeDocumentMetadata = 11,
Unknown = 12
}
export interface NotebookCellsInitializeEvent<T> {
@ -270,6 +271,14 @@ export interface NotebookOutputChangedEvent {
readonly outputs: IOutputDto[];
}
export interface NotebookOutputItemChangedEvent {
readonly kind: NotebookCellsChangeType.OutputItem;
readonly index: number;
readonly outputId: string;
readonly outputItems: IOutputItemDto[];
readonly append: boolean;
}
export interface NotebookCellsChangeLanguageEvent {
readonly kind: NotebookCellsChangeType.ChangeLanguage;
readonly index: number;
@ -291,14 +300,14 @@ export interface NotebookDocumentUnknownChangeEvent {
readonly kind: NotebookCellsChangeType.Unknown;
}
export type NotebookRawContentEventDto = NotebookCellsInitializeEvent<IMainCellDto> | NotebookDocumentChangeMetadataEvent | NotebookCellContentChangeEvent | NotebookCellsModelChangedEvent<IMainCellDto> | NotebookCellsModelMoveEvent<IMainCellDto> | NotebookOutputChangedEvent | NotebookCellsChangeLanguageEvent | NotebookCellsChangeMetadataEvent | NotebookDocumentUnknownChangeEvent;
export type NotebookRawContentEventDto = NotebookCellsInitializeEvent<IMainCellDto> | NotebookDocumentChangeMetadataEvent | NotebookCellContentChangeEvent | NotebookCellsModelChangedEvent<IMainCellDto> | NotebookCellsModelMoveEvent<IMainCellDto> | NotebookOutputChangedEvent | NotebookOutputItemChangedEvent | NotebookCellsChangeLanguageEvent | NotebookCellsChangeMetadataEvent | NotebookDocumentUnknownChangeEvent;
export type NotebookCellsChangedEventDto = {
readonly rawEvents: NotebookRawContentEventDto[];
readonly versionId: number;
};
export type NotebookRawContentEvent = (NotebookCellsInitializeEvent<ICell> | NotebookDocumentChangeMetadataEvent | NotebookCellContentChangeEvent | NotebookCellsModelChangedEvent<ICell> | NotebookCellsModelMoveEvent<ICell> | NotebookOutputChangedEvent | NotebookCellsChangeLanguageEvent | NotebookCellsChangeMetadataEvent | NotebookDocumentUnknownChangeEvent) & { transient: boolean; };
export type NotebookRawContentEvent = (NotebookCellsInitializeEvent<ICell> | NotebookDocumentChangeMetadataEvent | NotebookCellContentChangeEvent | NotebookCellsModelChangedEvent<ICell> | NotebookCellsModelMoveEvent<ICell> | NotebookOutputChangedEvent | NotebookOutputItemChangedEvent | NotebookCellsChangeLanguageEvent | NotebookCellsChangeMetadataEvent | NotebookDocumentUnknownChangeEvent) & { transient: boolean; };
export type NotebookTextModelChangedEvent = {
readonly rawEvents: NotebookRawContentEvent[];
readonly versionId: number;