What I'm doing is basically:
async function doItAll(ownEdits: Array<TextEdit>) {
const editor: TextEditor = await getEditor();
applyOwnChanges(editor, ownEdits);
await commands.executeCommand('editor.action.organizeImports',
editor.document.uri.path);
await editor.document.save();
}
It all works, but the save
happens before organizeImports
finishes and I end up with a dirty editor, when the imports get modified.
I tripple checked that I didn't forget the await
keyword, yet it works like it wasn't there.
This may be a bug or I may be doing or expecting something wrong. Am I?
Indeed organizeImports command returns before it actually updated the document.
As workaround use onDidChangeTextDocument to intercept when organizeImports is finished and your doc is ready for save.
const handler = vscode.workspace.onDidChangeTextDocument(doc => {
if (doc.document.uri.path === editor.document.uri.path) {
doc.document.save()
handler.dispose()
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With