2020-05-25 08:59:54 +00:00
|
|
|
import * as vscode from "vscode";
|
|
|
|
|
|
|
|
import { assert } from "./util";
|
2023-06-27 19:03:53 +00:00
|
|
|
import { unwrapUndefinable } from "./undefinable";
|
2020-05-25 08:59:54 +00:00
|
|
|
|
2024-02-02 01:38:42 +00:00
|
|
|
export type SnippetTextDocumentEdit = [vscode.Uri, (vscode.TextEdit | vscode.SnippetTextEdit)[]];
|
|
|
|
|
|
|
|
export async function applySnippetWorkspaceEdit(
|
|
|
|
edit: vscode.WorkspaceEdit,
|
|
|
|
editEntries: SnippetTextDocumentEdit[],
|
|
|
|
) {
|
|
|
|
if (editEntries.length === 1) {
|
|
|
|
const [uri, edits] = unwrapUndefinable(editEntries[0]);
|
2020-11-04 13:50:44 +00:00
|
|
|
const editor = await editorFromUri(uri);
|
2024-02-02 01:38:42 +00:00
|
|
|
if (editor) {
|
|
|
|
edit.set(uri, edits);
|
|
|
|
await vscode.workspace.applyEdit(edit);
|
|
|
|
}
|
2020-11-04 13:50:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-02-02 01:38:42 +00:00
|
|
|
for (const [uri, edits] of editEntries) {
|
2020-11-04 13:50:44 +00:00
|
|
|
const editor = await editorFromUri(uri);
|
2022-05-17 17:31:51 +00:00
|
|
|
if (editor) {
|
2020-11-04 13:50:44 +00:00
|
|
|
await editor.edit((builder) => {
|
|
|
|
for (const indel of edits) {
|
|
|
|
assert(
|
2024-02-02 01:38:42 +00:00
|
|
|
!(indel instanceof vscode.SnippetTextEdit),
|
2023-07-11 13:35:10 +00:00
|
|
|
`bad ws edit: snippet received with multiple edits: ${JSON.stringify(
|
|
|
|
edit,
|
|
|
|
)}`,
|
2020-11-04 13:50:44 +00:00
|
|
|
);
|
|
|
|
builder.replace(indel.range, indel.newText);
|
|
|
|
}
|
|
|
|
});
|
2022-05-17 17:31:51 +00:00
|
|
|
}
|
2020-11-04 13:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 08:59:54 +00:00
|
|
|
|
2020-11-04 13:50:44 +00:00
|
|
|
async function editorFromUri(uri: vscode.Uri): Promise<vscode.TextEditor | undefined> {
|
2020-07-21 17:47:27 +00:00
|
|
|
if (vscode.window.activeTextEditor?.document.uri !== uri) {
|
|
|
|
// `vscode.window.visibleTextEditors` only contains editors whose contents are being displayed
|
|
|
|
await vscode.window.showTextDocument(uri, {});
|
|
|
|
}
|
2020-11-04 13:50:44 +00:00
|
|
|
return vscode.window.visibleTextEditors.find(
|
2023-07-11 13:35:10 +00:00
|
|
|
(it) => it.document.uri.toString() === uri.toString(),
|
2020-11-04 13:50:44 +00:00
|
|
|
);
|
2020-05-25 12:12:53 +00:00
|
|
|
}
|
2020-05-25 08:59:54 +00:00
|
|
|
|
2020-05-25 12:12:53 +00:00
|
|
|
export async function applySnippetTextEdits(editor: vscode.TextEditor, edits: vscode.TextEdit[]) {
|
2024-02-02 01:38:42 +00:00
|
|
|
const edit = new vscode.WorkspaceEdit();
|
|
|
|
edit.set(editor.document.uri, toSnippetTextEdits(edits));
|
|
|
|
await vscode.workspace.applyEdit(edit);
|
2020-05-25 08:59:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 01:38:42 +00:00
|
|
|
function hasSnippet(snip: string): boolean {
|
|
|
|
const m = snip.match(/\$\d+|\{\d+:[^}]*\}/);
|
|
|
|
return m != null;
|
2020-05-25 08:59:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 01:38:42 +00:00
|
|
|
function toSnippetTextEdits(
|
|
|
|
edits: vscode.TextEdit[],
|
|
|
|
): (vscode.TextEdit | vscode.SnippetTextEdit)[] {
|
|
|
|
return edits.map((textEdit) => {
|
|
|
|
// Note: text edits without any snippets are returned as-is instead of
|
|
|
|
// being wrapped in a SnippetTextEdit, as otherwise it would be
|
|
|
|
// treated as if it had a tab stop at the end.
|
|
|
|
if (hasSnippet(textEdit.newText)) {
|
|
|
|
return new vscode.SnippetTextEdit(
|
|
|
|
textEdit.range,
|
|
|
|
new vscode.SnippetString(textEdit.newText),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return textEdit;
|
|
|
|
}
|
|
|
|
});
|
2020-05-25 08:59:54 +00:00
|
|
|
}
|