Skip to content

Commit

Permalink
tweak API proposal for snippet edits, make this WorkspaceEdit only,…
Browse files Browse the repository at this point in the history
… remove old proposal bit

#145374
  • Loading branch information
jrieken committed Jul 7, 2022
1 parent 898ac3c commit c0d0135
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1157,17 +1157,14 @@ suite('vscode API - workspace', () => {
assert.ok(edt === vscode.window.activeTextEditor);

const we = new vscode.WorkspaceEdit();
we.set(document.uri, [{ range: new vscode.Range(0, 0, 0, 0), newText: '', newText2: new vscode.SnippetString('${1:foo}${2:bar}') }]);
we.replace(document.uri, new vscode.Range(0, 0, 0, 0), new vscode.SnippetString('${1:foo}${2:bar}'));
const success = await vscode.workspace.applyEdit(we);


if (edt !== vscode.window.activeTextEditor) {
return this.skip();
}

assert.ok(success);
assert.strictEqual(document.getText(), 'foobarhello\nworld');
assert.deepStrictEqual(edt.selections, [new vscode.Selection(0, 0, 0, 3)]);

});
});
23 changes: 17 additions & 6 deletions src/vs/workbench/api/common/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,28 @@ export namespace WorkspaceEdit {

} else if (entry._type === types.FileEditType.Text) {
// text edits
const edit = <languages.IWorkspaceTextEdit>{
result.edits.push(<languages.IWorkspaceTextEdit>{
resource: entry.uri,
textEdit: TextEdit.from(entry.edit),
versionId: !toCreate.has(entry.uri) ? versionInfo?.getTextDocumentVersion(entry.uri) : undefined,
metadata: entry.metadata
};
if (allowSnippetTextEdit && entry.edit.newText2 instanceof types.SnippetString) {
edit.textEdit.insertAsSnippet = true;
edit.textEdit.text = entry.edit.newText2.value;
});
} else if (entry._type === types.FileEditType.Snippet) {
// snippet text edits
if (!allowSnippetTextEdit) {
console.warn(`DROPPING snippet text edit because proposal IS NOT ENABLED`, entry);
continue;
}
result.edits.push(edit);
result.edits.push(<languages.IWorkspaceTextEdit>{
resource: entry.uri,
textEdit: {
range: Range.from(entry.range),
text: entry.edit.value,
insertAsSnippet: true
},
versionId: !toCreate.has(entry.uri) ? versionInfo?.getTextDocumentVersion(entry.uri) : undefined,
metadata: entry.metadata
});

} else if (entry._type === types.FileEditType.Cell) {
// cell edit
Expand Down
20 changes: 16 additions & 4 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,6 @@ export class TextEdit {

protected _range: Range;
protected _newText: string | null;
newText2?: string | SnippetString;
protected _newEol?: EndOfLine;

get range(): Range {
Expand Down Expand Up @@ -660,6 +659,7 @@ export const enum FileEditType {
Text = 2,
Cell = 3,
CellReplace = 5,
Snippet = 6,
}

export interface IFileOperation {
Expand All @@ -677,6 +677,14 @@ export interface IFileTextEdit {
metadata?: vscode.WorkspaceEditEntryMetadata;
}

export interface IFileSnippetTextEdit {
_type: FileEditType.Snippet;
uri: URI;
range: vscode.Range;
edit: vscode.SnippetString;
metadata?: vscode.WorkspaceEditEntryMetadata;
}

export interface IFileCellEdit {
_type: FileEditType.Cell;
uri: URI;
Expand All @@ -695,7 +703,7 @@ export interface ICellEdit {
}


type WorkspaceEditEntry = IFileOperation | IFileTextEdit | IFileCellEdit | ICellEdit;
type WorkspaceEditEntry = IFileOperation | IFileTextEdit | IFileSnippetTextEdit | IFileCellEdit | ICellEdit;

@es5ClassCompat
export class WorkspaceEdit implements vscode.WorkspaceEdit {
Expand Down Expand Up @@ -762,8 +770,12 @@ export class WorkspaceEdit implements vscode.WorkspaceEdit {

// --- text

replace(uri: URI, range: Range, newText: string, metadata?: vscode.WorkspaceEditEntryMetadata): void {
this._edits.push({ _type: FileEditType.Text, uri, edit: new TextEdit(range, newText), metadata });
replace(uri: URI, range: Range, newText: string | vscode.SnippetString, metadata?: vscode.WorkspaceEditEntryMetadata): void {
if (typeof newText === 'string') {
this._edits.push({ _type: FileEditType.Text, uri, edit: new TextEdit(range, newText), metadata });
} else {
this._edits.push({ _type: FileEditType.Snippet, uri, range, edit: newText, metadata });
}
}

insert(resource: URI, position: Position, newText: string, metadata?: vscode.WorkspaceEditEntryMetadata): void {
Expand Down
7 changes: 3 additions & 4 deletions src/vscode-dts/vscode.proposed.snippetWorkspaceEdit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ declare module 'vscode' {

// https://github.com/microsoft/vscode/issues/145374

export interface TextEdit {
interface WorkspaceEdit {

// will be merged with newText
// will NOT be supported everywhere, only: `workspace.applyEdit`
newText2?: string | SnippetString;
// todo@API have a SnippetTextEdit and allow to set that?
replace(uri: Uri, range: Range, newText: string | SnippetString, metadata?: WorkspaceEditEntryMetadata): void;
}
}

0 comments on commit c0d0135

Please sign in to comment.