Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically invoke Organize Imports and save the file afterwards

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?

like image 443
maaartinus Avatar asked Oct 26 '22 21:10

maaartinus


1 Answers

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()
  }
})
like image 180
Yuriy Naydenov Avatar answered Dec 28 '22 10:12

Yuriy Naydenov