Extensions do not update cell outputs/metadata directly.

This commit is contained in:
rebornix
2021-02-04 16:35:50 -08:00
parent 4d6d2266fa
commit f2b87fd6cd
4 changed files with 11 additions and 83 deletions
+2 -4
View File
@@ -1081,10 +1081,8 @@ declare module 'vscode' {
readonly cellKind: CellKind;
readonly document: TextDocument;
readonly language: string;
/** @deprecated use WorkspaceEdit.replaceCellOutput */
outputs: CellOutput[];
/** @deprecated use WorkspaceEdit.replaceCellMetadata */
metadata: NotebookCellMetadata;
readonly outputs: CellOutput[];
readonly metadata: NotebookCellMetadata;
}
export interface NotebookDocumentMetadata {
@@ -569,29 +569,6 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
textModel?.updateLanguages(languages);
}
async $spliceNotebookCellOutputs(viewType: string, resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[]): Promise<void> {
this.logService.debug('MainThreadNotebooks#spliceNotebookCellOutputs', resource.path, cellHandle);
const textModel = this._notebookService.getNotebookTextModel(URI.from(resource));
if (!textModel) {
return;
}
const cell = textModel.cells.find(cell => cell.handle === cellHandle);
if (!cell) {
return;
}
textModel.applyEdits(textModel.versionId, [
{
editType: CellEditType.OutputsSplice,
index: textModel.cells.indexOf(cell),
splices
}
], true, undefined, () => undefined, undefined);
}
async $postMessage(editorId: string, forRendererId: string | undefined, value: any): Promise<boolean> {
const editor = this._notebookService.getNotebookEditor(editorId) as INotebookEditor | undefined;
if (editor?.isNotebookEditor) {
@@ -789,7 +789,6 @@ export interface MainThreadNotebookShape extends IDisposable {
$onNotebookKernelChange(handle: number, uri: UriComponents | undefined): void;
$tryApplyEdits(viewType: string, resource: UriComponents, modelVersionId: number, edits: ICellEditOperation[]): Promise<boolean>;
$updateNotebookLanguages(viewType: string, resource: UriComponents, languages: string[]): Promise<void>;
$spliceNotebookCellOutputs(viewType: string, resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[]): Promise<void>;
$postMessage(editorId: string, forRendererId: string | undefined, value: any): Promise<boolean>;
$setStatusBarEntry(id: number, statusBarEntry: INotebookCellStatusBarEntryDto): Promise<void>;
$tryOpenDocument(uriComponents: UriComponents, viewType?: string): Promise<URI>;
@@ -72,7 +72,7 @@ export class ExtHostCell extends Disposable {
private _onDidChangeOutputs = new Emitter<ISplice<IOutputDtoWithId>[]>();
readonly onDidChangeOutputs: Event<ISplice<IOutputDtoWithId>[]> = this._onDidChangeOutputs.event;
private _outputs: any[];
private _outputs: any[]; // it's `IOutputDtop[]`
private _outputMapping = new WeakMap<vscode.CellOutput, string | undefined /* output ID */>();
private _metadata: vscode.NotebookCellMetadata;
@@ -124,12 +124,9 @@ export class ExtHostCell extends Disposable {
document: data.document,
get language() { return data!.document.languageId; },
get outputs() { return that._outputs; },
set outputs(value) { that._updateOutputs(value); },
set outputs(value) { throw new Error('Use WorkspaceEdit to update cell outputs.'); },
get metadata() { return that._metadata; },
set metadata(value) {
that.setMetadata(value);
that._updateMetadata();
},
set metadata(value) { throw new Error('Use WorkspaceEdit to update cell metadata.'); },
});
}
return this._cell;
@@ -140,33 +137,13 @@ export class ExtHostCell extends Disposable {
this._onDidDispose.fire();
}
setOutputs(newOutputs: vscode.CellOutput[]): void {
setOutputs(newOutputs: IOutputDtoWithId[]): void {
this._outputs = newOutputs;
}
private _updateOutputs(newOutputs: vscode.CellOutput[]) {
const rawDiffs = diff<vscode.CellOutput>(this._outputs || [], newOutputs || [], (a) => {
return this._outputMapping.has(a);
});
const transformedDiffs: ISplice<IOutputDtoWithId>[] = rawDiffs.map(diff => {
for (let i = diff.start; i < diff.start + diff.deleteCount; i++) {
this._outputMapping.delete(this._outputs[i]);
}
return {
deleteCount: diff.deleteCount,
start: diff.start,
toInsert: diff.toInsert.map((output): IOutputDtoWithId => {
const uuid = UUID.generateUuid();
this._outputMapping.set(output, uuid);
return { ...output, outputId: uuid };
})
};
});
this._outputs = newOutputs;
this._onDidChangeOutputs.fire(transformedDiffs);
this._outputMapping = new WeakMap<vscode.CellOutput, string | undefined /* output ID */>();
for (const output of this._outputs) {
this._outputMapping.set(output, output.outputId);
delete output.outputId;
}
}
setMetadata(newMetadata: vscode.NotebookCellMetadata): void {
@@ -390,12 +367,6 @@ export class ExtHostNotebookDocument extends Disposable {
this._cellDisposableMapping.set(extCell.handle, store);
}
const store = this._cellDisposableMapping.get(extCell.handle)!;
store.add(extCell.onDidChangeOutputs((diffs) => {
this.eventuallyUpdateCellOutputs(extCell, diffs);
}));
return extCell;
});
@@ -464,23 +435,6 @@ export class ExtHostNotebookDocument extends Disposable {
this._emitter.emitCellMetadataChange(event);
}
async eventuallyUpdateCellOutputs(cell: ExtHostCell, diffs: ISplice<IOutputDtoWithId>[]) {
const outputDtos: NotebookCellOutputsSplice[] = diffs.map(diff => {
const outputs = diff.toInsert;
return [diff.start, diff.deleteCount, outputs];
});
if (!outputDtos.length) {
return;
}
await this._proxy.$spliceNotebookCellOutputs(this._viewType, this.uri, cell.handle, outputDtos);
this._emitter.emitCellOutputsChange({
document: this.notebookDocument,
cells: [cell.cell]
});
}
getCell(cellHandle: number): ExtHostCell | undefined {
return this._cells.find(cell => cell.handle === cellHandle);
}