【问题标题】:vscode - Is there a way to manually trigger events?vscode - 有没有办法手动触发事件?
【发布时间】:2017-03-09 20:18:52
【问题描述】:

我正在workspace.onWillSaveTextDocument 处理程序上运行回调。它提供了vscode.TextDocument,这对于我想要在这个事件中做的工作是必要的。

在某些情况下,我也希望将其他文件(当前未打开)视为刚刚保存。

能够创建vscode.TextDocument 的新实例就足够了,但我无法弄清楚。

有没有办法做类似的事情:

workspace.pretendThisWasSavedJustNow('/path/to/other/file.ts');

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    我从事 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) { ... }
    

    【讨论】:

      猜你喜欢
      • 2011-01-30
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多