mirror of
https://github.com/Microsoft/vscode
synced 2024-08-28 05:19:39 +00:00
new proposal for NotebookCellOutput, partial implementation
This commit is contained in:
parent
ea78dd7b9c
commit
b297be52ca
|
@ -417,6 +417,37 @@ suite('Notebook API tests', () => {
|
|||
await saveAllFilesAndCloseAll(resource);
|
||||
});
|
||||
|
||||
test('edit API (replaceOutput, USE NotebookCellOutput-type)', async function () {
|
||||
assertInitalState();
|
||||
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
|
||||
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
|
||||
|
||||
await vscode.notebook.activeNotebookEditor!.edit(editBuilder => {
|
||||
editBuilder.replaceCellOutput(0, [
|
||||
new vscode.NotebookCellOutput('application/foo', 'bar'),
|
||||
new vscode.NotebookCellOutput('application/json', { data: true }, { metadata: true }),
|
||||
]);
|
||||
});
|
||||
|
||||
const document = vscode.notebook.activeNotebookEditor?.document!;
|
||||
assert.strictEqual(document.isDirty, true);
|
||||
assert.strictEqual(document.cells.length, 1);
|
||||
assert.strictEqual(document.cells[0].outputs.length, 2);
|
||||
|
||||
// consuming is OLD api
|
||||
const [one, two] = document.cells[0].outputs;
|
||||
|
||||
assert.strictEqual(one.outputKind, vscode.CellOutputKind.Rich);
|
||||
assert.strictEqual((<vscode.CellDisplayOutput>one).data['application/foo'], 'bar');
|
||||
assert.strictEqual((<vscode.CellDisplayOutput>one).metadata, undefined);
|
||||
|
||||
assert.strictEqual(two.outputKind, vscode.CellOutputKind.Rich);
|
||||
assert.deepStrictEqual((<vscode.CellDisplayOutput>two).data['application/json'], { data: true });
|
||||
assert.deepStrictEqual((<vscode.CellDisplayOutput>two).metadata, { custom: { metadata: true } });
|
||||
|
||||
await saveAllFilesAndCloseAll(undefined);
|
||||
});
|
||||
|
||||
test('edit API (replaceOutput)', async function () {
|
||||
assertInitalState();
|
||||
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
|
||||
|
|
11
src/vs/vscode.proposed.d.ts
vendored
11
src/vs/vscode.proposed.d.ts
vendored
|
@ -1237,6 +1237,15 @@ declare module 'vscode' {
|
|||
|
||||
export type CellOutput = CellStreamOutput | CellErrorOutput | CellDisplayOutput;
|
||||
|
||||
export class NotebookCellOutput {
|
||||
|
||||
readonly mime: string;
|
||||
readonly value: unknown;
|
||||
readonly metadata?: Record<string, string | number | boolean>;
|
||||
|
||||
constructor(mime: string, value: unknown, metadata?: Record<string, string | number | boolean>);
|
||||
}
|
||||
|
||||
export enum NotebookCellRunState {
|
||||
Running = 1,
|
||||
Idle = 2,
|
||||
|
@ -1425,7 +1434,7 @@ declare module 'vscode' {
|
|||
export interface NotebookEditorEdit {
|
||||
replaceMetadata(value: NotebookDocumentMetadata): void;
|
||||
replaceCells(start: number, end: number, cells: NotebookCellData[]): void;
|
||||
replaceCellOutput(index: number, outputs: CellOutput[]): void;
|
||||
replaceCellOutput(index: number, outputs: (NotebookCellOutput | CellOutput)[]): void;
|
||||
replaceCellMetadata(index: number, metadata: NotebookCellMetadata): void;
|
||||
}
|
||||
|
||||
|
|
|
@ -1147,7 +1147,8 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
|
|||
NotebookCellRunState: extHostTypes.NotebookCellRunState,
|
||||
NotebookRunState: extHostTypes.NotebookRunState,
|
||||
NotebookCellStatusBarAlignment: extHostTypes.NotebookCellStatusBarAlignment,
|
||||
NotebookEditorRevealType: extHostTypes.NotebookEditorRevealType
|
||||
NotebookEditorRevealType: extHostTypes.NotebookEditorRevealType,
|
||||
NotebookCellOutput: extHostTypes.NotebookCellOutput,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import { readonly } from 'vs/base/common/errors';
|
|||
import { Emitter, Event } from 'vs/base/common/event';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { MainThreadNotebookShape } from 'vs/workbench/api/common/extHost.protocol';
|
||||
import { NotebookCellOutput } from 'vs/workbench/api/common/extHostTypeConverters';
|
||||
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
|
||||
import { addIdToOutput, CellEditType, ICellEditOperation, ICellReplaceEdit, INotebookEditData, notebookDocumentMetadataDefaults } from 'vs/workbench/contrib/notebook/common/notebookCommon';
|
||||
import * as vscode from 'vscode';
|
||||
|
@ -54,12 +55,18 @@ class NotebookEditorCellEditBuilder implements vscode.NotebookEditorEdit {
|
|||
});
|
||||
}
|
||||
|
||||
replaceCellOutput(index: number, outputs: vscode.CellOutput[]): void {
|
||||
replaceCellOutput(index: number, outputs: (vscode.NotebookCellOutput | vscode.CellOutput)[]): void {
|
||||
this._throwIfFinalized();
|
||||
this._collectedEdits.push({
|
||||
editType: CellEditType.Output,
|
||||
index,
|
||||
outputs: outputs.map(output => addIdToOutput(output))
|
||||
outputs: outputs.map(output => {
|
||||
if (extHostTypes.NotebookCellOutput.isNotebookCellOutput(output)) {
|
||||
return addIdToOutput(NotebookCellOutput.from(output));
|
||||
} else {
|
||||
return addIdToOutput(output);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ import { coalesce, isNonEmptyArray } from 'vs/base/common/arrays';
|
|||
import { RenderLineNumbersType } from 'vs/editor/common/config/editorOptions';
|
||||
import { CommandsConverter } from 'vs/workbench/api/common/extHostCommands';
|
||||
import { ExtHostNotebookController } from 'vs/workbench/api/common/extHostNotebook';
|
||||
import { INotebookDecorationRenderOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
|
||||
import { CellOutputKind, IDisplayOutput, INotebookDecorationRenderOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
|
||||
|
||||
export interface PositionLike {
|
||||
line: number;
|
||||
|
@ -1295,6 +1295,16 @@ export namespace LogLevel {
|
|||
}
|
||||
}
|
||||
|
||||
export namespace NotebookCellOutput {
|
||||
export function from(output: types.NotebookCellOutput): IDisplayOutput {
|
||||
return {
|
||||
outputKind: CellOutputKind.Rich,
|
||||
data: { [output.mime]: output.value },
|
||||
metadata: output.metadata && { custom: output.metadata }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export namespace NotebookExclusiveDocumentPattern {
|
||||
export function from(pattern: { include: vscode.GlobPattern | undefined, exclude: vscode.GlobPattern | undefined }): { include: string | types.RelativePattern | undefined, exclude: string | types.RelativePattern | undefined };
|
||||
export function from(pattern: vscode.GlobPattern): string | types.RelativePattern;
|
||||
|
|
|
@ -2765,6 +2765,19 @@ export enum ColorThemeKind {
|
|||
|
||||
//#region Notebook
|
||||
|
||||
export class NotebookCellOutput {
|
||||
|
||||
static isNotebookCellOutput(obj: unknown): obj is vscode.NotebookCellOutput {
|
||||
return obj instanceof NotebookCellOutput;
|
||||
}
|
||||
|
||||
constructor(
|
||||
readonly mime: string,
|
||||
readonly value: unknown, // JSON'able
|
||||
readonly metadata?: Record<string, string | number | boolean>
|
||||
) { }
|
||||
}
|
||||
|
||||
export enum CellKind {
|
||||
Markdown = 1,
|
||||
Code = 2
|
||||
|
|
Loading…
Reference in a new issue