change IOutputItemDto to use real bytes, add many dedicated dto-types for transporting output (which cannot be bytes), remove metadata2 from renderer

This commit is contained in:
Johannes Rieken
2021-06-10 11:18:46 +02:00
parent 1d7b2d4b48
commit d0cc52143b
28 changed files with 470 additions and 315 deletions

View File

@@ -27,6 +27,7 @@ import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/wo
import { revive } from 'vs/base/common/marshalling';
import { ResourceNotebookCellEdit } from 'vs/workbench/contrib/bulkEdit/browser/bulkCellEdits';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
export function reviveWorkspaceEditDto2(data: IWorkspaceEditDto | undefined): ResourceEdit[] {
if (!data?.edits) {
@@ -40,7 +41,7 @@ export function reviveWorkspaceEditDto2(data: IWorkspaceEditDto | undefined): Re
} else if (edit._type === WorkspaceEditType.Text) {
result.push(new ResourceTextEdit(edit.resource, edit.edit, edit.modelVersionId, edit.metadata));
} else if (edit._type === WorkspaceEditType.Cell) {
result.push(new ResourceNotebookCellEdit(edit.resource, edit.edit, edit.notebookVersionId, edit.metadata));
result.push(new ResourceNotebookCellEdit(edit.resource, NotebookDto.fromCellEditOperationDto(edit.edit), edit.notebookVersionId, edit.metadata));
}
}
return result;

View File

@@ -8,9 +8,10 @@ import { CancellationToken } from 'vs/base/common/cancellation';
import { Emitter } from 'vs/base/common/event';
import { DisposableStore, dispose, IDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { INotebookCellStatusBarService } from 'vs/workbench/contrib/notebook/common/notebookCellStatusBarService';
import { INotebookCellStatusBarItemProvider, INotebookContributionData, NotebookDataDto, TransientCellMetadata, TransientDocumentMetadata, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookCellStatusBarItemProvider, INotebookContributionData, NotebookData as NotebookData, TransientCellMetadata, TransientDocumentMetadata, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookContentProvider, INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { ExtHostContext, ExtHostNotebookShape, IExtHostContext, MainContext, MainThreadNotebookShape, NotebookExtensionDescription } from '../common/extHost.protocol';
@@ -56,7 +57,7 @@ export class MainThreadNotebooks implements MainThreadNotebookShape {
open: async (uri: URI, backupId: string | undefined, untitledDocumentData: VSBuffer | undefined, token: CancellationToken) => {
const data = await this._proxy.$openNotebook(viewType, uri, backupId, untitledDocumentData, token);
return {
data,
data: NotebookDto.fromNotebookDataDto(data),
transientOptions: contentOptions
};
},
@@ -100,14 +101,16 @@ export class MainThreadNotebooks implements MainThreadNotebookShape {
}
}
$registerNotebookSerializer(handle: number, extension: NotebookExtensionDescription, viewType: string, options: TransientOptions, data: INotebookContributionData | undefined): void {
const registration = this._notebookService.registerNotebookSerializer(viewType, extension, {
options,
dataToNotebook: (data: VSBuffer): Promise<NotebookDataDto> => {
return this._proxy.$dataToNotebook(handle, data, CancellationToken.None);
dataToNotebook: async (data: VSBuffer): Promise<NotebookData> => {
const dto = await this._proxy.$dataToNotebook(handle, data, CancellationToken.None);
return NotebookDto.fromNotebookDataDto(dto);
},
notebookToData: (data: NotebookDataDto): Promise<VSBuffer> => {
return this._proxy.$notebookToData(handle, data, CancellationToken.None);
notebookToData: (data: NotebookData): Promise<VSBuffer> => {
return this._proxy.$notebookToData(handle, NotebookDto.toNotebookDataDto(data), CancellationToken.None);
}
});
const disposables = new DisposableStore();

View File

@@ -9,14 +9,15 @@ import { URI, UriComponents } from 'vs/base/common/uri';
import { BoundModelReferenceCollection } from 'vs/workbench/api/browser/mainThreadDocuments';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { IMainCellDto, NotebookCellsChangeType, NotebookDataDto } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { NotebookCellsChangeType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookEditorModelResolverService } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverService';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
import { ExtHostContext, ExtHostNotebookDocumentsShape, IExtHostContext, MainThreadNotebookDocumentsShape } from '../common/extHost.protocol';
import { ExtHostContext, ExtHostNotebookDocumentsShape, IExtHostContext, MainThreadNotebookDocumentsShape, NotebookCellDto, NotebookCellsChangedEventDto, NotebookDataDto } from '../common/extHost.protocol';
import { MainThreadNotebooksAndEditors } from 'vs/workbench/api/browser/mainThreadNotebookDocumentsAndEditors';
import { Schemas } from 'vs/base/common/network';
import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
export class MainThreadNotebookDocuments implements MainThreadNotebookDocumentsShape {
@@ -55,36 +56,59 @@ export class MainThreadNotebookDocuments implements MainThreadNotebookDocumentsS
for (const textModel of notebooks) {
const disposableStore = new DisposableStore();
disposableStore.add(textModel.onDidChangeContent(event => {
const dto = event.rawEvents.map(e => {
const data =
e.kind === NotebookCellsChangeType.ModelChange || e.kind === NotebookCellsChangeType.Initialize
? {
kind: e.kind,
versionId: event.versionId,
changes: e.changes.map(diff => [diff[0], diff[1], diff[2].map(cell => MainThreadNotebookDocuments._cellToDto(cell as NotebookCellTextModel))] as [number, number, IMainCellDto[]])
}
: (
e.kind === NotebookCellsChangeType.Move
? {
kind: e.kind,
index: e.index,
length: e.length,
newIdx: e.newIdx,
versionId: event.versionId,
cells: e.cells.map(cell => MainThreadNotebookDocuments._cellToDto(cell as NotebookCellTextModel))
}
: e
);
return data;
});
const eventDto: NotebookCellsChangedEventDto = {
versionId: event.versionId,
rawEvents: []
};
for (const e of event.rawEvents) {
switch (e.kind) {
case NotebookCellsChangeType.ModelChange:
eventDto.rawEvents.push({
kind: e.kind,
changes: e.changes.map(diff => [diff[0], diff[1], diff[2].map(cell => NotebookDto.toNotebookCellDto(cell as NotebookCellTextModel))] as [number, number, NotebookCellDto[]])
});
break;
case NotebookCellsChangeType.Move:
eventDto.rawEvents.push({
kind: e.kind,
index: e.index,
length: e.length,
newIdx: e.newIdx,
});
break;
case NotebookCellsChangeType.Output:
eventDto.rawEvents.push({
kind: e.kind,
index: e.index,
outputs: e.outputs.map(NotebookDto.toNotebookOutputDto)
});
break;
case NotebookCellsChangeType.OutputItem:
eventDto.rawEvents.push({
kind: e.kind,
index: e.index,
outputId: e.outputId,
outputItems: e.outputItems.map(NotebookDto.toNotebookOutputItemDto),
append: e.append
});
break;
case NotebookCellsChangeType.ChangeLanguage:
case NotebookCellsChangeType.ChangeCellMetadata:
case NotebookCellsChangeType.ChangeCellInternalMetadata:
eventDto.rawEvents.push(e);
break;
}
}
// using the model resolver service to know if the model is dirty or not.
// assuming this is the first listener it can mean that at first the model
// is marked as dirty and that another event is fired
this._proxy.$acceptModelChanged(
textModel.uri,
{ rawEvents: dto, versionId: event.versionId },
eventDto,
this._notebookEditorModelResolverService.isDirty(textModel.uri)
);
@@ -105,19 +129,6 @@ export class MainThreadNotebookDocuments implements MainThreadNotebookDocumentsS
}
}
private static _cellToDto(cell: NotebookCellTextModel): IMainCellDto {
return {
handle: cell.handle,
uri: cell.uri,
source: cell.textBuffer.getLinesContent(),
eol: cell.textBuffer.getEOL(),
language: cell.language,
cellKind: cell.cellKind,
outputs: cell.outputs,
metadata: cell.metadata,
internalMetadata: cell.internalMetadata,
};
}
async $tryCreateNotebook(options: { viewType: string, content?: NotebookDataDto }): Promise<UriComponents> {
@@ -150,11 +161,8 @@ export class MainThreadNotebookDocuments implements MainThreadNotebookDocumentsS
// apply content changes... slightly HACKY -> this triggers a change event
if (options.content) {
ref.object.notebook.reset(
options.content.cells,
options.content.metadata,
ref.object.notebook.transientOptions
);
const data = NotebookDto.fromNotebookDataDto(options.content);
ref.object.notebook.reset(data.cells, data.metadata, ref.object.notebook.transientOptions);
}
return uri;
}

View File

@@ -9,6 +9,7 @@ import { combinedDisposable, DisposableStore, IDisposable } from 'vs/base/common
import { URI } from 'vs/base/common/uri';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { MainThreadNotebookDocuments } from 'vs/workbench/api/browser/mainThreadNotebookDocuments';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
import { MainThreadNotebookEditors } from 'vs/workbench/api/browser/mainThreadNotebookEditors';
import { extHostCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { editorGroupToViewColumn } from 'vs/workbench/common/editor';
@@ -226,17 +227,7 @@ export class MainThreadNotebooksAndEditors {
uri: e.uri,
metadata: e.metadata,
versionId: e.versionId,
cells: e.cells.map(cell => ({
handle: cell.handle,
uri: cell.uri,
source: cell.textBuffer.getLinesContent(),
eol: cell.textBuffer.getEOL(),
language: cell.language,
cellKind: cell.cellKind,
outputs: cell.outputs,
metadata: cell.metadata,
internalMetadata: cell.internalMetadata,
}))
cells: e.cells.map(NotebookDto.toNotebookCellDto)
};
}

View File

@@ -0,0 +1,122 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as extHostProtocol from 'vs/workbench/api/common/extHost.protocol';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import * as notebookCommon from 'vs/workbench/contrib/notebook/common/notebookCommon';
export namespace NotebookDto {
export function toNotebookOutputItemDto(item: notebookCommon.IOutputItemDto): extHostProtocol.NotebookOutputItemDto {
return {
mime: item.mime,
valueBytes: Array.from(item.data)
};
}
export function toNotebookOutputDto(output: notebookCommon.IOutputDto): extHostProtocol.NotebookOutputDto {
return {
outputId: output.outputId,
metadata: output.metadata,
items: output.outputs.map(toNotebookOutputItemDto)
};
}
export function toNotebookCellDataDto(cell: notebookCommon.ICellDto2): extHostProtocol.NotebookCellDataDto {
return {
cellKind: cell.cellKind,
language: cell.language,
source: cell.source,
outputs: cell.outputs.map(toNotebookOutputDto)
};
}
export function toNotebookDataDto(data: notebookCommon.NotebookData): extHostProtocol.NotebookDataDto {
return {
metadata: data.metadata,
cells: data.cells.map(toNotebookCellDataDto)
};
}
export function fromNotebookOutputItemDto(item: extHostProtocol.NotebookOutputItemDto): notebookCommon.IOutputItemDto {
return {
mime: item.mime,
data: new Uint8Array(item.valueBytes)
};
}
export function fromNotebookOutputDto(output: extHostProtocol.NotebookOutputDto): notebookCommon.IOutputDto {
return {
outputId: output.outputId,
metadata: output.metadata,
outputs: output.items.map(fromNotebookOutputItemDto)
};
}
export function fromNotebookCellDataDto(cell: extHostProtocol.NotebookCellDataDto): notebookCommon.ICellDto2 {
return {
cellKind: cell.cellKind,
language: cell.language,
source: cell.source,
outputs: cell.outputs.map(fromNotebookOutputDto),
metadata: cell.metadata,
internalMetadata: cell.internalMetadata
};
}
export function fromNotebookDataDto(data: extHostProtocol.NotebookDataDto): notebookCommon.NotebookData {
return {
metadata: data.metadata,
cells: data.cells.map(fromNotebookCellDataDto)
};
}
export function toNotebookCellDto(cell: NotebookCellTextModel): extHostProtocol.NotebookCellDto {
return {
handle: cell.handle,
uri: cell.uri,
source: cell.textBuffer.getLinesContent(),
eol: cell.textBuffer.getEOL(),
language: cell.language,
cellKind: cell.cellKind,
outputs: cell.outputs.map(toNotebookOutputDto),
metadata: cell.metadata,
internalMetadata: cell.internalMetadata,
};
}
export function fromCellExecuteEditDto(data: extHostProtocol.CellExecuteEditDto): notebookCommon.IImmediateCellEditOperation {
if (data.editType === notebookCommon.CellEditType.PartialInternalMetadata) {
return data;
} else if (data.editType === notebookCommon.CellEditType.Output) {
return {
editType: data.editType,
handle: data.handle,
append: data.append,
outputs: data.outputs.map(fromNotebookOutputDto)
};
} else {
return {
editType: data.editType,
append: data.append,
outputId: data.outputId,
items: data.items.map(fromNotebookOutputItemDto)
};
}
}
export function fromCellEditOperationDto(edit: extHostProtocol.ICellEditOperationDto): notebookCommon.ICellEditOperation {
if (edit.editType === notebookCommon.CellEditType.Replace) {
return {
editType: edit.editType,
index: edit.index,
count: edit.count,
cells: edit.cells.map(fromNotebookCellDataDto)
};
} else {
return edit;
}
}
}

View File

@@ -6,9 +6,9 @@
import { DisposableStore, dispose } from 'vs/base/common/lifecycle';
import { getNotebookEditorFromEditorPane, INotebookEditor, INotebookEditorOptions } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/notebookEditorService';
import { ExtHostContext, ExtHostNotebookEditorsShape, IExtHostContext, INotebookDocumentShowOptions, INotebookEditorViewColumnInfo, MainThreadNotebookEditorsShape, NotebookEditorRevealType } from '../common/extHost.protocol';
import { ExtHostContext, ExtHostNotebookEditorsShape, ICellEditOperationDto, IExtHostContext, INotebookDocumentShowOptions, INotebookEditorViewColumnInfo, MainThreadNotebookEditorsShape, NotebookEditorRevealType } from '../common/extHost.protocol';
import { MainThreadNotebooksAndEditors } from 'vs/workbench/api/browser/mainThreadNotebookDocumentsAndEditors';
import { ICellEditOperation, INotebookDecorationRenderOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookDecorationRenderOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { ICellRange } from 'vs/workbench/contrib/notebook/common/notebookRange';
import { ILogService } from 'vs/platform/log/common/log';
import { URI, UriComponents } from 'vs/base/common/uri';
@@ -19,6 +19,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { editorGroupToViewColumn } from 'vs/workbench/common/editor';
import { equals } from 'vs/base/common/objects';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
class MainThreadNotebook {
@@ -104,7 +105,7 @@ export class MainThreadNotebookEditors implements MainThreadNotebookEditorsShape
}
}
async $tryApplyEdits(editorId: string, modelVersionId: number, cellEdits: ICellEditOperation[]): Promise<boolean> {
async $tryApplyEdits(editorId: string, modelVersionId: number, cellEdits: ICellEditOperationDto[]): Promise<boolean> {
const wrapper = this._mainThreadEditors.get(editorId);
if (!wrapper) {
return false;
@@ -118,7 +119,7 @@ export class MainThreadNotebookEditors implements MainThreadNotebookEditorsShape
return false;
}
//todo@jrieken use proper selection logic!
return editor.textModel.applyEdits(cellEdits, true, undefined, () => undefined, undefined);
return editor.textModel.applyEdits(cellEdits.map(NotebookDto.fromCellEditOperationDto), true, undefined, () => undefined, undefined);
}
async $tryShowNotebookDocument(resource: UriComponents, viewType: string, options: INotebookDocumentShowOptions): Promise<string> {

View File

@@ -10,13 +10,14 @@ import { combinedDisposable, DisposableStore, IDisposable } from 'vs/base/common
import { URI, UriComponents } from 'vs/base/common/uri';
import { IModeService } from 'vs/editor/common/services/modeService';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/notebookEditorService';
import { IImmediateCellEditOperation, INotebookKernel, INotebookKernelChangeEvent } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookKernel, INotebookKernelChangeEvent } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { ExtHostContext, ExtHostNotebookKernelsShape, IExtHostContext, INotebookKernelDto2, MainContext, MainThreadNotebookKernelsShape } from '../common/extHost.protocol';
import { CellExecuteEditDto, ExtHostContext, ExtHostNotebookKernelsShape, IExtHostContext, INotebookKernelDto2, MainContext, MainThreadNotebookKernelsShape } from '../common/extHost.protocol';
abstract class MainThreadKernel implements INotebookKernel {
@@ -225,14 +226,16 @@ export class MainThreadNotebookKernels implements MainThreadNotebookKernelsShape
// --- execution editss
async $applyExecutionEdits(resource: UriComponents, cellEdits: IImmediateCellEditOperation[]): Promise<void> {
async $applyExecutionEdits(resource: UriComponents, cellEdits: CellExecuteEditDto[]): Promise<void> {
const textModel = this._notebookService.getNotebookTextModel(URI.from(resource));
if (!textModel) {
throw new Error(`Can't apply edits to unknown notebook model: ${URI.revive(resource).toString()}`);
}
const edits = cellEdits.map(NotebookDto.fromCellExecuteEditDto);
try {
textModel.applyEdits(cellEdits, true, undefined, () => undefined, undefined, false);
textModel.applyEdits(edits, true, undefined, () => undefined, undefined, false);
} catch (e) {
// Clearing outputs at the same time as the EH calling append/replaceOutputItems is an expected race, and it should be a no-op.
// And any other failure should not throw back to the extension.