vscode - 有没有办法手动触发事件?

vscode - Is there a way to manually trigger events?

我是 运行 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) { ... }