Add notebookWorkspaceEdit api proposal (#149128)

* Add notebookWorkspaceEdit api proposal

Splits a new `notebookWorkspaceEdit` out of the existing `notebookEditorEdit` proposal.

The notebookWorkspaceEdit reflects the api that we believe should be finalized instead of `notebookEditorEdit`. It lets extensions use workspaceedits to change cells in a notebook or replace the metadata for a notebook document

As part of this change, I've also marked all of the `notebookEditorEdit` apis as deprecated (except for `replaceNotebookMetadata` which exists in the new proposal too)

* Export type from extHost
This commit is contained in:
Matt Bierner
2022-05-16 11:25:14 -07:00
committed by GitHub
parent cfbf3d5dd7
commit 3f531a7de3
5 changed files with 141 additions and 21 deletions

View File

@@ -1321,6 +1321,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
NotebookCellOutputItem: extHostTypes.NotebookCellOutputItem,
NotebookCellStatusBarItem: extHostTypes.NotebookCellStatusBarItem,
NotebookControllerAffinity: extHostTypes.NotebookControllerAffinity,
NotebookEdit: extHostTypes.NotebookEdit,
PortAttributes: extHostTypes.PortAttributes,
LinkedEditingRanges: extHostTypes.LinkedEditingRanges,
TestResultState: extHostTypes.TestResultState,

View File

@@ -598,6 +598,43 @@ export class TextEdit {
}
}
@es5ClassCompat
export class NotebookEdit implements vscode.NotebookEdit {
static isNotebookCellEdit(thing: any): thing is NotebookEdit {
if (thing instanceof NotebookEdit) {
return true;
}
if (!thing) {
return false;
}
return NotebookRange.isNotebookRange((<NotebookEdit>thing))
&& Array.isArray((<NotebookEdit>thing).newCells);
}
static replaceCells(range: NotebookRange, newCells: NotebookCellData[]): NotebookEdit {
return new NotebookEdit(range, newCells);
}
static deleteCells(range: NotebookRange): NotebookEdit {
return new NotebookEdit(range, []);
}
static updateCellMetadata(index: number, newMetadata: { [key: string]: any }): NotebookEdit {
return new NotebookEdit(new NotebookRange(index, index), [], newMetadata);
}
readonly range: NotebookRange;
readonly newCells: NotebookCellData[];
readonly newCellMetadata?: { [key: string]: any };
constructor(range: NotebookRange, newCells: NotebookCellData[], newCellMetadata?: { [key: string]: any }) {
this.range = range;
this.newCells = newCells;
this.newCellMetadata = newCellMetadata;
}
}
export class SnippetTextEdit implements vscode.SnippetTextEdit {
range: vscode.Range;
@@ -741,7 +778,7 @@ export class WorkspaceEdit implements vscode.WorkspaceEdit {
return this._edits.some(edit => edit._type === FileEditType.Text && edit.uri.toString() === uri.toString());
}
set(uri: URI, edits: TextEdit[]): void {
set(uri: URI, edits: TextEdit[] | unknown): void {
if (!edits) {
// remove all text edits for `uri`
for (let i = 0; i < this._edits.length; i++) {
@@ -753,9 +790,17 @@ export class WorkspaceEdit implements vscode.WorkspaceEdit {
coalesceInPlace(this._edits);
} else {
// append edit to the end
for (const edit of edits) {
for (const edit of edits as TextEdit[] | NotebookEdit[]) {
if (edit) {
this._edits.push({ _type: FileEditType.Text, uri, edit });
if (NotebookEdit.isNotebookCellEdit(edit)) {
if (edit.newCellMetadata) {
this.replaceNotebookCellMetadata(uri, edit.range.start, edit.newCellMetadata);
} else {
this.replaceNotebookCells(uri, edit.range, edit.newCells);
}
} else {
this._edits.push({ _type: FileEditType.Text, uri, edit });
}
}
}
}