我正在 workspace.onWillSaveTextDocument
处理程序上运行回调。它提供了 vscode.TextDocument
,这对于我想要在此事件中完成的工作是必要的。
在某些情况下,我也想将其他文件(当前未打开的文件)视为刚刚保存。
能够创建 vscode.TextDocument
的新实例就足够了,但我无法弄清楚。
有没有办法做这样的事情:
workspace.pretendThisWasSavedJustNow('/path/to/other/file.ts');
最佳答案
我从事 VSCode 工作,虽然我不知道您的确切用例,但我相信您正在寻找 workspace.openTextDocument
。
要对 workspace.onWillSaveTextDocument
以及扩展程序触发的其他事件使用相同的函数,请尝试以下操作:
// Actual save
workspace.onWillSaveTextDocument(e => onDocumentSave(e.document))
// Emulated save
setInterval(() => {
workspace.openTextDocument('path/to/other/file.ts')
.then(document => onDocumentSave(document))
}, 5000)
// Common save handling implementation
function onDocumentSave(document: TextDocument) { ... }
关于visual-studio-code - vscode - 有没有办法手动触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42704683/