我在做什么基本上是:
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();
}
一切正常,但
save
发生在 organizeImports
之前完成,当导入被修改时,我最终得到一个肮脏的编辑器。我检查了一下我没有忘记
await
关键字,但它的工作原理就像它不存在一样。这可能是一个错误,或者我可能正在做或期待一些错误的事情。我是吗?
最佳答案
事实上,在它实际更新文档之前,组织导入命令返回。
作为解决方法使用 onDidChangeTextDocument 当组织导入完成并且您的文档准备好保存时进行拦截。
const handler = vscode.workspace.onDidChangeTextDocument(doc => {
if (doc.document.uri.path === editor.document.uri.path) {
doc.document.save()
handler.dispose()
}
})
关于typescript - 以编程方式调用组织导入并随后保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62244964/