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

Update output items instead of output replacement if possible #157850

Merged
merged 1 commit into from Aug 11, 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
42 changes: 30 additions & 12 deletions src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts
Expand Up @@ -418,12 +418,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
index: i,
metadata: cells[i].metadata ?? {}
},
{
editType: CellEditType.Output,
index: i,
outputs: cells[i].outputs,
append: false
}
...this._computeOutputEdit(i, model.cells[i].outputs, cells[i].outputs)
);
}
}
Expand Down Expand Up @@ -451,19 +446,42 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
index: model.cells.length - i,
metadata: cells[cells.length - i].metadata ?? {}
},
{
editType: CellEditType.Output,
index: model.cells.length - i,
outputs: cells[cells.length - i].outputs,
append: false
}
...this._computeOutputEdit(model.cells.length - i, model.cells[model.cells.length - i].outputs, cells[cells.length - i].outputs)
);
}
}

return edits;
}

private static _computeOutputEdit(index: number, a: ICellOutput[], b: IOutputDto[]): ICellEditOperation[] {
if (a.length !== b.length) {
return [
{
editType: CellEditType.Output,
index: index,
outputs: b,
append: false
}
];
}

if (a.length === 0) {
// no output
return [];
}

// same length
return b.map((output, i) => {
return {
editType: CellEditType.OutputItems,
outputId: a[i].outputId,
items: output.outputs,
append: false
};
});
}

private static _commonPrefix(a: readonly NotebookCellTextModel[], aLen: number, aDelta: number, b: ICellDto2[], bLen: number, bDelta: number): number {
const maxResult = Math.min(aLen, bLen);
let result = 0;
Expand Down
Expand Up @@ -772,8 +772,7 @@ suite('NotebookTextModel', () => {
]);

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: {} },
{ editType: CellEditType.Output, index: 0, outputs: [], append: false }
{ editType: CellEditType.Metadata, index: 0, metadata: {} }
]);
});
});
Expand Down Expand Up @@ -808,7 +807,6 @@ suite('NotebookTextModel', () => {

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: {} },
{ editType: CellEditType.Output, index: 0, outputs: [], append: false },
{ editType: CellEditType.Replace, index: 1, count: 1, cells: cells.slice(1) },
]);
});
Expand All @@ -828,7 +826,6 @@ suite('NotebookTextModel', () => {
assert.deepStrictEqual(edits, [
{ editType: CellEditType.Replace, index: 0, count: 1, cells: cells.slice(0, 1) },
{ editType: CellEditType.Metadata, index: 1, metadata: {} },
{ editType: CellEditType.Output, index: 1, outputs: [], append: false },
]);
});
});
Expand All @@ -849,10 +846,8 @@ suite('NotebookTextModel', () => {

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: {} },
{ editType: CellEditType.Output, index: 0, outputs: [], append: false },
{ editType: CellEditType.Replace, index: 1, count: 1, cells: cells.slice(1, 2) },
{ editType: CellEditType.Metadata, index: 2, metadata: {} },
{ editType: CellEditType.Output, index: 2, outputs: [], append: false },
]);
});
});
Expand All @@ -871,9 +866,7 @@ suite('NotebookTextModel', () => {

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: { name: 'foo' } },
{ editType: CellEditType.Output, index: 0, outputs: [], append: false },
{ editType: CellEditType.Metadata, index: 1, metadata: {} },
{ editType: CellEditType.Output, index: 1, outputs: [], append: false },
]);
});
});
Expand All @@ -893,7 +886,6 @@ suite('NotebookTextModel', () => {
assert.deepStrictEqual(edits, [
{ editType: CellEditType.Replace, index: 0, count: 1, cells: cells.slice(0, 1) },
{ editType: CellEditType.Metadata, index: 1, metadata: {} },
{ editType: CellEditType.Output, index: 1, outputs: [], append: false },
]);
});
});
Expand All @@ -912,7 +904,6 @@ suite('NotebookTextModel', () => {

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: {} },
{ editType: CellEditType.Output, index: 0, outputs: [], append: false },
{ editType: CellEditType.Replace, index: 1, count: 1, cells: cells.slice(1) },
]);
});
Expand All @@ -932,7 +923,6 @@ suite('NotebookTextModel', () => {

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: { name: 'foo' } },
{ editType: CellEditType.Output, index: 0, outputs: [], append: false },
{ editType: CellEditType.Replace, index: 1, count: 1, cells: cells.slice(1) }
]);
});
Expand All @@ -953,7 +943,6 @@ suite('NotebookTextModel', () => {
assert.deepStrictEqual(edits, [
{ editType: CellEditType.Replace, index: 0, count: 1, cells: cells.slice(0, 1) },
{ editType: CellEditType.Metadata, index: 1, metadata: {} },
{ editType: CellEditType.Output, index: 1, outputs: [], append: false },
]);
});
});
Expand All @@ -973,10 +962,8 @@ suite('NotebookTextModel', () => {

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: {} },
{ editType: CellEditType.Output, index: 0, outputs: [], append: false },
{ editType: CellEditType.Replace, index: 1, count: 0, cells: cells.slice(1, 2) },
{ editType: CellEditType.Metadata, index: 1, metadata: { foo: 'bar' } },
{ editType: CellEditType.Output, index: 1, outputs: [], append: false },
]);

model.applyEdits(edits, true, undefined, () => undefined, undefined, true);
Expand All @@ -986,4 +973,74 @@ suite('NotebookTextModel', () => {
assert.deepStrictEqual(model.cells[2].metadata, { foo: 'bar' });
});
});

test('computeEdits output changed', async function () {
await withTestNotebook([
['var a = 1;', 'javascript', CellKind.Code, [], {}],
['var b = 1;', 'javascript', CellKind.Code, [], {}]
], (editor) => {
const model = editor.textModel;
const cells = [
{
source: 'var a = 1;', language: 'javascript', cellKind: CellKind.Code, mime: undefined, outputs: [{
outputId: 'someId',
outputs: [{ mime: Mimes.markdown, data: valueBytesFromString('_World_') }]
}], metadata: undefined,
},
{ source: 'var b = 1;', language: 'javascript', cellKind: CellKind.Code, mime: undefined, outputs: [], metadata: { foo: 'bar' } }
];
const edits = NotebookTextModel.computeEdits(model, cells);

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: {} },
{
editType: CellEditType.Output, index: 0, outputs: [{
outputId: 'someId',
outputs: [{ mime: Mimes.markdown, data: valueBytesFromString('_World_') }]
}], append: false
},
{ editType: CellEditType.Metadata, index: 1, metadata: { foo: 'bar' } },
]);

model.applyEdits(edits, true, undefined, () => undefined, undefined, true);
assert.equal(model.cells.length, 2);
assert.strictEqual(model.cells[0].outputs.length, 1);
assert.equal(model.cells[0].outputs[0].outputId, 'someId');
assert.equal(model.cells[0].outputs[0].outputs[0].data.toString(), '_World_');
});
});

test('computeEdits output items changed', async function () {
await withTestNotebook([
['var a = 1;', 'javascript', CellKind.Code, [{
outputId: 'someId',
outputs: [{ mime: Mimes.markdown, data: valueBytesFromString('_Hello_') }]
}], {}],
['var b = 1;', 'javascript', CellKind.Code, [], {}]
], (editor) => {
const model = editor.textModel;
const cells = [
{
source: 'var a = 1;', language: 'javascript', cellKind: CellKind.Code, mime: undefined, outputs: [{
outputId: 'someId',
outputs: [{ mime: Mimes.markdown, data: valueBytesFromString('_World_') }]
}], metadata: undefined,
},
{ source: 'var b = 1;', language: 'javascript', cellKind: CellKind.Code, mime: undefined, outputs: [], metadata: { foo: 'bar' } }
];
const edits = NotebookTextModel.computeEdits(model, cells);

assert.deepStrictEqual(edits, [
{ editType: CellEditType.Metadata, index: 0, metadata: {} },
{ editType: CellEditType.OutputItems, outputId: 'someId', items: [{ mime: Mimes.markdown, data: valueBytesFromString('_World_') }], append: false },
{ editType: CellEditType.Metadata, index: 1, metadata: { foo: 'bar' } },
]);

model.applyEdits(edits, true, undefined, () => undefined, undefined, true);
assert.equal(model.cells.length, 2);
assert.strictEqual(model.cells[0].outputs.length, 1);
assert.equal(model.cells[0].outputs[0].outputId, 'someId');
assert.equal(model.cells[0].outputs[0].outputs[0].data.toString(), '_World_');
});
});
});