diff options
author | Lukas Wirth <[email protected]> | 2020-11-04 13:50:44 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2020-11-04 14:04:28 +0000 |
commit | be00b6b8fa03f9b578ca5f736e745d4d99ffd649 (patch) | |
tree | e1b83482d2d6eb50aaad6a5b16c13abed448f180 /editors | |
parent | db5c52978151412f43a4d98c1b30ba091c533c39 (diff) |
Support multiple workspace edits
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/src/snippets.ts | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/editors/code/src/snippets.ts b/editors/code/src/snippets.ts index 258b49982..fee736e7d 100644 --- a/editors/code/src/snippets.ts +++ b/editors/code/src/snippets.ts | |||
@@ -3,16 +3,29 @@ import * as vscode from 'vscode'; | |||
3 | import { assert } from './util'; | 3 | import { assert } from './util'; |
4 | 4 | ||
5 | export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { | 5 | export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { |
6 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); | 6 | if (edit.entries().length === 1) { |
7 | const [uri, edits] = edit.entries()[0]; | 7 | const [uri, edits] = edit.entries()[0]; |
8 | const editor = await editorFromUri(uri); | ||
9 | if (editor) await applySnippetTextEdits(editor, edits); | ||
10 | return; | ||
11 | } | ||
12 | for (const [uri, edits] of edit.entries()) { | ||
13 | const editor = await editorFromUri(uri); | ||
14 | if (editor) await editor.edit((builder) => { | ||
15 | for (const indel of edits) { | ||
16 | assert(!parseSnippet(indel.newText), `bad ws edit: snippet received with multiple edits: ${JSON.stringify(edit)}`); | ||
17 | builder.replace(indel.range, indel.newText); | ||
18 | } | ||
19 | }); | ||
20 | } | ||
21 | } | ||
8 | 22 | ||
23 | async function editorFromUri(uri: vscode.Uri): Promise<vscode.TextEditor | undefined> { | ||
9 | if (vscode.window.activeTextEditor?.document.uri !== uri) { | 24 | if (vscode.window.activeTextEditor?.document.uri !== uri) { |
10 | // `vscode.window.visibleTextEditors` only contains editors whose contents are being displayed | 25 | // `vscode.window.visibleTextEditors` only contains editors whose contents are being displayed |
11 | await vscode.window.showTextDocument(uri, {}); | 26 | await vscode.window.showTextDocument(uri, {}); |
12 | } | 27 | } |
13 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); | 28 | return vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); |
14 | if (!editor) return; | ||
15 | await applySnippetTextEdits(editor, edits); | ||
16 | } | 29 | } |
17 | 30 | ||
18 | export async function applySnippetTextEdits(editor: vscode.TextEditor, edits: vscode.TextEdit[]) { | 31 | export async function applySnippetTextEdits(editor: vscode.TextEditor, edits: vscode.TextEdit[]) { |