Skip to content

Commit b297be5

Browse files
committed
new proposal for NotebookCellOutput, partial implementation
1 parent ea78dd7 commit b297be5

6 files changed

Lines changed: 76 additions & 5 deletions

File tree

extensions/vscode-notebook-tests/src/notebook.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,37 @@ suite('Notebook API tests', () => {
417417
await saveAllFilesAndCloseAll(resource);
418418
});
419419

420+
test('edit API (replaceOutput, USE NotebookCellOutput-type)', async function () {
421+
assertInitalState();
422+
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
423+
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
424+
425+
await vscode.notebook.activeNotebookEditor!.edit(editBuilder => {
426+
editBuilder.replaceCellOutput(0, [
427+
new vscode.NotebookCellOutput('application/foo', 'bar'),
428+
new vscode.NotebookCellOutput('application/json', { data: true }, { metadata: true }),
429+
]);
430+
});
431+
432+
const document = vscode.notebook.activeNotebookEditor?.document!;
433+
assert.strictEqual(document.isDirty, true);
434+
assert.strictEqual(document.cells.length, 1);
435+
assert.strictEqual(document.cells[0].outputs.length, 2);
436+
437+
// consuming is OLD api
438+
const [one, two] = document.cells[0].outputs;
439+
440+
assert.strictEqual(one.outputKind, vscode.CellOutputKind.Rich);
441+
assert.strictEqual((<vscode.CellDisplayOutput>one).data['application/foo'], 'bar');
442+
assert.strictEqual((<vscode.CellDisplayOutput>one).metadata, undefined);
443+
444+
assert.strictEqual(two.outputKind, vscode.CellOutputKind.Rich);
445+
assert.deepStrictEqual((<vscode.CellDisplayOutput>two).data['application/json'], { data: true });
446+
assert.deepStrictEqual((<vscode.CellDisplayOutput>two).metadata, { custom: { metadata: true } });
447+
448+
await saveAllFilesAndCloseAll(undefined);
449+
});
450+
420451
test('edit API (replaceOutput)', async function () {
421452
assertInitalState();
422453
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');

src/vs/vscode.proposed.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,15 @@ declare module 'vscode' {
12371237

12381238
export type CellOutput = CellStreamOutput | CellErrorOutput | CellDisplayOutput;
12391239

1240+
export class NotebookCellOutput {
1241+
1242+
readonly mime: string;
1243+
readonly value: unknown;
1244+
readonly metadata?: Record<string, string | number | boolean>;
1245+
1246+
constructor(mime: string, value: unknown, metadata?: Record<string, string | number | boolean>);
1247+
}
1248+
12401249
export enum NotebookCellRunState {
12411250
Running = 1,
12421251
Idle = 2,
@@ -1425,7 +1434,7 @@ declare module 'vscode' {
14251434
export interface NotebookEditorEdit {
14261435
replaceMetadata(value: NotebookDocumentMetadata): void;
14271436
replaceCells(start: number, end: number, cells: NotebookCellData[]): void;
1428-
replaceCellOutput(index: number, outputs: CellOutput[]): void;
1437+
replaceCellOutput(index: number, outputs: (NotebookCellOutput | CellOutput)[]): void;
14291438
replaceCellMetadata(index: number, metadata: NotebookCellMetadata): void;
14301439
}
14311440

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,8 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
11471147
NotebookCellRunState: extHostTypes.NotebookCellRunState,
11481148
NotebookRunState: extHostTypes.NotebookRunState,
11491149
NotebookCellStatusBarAlignment: extHostTypes.NotebookCellStatusBarAlignment,
1150-
NotebookEditorRevealType: extHostTypes.NotebookEditorRevealType
1150+
NotebookEditorRevealType: extHostTypes.NotebookEditorRevealType,
1151+
NotebookCellOutput: extHostTypes.NotebookCellOutput,
11511152
};
11521153
};
11531154
}

src/vs/workbench/api/common/extHostNotebookEditor.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { readonly } from 'vs/base/common/errors';
77
import { Emitter, Event } from 'vs/base/common/event';
88
import { Disposable } from 'vs/base/common/lifecycle';
99
import { MainThreadNotebookShape } from 'vs/workbench/api/common/extHost.protocol';
10+
import { NotebookCellOutput } from 'vs/workbench/api/common/extHostTypeConverters';
1011
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
1112
import { addIdToOutput, CellEditType, ICellEditOperation, ICellReplaceEdit, INotebookEditData, notebookDocumentMetadataDefaults } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1213
import * as vscode from 'vscode';
@@ -54,12 +55,18 @@ class NotebookEditorCellEditBuilder implements vscode.NotebookEditorEdit {
5455
});
5556
}
5657

57-
replaceCellOutput(index: number, outputs: vscode.CellOutput[]): void {
58+
replaceCellOutput(index: number, outputs: (vscode.NotebookCellOutput | vscode.CellOutput)[]): void {
5859
this._throwIfFinalized();
5960
this._collectedEdits.push({
6061
editType: CellEditType.Output,
6162
index,
62-
outputs: outputs.map(output => addIdToOutput(output))
63+
outputs: outputs.map(output => {
64+
if (extHostTypes.NotebookCellOutput.isNotebookCellOutput(output)) {
65+
return addIdToOutput(NotebookCellOutput.from(output));
66+
} else {
67+
return addIdToOutput(output);
68+
}
69+
})
6370
});
6471
}
6572

src/vs/workbench/api/common/extHostTypeConverters.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { coalesce, isNonEmptyArray } from 'vs/base/common/arrays';
3232
import { RenderLineNumbersType } from 'vs/editor/common/config/editorOptions';
3333
import { CommandsConverter } from 'vs/workbench/api/common/extHostCommands';
3434
import { ExtHostNotebookController } from 'vs/workbench/api/common/extHostNotebook';
35-
import { INotebookDecorationRenderOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
35+
import { CellOutputKind, IDisplayOutput, INotebookDecorationRenderOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
3636

3737
export interface PositionLike {
3838
line: number;
@@ -1295,6 +1295,16 @@ export namespace LogLevel {
12951295
}
12961296
}
12971297

1298+
export namespace NotebookCellOutput {
1299+
export function from(output: types.NotebookCellOutput): IDisplayOutput {
1300+
return {
1301+
outputKind: CellOutputKind.Rich,
1302+
data: { [output.mime]: output.value },
1303+
metadata: output.metadata && { custom: output.metadata }
1304+
};
1305+
}
1306+
}
1307+
12981308
export namespace NotebookExclusiveDocumentPattern {
12991309
export function from(pattern: { include: vscode.GlobPattern | undefined, exclude: vscode.GlobPattern | undefined }): { include: string | types.RelativePattern | undefined, exclude: string | types.RelativePattern | undefined };
13001310
export function from(pattern: vscode.GlobPattern): string | types.RelativePattern;

src/vs/workbench/api/common/extHostTypes.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,6 +2765,19 @@ export enum ColorThemeKind {
27652765

27662766
//#region Notebook
27672767

2768+
export class NotebookCellOutput {
2769+
2770+
static isNotebookCellOutput(obj: unknown): obj is vscode.NotebookCellOutput {
2771+
return obj instanceof NotebookCellOutput;
2772+
}
2773+
2774+
constructor(
2775+
readonly mime: string,
2776+
readonly value: unknown, // JSON'able
2777+
readonly metadata?: Record<string, string | number | boolean>
2778+
) { }
2779+
}
2780+
27682781
export enum CellKind {
27692782
Markdown = 1,
27702783
Code = 2

0 commit comments

Comments
 (0)