mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 09:27:27 +00:00
Support multiple workspace edits
This commit is contained in:
parent
db5c529781
commit
be00b6b8fa
1 changed files with 18 additions and 5 deletions
|
@ -3,16 +3,29 @@ import * as vscode from 'vscode';
|
|||
import { assert } from './util';
|
||||
|
||||
export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) {
|
||||
assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`);
|
||||
const [uri, edits] = edit.entries()[0];
|
||||
if (edit.entries().length === 1) {
|
||||
const [uri, edits] = edit.entries()[0];
|
||||
const editor = await editorFromUri(uri);
|
||||
if (editor) await applySnippetTextEdits(editor, edits);
|
||||
return;
|
||||
}
|
||||
for (const [uri, edits] of edit.entries()) {
|
||||
const editor = await editorFromUri(uri);
|
||||
if (editor) await editor.edit((builder) => {
|
||||
for (const indel of edits) {
|
||||
assert(!parseSnippet(indel.newText), `bad ws edit: snippet received with multiple edits: ${JSON.stringify(edit)}`);
|
||||
builder.replace(indel.range, indel.newText);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function editorFromUri(uri: vscode.Uri): Promise<vscode.TextEditor | undefined> {
|
||||
if (vscode.window.activeTextEditor?.document.uri !== uri) {
|
||||
// `vscode.window.visibleTextEditors` only contains editors whose contents are being displayed
|
||||
await vscode.window.showTextDocument(uri, {});
|
||||
}
|
||||
const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString());
|
||||
if (!editor) return;
|
||||
await applySnippetTextEdits(editor, edits);
|
||||
return vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString());
|
||||
}
|
||||
|
||||
export async function applySnippetTextEdits(editor: vscode.TextEditor, edits: vscode.TextEdit[]) {
|
||||
|
|
Loading…
Reference in a new issue