I'm playing with vscode's API and I simply tried to insert two strings into the active editor with two edit calls, but only the first string is inserted:
const editor = vscode.window.activeTextEditor;
editor?.edit(edit => {
edit.insert(editor.selection.active, 'test1');
});
editor?.edit(edit => {
edit.insert(editor.selection.active, 'test2');
});
Why is that? I know that if I put the two strings in the same call then it works, but I don't get it why it doesn't work with separate calls.
CodePudding user response:
I think it has to do with "Note that the edit-builder is only valid while the callback executes." bit since it never prints test2
(from https://code.visualstudio.com/api/references/vscode-api#TextEditor).
Perhaps it is because you are creating another edit-builder while the first still exists - it is asynchronous (and may very well be a singleton so another instance may not be created before the first finishes).
This works:
editor?.edit(edit => {
edit.insert(editor.selection.active, 'test1');
edit.insert(editor.selection.active, 'test2');
})
and this works:
editor?.edit(edit => {
edit.insert(editor.selection.active, 'test1');
// edit.insert(editor.selection.active, 'test2');
}).then(success => {
if (!success) {
return
}
else {
editor?.edit(edit => {
edit.insert(editor.selection.active, 'test2');
})
}