Skip to content

Commit

Permalink
Add replace and insert factory functions for SnippetTextEdit, a…
Browse files Browse the repository at this point in the history
…dd `WorkspaceEdit#set` overloads

#145374
  • Loading branch information
jrieken committed Sep 7, 2022
1 parent c6fd3c9 commit cf2aa98
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 21 deletions.
65 changes: 47 additions & 18 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,14 @@ export class SnippetTextEdit implements vscode.SnippetTextEdit {
&& SnippetString.isSnippetString((<SnippetTextEdit>thing).snippet);
}

static replace(range: Range, snippet: SnippetString): SnippetTextEdit {
return new SnippetTextEdit(range, snippet);
}

static insert(position: Position, snippet: SnippetString): SnippetTextEdit {
return SnippetTextEdit.replace(new Range(position, position), snippet);
}

range: Range;

snippet: SnippetString;
Expand Down Expand Up @@ -799,34 +807,55 @@ 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[] | unknown): void {
set(uri: URI, edits: (TextEdit | SnippetTextEdit)[]): void;
set(uri: URI, edits: [TextEdit | SnippetTextEdit, vscode.WorkspaceEditEntryMetadata][]): void;
set(uri: URI, edits: NotebookEdit[]): void;
set(uri: URI, edits: [NotebookEdit, vscode.WorkspaceEditEntryMetadata][]): void;

set(uri: URI, edits: null | undefined | (TextEdit | SnippetTextEdit | NotebookEdit | [NotebookEdit, vscode.WorkspaceEditEntryMetadata] | [TextEdit | SnippetTextEdit, vscode.WorkspaceEditEntryMetadata])[]): void {
if (!edits) {
// remove all text edits for `uri`
// remove all text, snippet, or notebook edits for `uri`
for (let i = 0; i < this._edits.length; i++) {
const element = this._edits[i];
if (element._type === FileEditType.Text && element.uri.toString() === uri.toString()) {
this._edits[i] = undefined!; // will be coalesced down below
switch (element._type) {
case FileEditType.Text:
case FileEditType.Snippet:
case FileEditType.Cell:
case FileEditType.CellReplace:
if (element.uri.toString() === uri.toString()) {
this._edits[i] = undefined!; // will be coalesced down below
}
break;
}
}
coalesceInPlace(this._edits);
} else {
// append edit to the end
for (const edit of edits as TextEdit[] | NotebookEdit[] | SnippetTextEdit[]) {
if (edit) {
if (NotebookEdit.isNotebookCellEdit(edit)) {
if (edit.newCellMetadata) {
this.replaceNotebookCellMetadata(uri, edit.range.start, edit.newCellMetadata);
} else if (edit.newNotebookMetadata) {
this.replaceNotebookMetadata(uri, edit.newNotebookMetadata);
} else {
this.replaceNotebookCells(uri, edit.range, edit.newCells);
}
} else if (SnippetTextEdit.isSnippetTextEdit(edit)) {
this._edits.push({ _type: FileEditType.Snippet, uri, range: edit.range, edit: edit.snippet });

for (const editOrTuple of edits) {
if (!editOrTuple) {
continue;
}
let edit: TextEdit | SnippetTextEdit | NotebookEdit;
let metadata: vscode.WorkspaceEditEntryMetadata | undefined;
if (Array.isArray(editOrTuple)) {
edit = editOrTuple[0];
metadata = editOrTuple[1];
} else {
edit = editOrTuple;
}
if (NotebookEdit.isNotebookCellEdit(edit)) {
if (edit.newCellMetadata) {
this.replaceNotebookCellMetadata(uri, edit.range.start, edit.newCellMetadata, metadata);
} else if (edit.newNotebookMetadata) {
this.replaceNotebookMetadata(uri, edit.newNotebookMetadata, metadata);
} else {
this._edits.push({ _type: FileEditType.Text, uri, edit });
this.replaceNotebookCells(uri, edit.range, edit.newCells, metadata);
}
} else if (SnippetTextEdit.isSnippetTextEdit(edit)) {
this._edits.push({ _type: FileEditType.Snippet, uri, range: edit.range, edit: edit.snippet, metadata });

} else {
this._edits.push({ _type: FileEditType.Text, uri, edit, metadata });
}
}
}
Expand Down
47 changes: 44 additions & 3 deletions src/vscode-dts/vscode.proposed.snippetWorkspaceEdit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ declare module 'vscode' {

export class SnippetTextEdit {

/**
* Utility to create an replace snippet edit.
*
* @param range A range.
* @param snippet A snippet string.
* @return A new snippet edit object.
*/
static replace(range: Range, snippet: SnippetString): SnippetTextEdit;

/**
* Utility to create an insert snippet edit.
*
* @param position A position, will become an empty range.
* @param snippet A snippet string.
* @return A new snippet edit object.
*/
static insert(position: Position, snippet: SnippetString): SnippetTextEdit;

/**
* The range this edit applies to.
*/
Expand All @@ -33,12 +51,35 @@ declare module 'vscode' {
interface WorkspaceEdit {

/**
* Set (and replace) edits for a resource.
* Set (and replace) notebook edits for a resource.
*
* @param uri A resource identifier.
* @param edits An array of edits.
*/
set(uri: Uri, edits: NotebookEdit[]): void;

/**
* Set (and replace) notebook edits with metadata for a resource.
*
* @param uri A resource identifier.
* @param edits An array of edits.
*/
set(uri: Uri, edits: [NotebookEdit, WorkspaceEditEntryMetadata][]): void;

/**
* Set (and replace) text edits or snippet edits for a resource.
*
* @param uri A resource identifier.
* @param edits An array of edits.
*/
set(uri: Uri, edits: (TextEdit | SnippetTextEdit)[]): void;

/**
* Set (and replace) text edits or snippet edits with metadata for a resource.
*
* @param uri A resource identifier.
* @param edits An array of edits.
*/
// TODO@API we support mixed edits of TextEdit and SnippetTextEdit
set(uri: Uri, edits: TextEdit[] | SnippetTextEdit[] | NotebookEdit[]): void;
set(uri: Uri, edits: [TextEdit | SnippetTextEdit, WorkspaceEditEntryMetadata][]): void;
}
}

0 comments on commit cf2aa98

Please sign in to comment.