make NotebookCellData and NotebookData classes, rename NotebookCellData#cellKind to NotebookCellData#kind

This commit is contained in:
Johannes Rieken 2021-03-05 15:17:48 +01:00
parent 86df444212
commit 6117524374
No known key found for this signature in database
GPG key ID: 96634B5AF12F8798
8 changed files with 70 additions and 47 deletions

View file

@ -11,10 +11,10 @@ suite('Notebook Document', function () {
const contentProvider = new class implements vscode.NotebookContentProvider {
async openNotebook(uri: vscode.Uri, _openContext: vscode.NotebookDocumentOpenContext): Promise<vscode.NotebookData> {
return {
cells: [{ cellKind: vscode.NotebookCellKind.Code, source: uri.toString(), language: 'javascript', metadata: new vscode.NotebookCellMetadata(), outputs: [] }],
metadata: new vscode.NotebookDocumentMetadata()
};
return new vscode.NotebookData(
[new vscode.NotebookCellData(vscode.NotebookCellKind.Code, uri.toString(), 'javascript')],
new vscode.NotebookDocumentMetadata()
);
}
async resolveNotebook(_document: vscode.NotebookDocument, _webview: vscode.NotebookCommunication) {
//
@ -125,13 +125,13 @@ suite('Notebook Document', function () {
{
const edit = new vscode.WorkspaceEdit();
edit.replaceNotebookCells(document.uri, 0, 0, [{
cellKind: vscode.NotebookCellKind.Markdown,
kind: vscode.NotebookCellKind.Markdown,
language: 'markdown',
metadata: undefined,
outputs: [],
source: 'new_markdown'
}, {
cellKind: vscode.NotebookCellKind.Code,
kind: vscode.NotebookCellKind.Code,
language: 'fooLang',
metadata: undefined,
outputs: [],
@ -162,13 +162,13 @@ suite('Notebook Document', function () {
{
const edit = new vscode.WorkspaceEdit();
edit.replaceNotebookCells(document.uri, 0, 1, [{
cellKind: vscode.NotebookCellKind.Markdown,
kind: vscode.NotebookCellKind.Markdown,
language: 'markdown',
metadata: undefined,
outputs: [],
source: 'new2_markdown'
}, {
cellKind: vscode.NotebookCellKind.Code,
kind: vscode.NotebookCellKind.Code,
language: 'fooLang',
metadata: undefined,
outputs: [],
@ -198,13 +198,13 @@ suite('Notebook Document', function () {
const edit = new vscode.WorkspaceEdit();
edit.replaceNotebookCells(document.uri, 0, 0, [{
cellKind: vscode.NotebookCellKind.Markdown,
kind: vscode.NotebookCellKind.Markdown,
language: 'markdown',
metadata: undefined,
outputs: [],
source: 'new_markdown'
}, {
cellKind: vscode.NotebookCellKind.Code,
kind: vscode.NotebookCellKind.Code,
language: 'fooLang',
metadata: undefined,
outputs: [],
@ -306,13 +306,13 @@ suite('Notebook Document', function () {
const edit = new vscode.WorkspaceEdit();
edit.replaceNotebookCells(notebook.uri, 0, 0, [{
cellKind: vscode.NotebookCellKind.Markdown,
kind: vscode.NotebookCellKind.Markdown,
language: 'markdown',
metadata: undefined,
outputs: [],
source: 'new_markdown'
}, {
cellKind: vscode.NotebookCellKind.Code,
kind: vscode.NotebookCellKind.Code,
language: 'fooLang',
metadata: undefined,
outputs: [],

View file

@ -11,10 +11,11 @@ suite('Notebook Editor', function () {
const contentProvider = new class implements vscode.NotebookContentProvider {
async openNotebook(uri: vscode.Uri, _openContext: vscode.NotebookDocumentOpenContext): Promise<vscode.NotebookData> {
return {
cells: [{ cellKind: vscode.NotebookCellKind.Code, source: uri.toString(), language: 'javascript', metadata: new vscode.NotebookCellMetadata(), outputs: [] }],
metadata: new vscode.NotebookDocumentMetadata()
};
return new vscode.NotebookData(
[new vscode.NotebookCellData(vscode.NotebookCellKind.Code, uri.toString(), 'javascript')],
new vscode.NotebookDocumentMetadata()
);
}
async resolveNotebook(_document: vscode.NotebookDocument, _webview: vscode.NotebookCommunication) {
//

View file

@ -96,14 +96,14 @@ suite('Notebook API tests', function () {
{
source: 'test',
language: 'typescript',
cellKind: vscode.NotebookCellKind.Code,
kind: vscode.NotebookCellKind.Code,
outputs: [],
metadata: new vscode.NotebookCellMetadata().with({ custom: { testCellMetadata: 123 } })
},
{
source: 'test2',
language: 'typescript',
cellKind: vscode.NotebookCellKind.Code,
kind: vscode.NotebookCellKind.Code,
outputs: [
new vscode.NotebookCellOutput([
new vscode.NotebookCellOutputItem('text/plain', 'Hello World', { testOutputItemMetadata: true })
@ -488,7 +488,7 @@ suite('Notebook API tests', function () {
const cellMetadataChangeEvent = asPromise<vscode.NotebookCellMetadataChangeEvent>(vscode.notebook.onDidChangeCellMetadata);
const version = vscode.window.activeNotebookEditor!.document.version;
await vscode.window.activeNotebookEditor!.edit(editBuilder => {
editBuilder.replaceCells(1, 0, [{ cellKind: vscode.NotebookCellKind.Code, language: 'javascript', source: 'test 2', outputs: [], metadata: undefined }]);
editBuilder.replaceCells(1, 0, [{ kind: vscode.NotebookCellKind.Code, language: 'javascript', source: 'test 2', outputs: [], metadata: undefined }]);
editBuilder.replaceCellMetadata(0, new vscode.NotebookCellMetadata().with({ runnable: false }));
});
@ -506,7 +506,7 @@ suite('Notebook API tests', function () {
const cellMetadataChangeEvent = asPromise<vscode.NotebookCellMetadataChangeEvent>(vscode.notebook.onDidChangeCellMetadata);
const version = vscode.window.activeNotebookEditor!.document.version;
await vscode.window.activeNotebookEditor!.edit(editBuilder => {
editBuilder.replaceCells(1, 0, [{ cellKind: vscode.NotebookCellKind.Code, language: 'javascript', source: 'test 2', outputs: [], metadata: undefined }]);
editBuilder.replaceCells(1, 0, [{ kind: vscode.NotebookCellKind.Code, language: 'javascript', source: 'test 2', outputs: [], metadata: undefined }]);
editBuilder.replaceCellMetadata(0, new vscode.NotebookCellMetadata().with({ runnable: false }));
});
@ -1187,7 +1187,7 @@ suite('Notebook API tests', function () {
const cellsChangeEvent = asPromise<vscode.NotebookCellsChangeEvent>(vscode.notebook.onDidChangeNotebookCells);
await vscode.window.activeNotebookEditor!.edit(editBuilder => {
editBuilder.replaceCells(1, 0, [{ cellKind: vscode.NotebookCellKind.Code, language: 'javascript', source: 'test 2', outputs: [], metadata: undefined }]);
editBuilder.replaceCells(1, 0, [{ kind: vscode.NotebookCellKind.Code, language: 'javascript', source: 'test 2', outputs: [], metadata: undefined }]);
});
const cellChangeEventRet = await cellsChangeEvent;

View file

@ -28,14 +28,14 @@ export function activate(context: vscode.ExtensionContext): any {
{
source: 'code()',
language: 'typescript',
cellKind: vscode.NotebookCellKind.Code,
kind: vscode.NotebookCellKind.Code,
outputs: [],
metadata: new vscode.NotebookCellMetadata().with({ custom: { testCellMetadata: 123 } })
},
{
source: 'Markdown Cell',
language: 'markdown',
cellKind: vscode.NotebookCellKind.Markdown,
kind: vscode.NotebookCellKind.Markdown,
outputs: [],
metadata: new vscode.NotebookCellMetadata().with({ custom: { testCellMetadata: 123 } })
}

View file

@ -1352,21 +1352,22 @@ declare module 'vscode' {
readonly visibleRanges: ReadonlyArray<NotebookCellRange>;
}
// todo@API make class
// todo@API support ids https://github.com/jupyter/enhancement-proposals/blob/master/62-cell-id/cell-id.md
export interface NotebookCellData {
readonly cellKind: NotebookCellKind;
readonly source: string;
readonly language: string;
// todo@API maybe use a separate data type?
readonly outputs: NotebookCellOutput[];
readonly metadata: NotebookCellMetadata | undefined;
export class NotebookCellData {
kind: NotebookCellKind;
// todo@API better names: value? text?
source: string;
// todo@API how does language and MD relate?
language: string;
outputs?: NotebookCellOutput[];
metadata?: NotebookCellMetadata;
constructor(kind: NotebookCellKind, source: string, language: string, outputs?: NotebookCellOutput[], metadata?: NotebookCellMetadata)
}
// todo@API make class
export interface NotebookData {
readonly cells: NotebookCellData[];
readonly metadata: NotebookDocumentMetadata;
export class NotebookData {
cells: NotebookCellData[];
metadata?: NotebookDocumentMetadata;
constructor(cells: NotebookCellData[], metadata?: NotebookDocumentMetadata);
}

View file

@ -1230,6 +1230,8 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
NotebookCellRunState: extHostTypes.NotebookCellRunState,
NotebookDocumentMetadata: extHostTypes.NotebookDocumentMetadata,
NotebookCellMetadata: extHostTypes.NotebookCellMetadata,
NotebookCellData: extHostTypes.NotebookCellData,
NotebookData: extHostTypes.NotebookData,
NotebookRunState: extHostTypes.NotebookRunState,
NotebookCellStatusBarAlignment: extHostTypes.NotebookCellStatusBarAlignment,
NotebookEditorRevealType: extHostTypes.NotebookEditorRevealType,

View file

@ -1462,19 +1462,11 @@ export namespace NotebookCellData {
export function from(data: vscode.NotebookCellData): notebooks.ICellDto2 {
return {
cellKind: NotebookCellKind.from(data.cellKind),
cellKind: NotebookCellKind.from(data.kind),
language: data.language,
source: data.source,
metadata: data.metadata,
outputs: data.outputs.map(output => ({
outputId: output.id,
metadata: output.metadata,
outputs: (output.outputs || []).map(op => ({
mime: op.mime,
value: op.value,
metadata: op.metadata
}))
}))
outputs: data.outputs ? data.outputs.map(NotebookCellOutput.from) : []
};
}
}

View file

@ -3138,11 +3138,38 @@ export class NotebookDocumentMetadata {
runState,
trusted
);
}
}
export class NotebookCellData {
kind: NotebookCellKind;
source: string;
language: string;
outputs?: NotebookCellOutput[];
metadata?: NotebookCellMetadata;
constructor(kind: NotebookCellKind, source: string, language: string, outputs?: NotebookCellOutput[], metadata?: NotebookCellMetadata) {
this.kind = kind;
this.source = source;
this.language = language;
this.outputs = outputs ?? [];
this.metadata = metadata;
}
}
export class NotebookData {
cells: NotebookCellData[];
metadata?: NotebookDocumentMetadata;
constructor(cells: NotebookCellData[], metadata?: NotebookDocumentMetadata) {
this.cells = cells;
this.metadata = metadata;
}
}
export class NotebookCellOutputItem {
static isNotebookCellOutputItem(obj: unknown): obj is vscode.NotebookCellOutputItem {