检测文档何时关闭

时间:2018-02-08 19:50:10

标签: javascript visual-studio-code vscode-extensions

我正在处理Visual Studio代码扩展,我需要检测何时关闭某个文档窗口。 我知道vscode.workspace.onDidCloseTextDocument事件,它一般都有效。

但是如果我通过API从工作区打开文件:

vscode.workspace.openTextDocument(localPath).then(function (doc) {
    vscode.window.showTextDocument(doc, 1);
});

然后关闭它,onDidCloseTextDocument不像往常一样触发。 几分钟之后就开火了。

我知道这是否是一个错误,或者这是VSCode的工作方式,但我需要知道如何检测文档窗口何时关闭。

我正在阅读通过API打开文件是某种"虚拟"文件。所以,这可能会导致问题。

3 个答案:

答案 0 :(得分:6)

文本文档处置时会发出

onDidCloseTextDocument。要在可见文本文档关闭时添加事件侦听器,请使用窗口命名空间中的TextEditor事件。请注意,当TextEditor关闭但文档在另一个可见文本编辑器中保持打开时,不会发出此事件。

有关详细信息,请参阅this

答案 1 :(得分:3)

private _subscriptions: vscode.Disposable;

constructor() {

    // Listen to closeTextDocument
    this._subscriptions = vscode.workspace.onDidCloseTextDocument(
        // your code here
    );
}

dispose() {
    this._subscriptions.dispose();
}

答案 2 :(得分:0)

不幸的是,没有办法捕获文档关闭atm,因为无论是否相信,sessionStorage.getItem('editvalue')触发的不仅仅是关闭文件,还需要测试将以下内容添加到ur ext onDidCloseTextDocument

active

并在控制台中观看魔术。

除了完全不相关的文档

  

要在关闭可见文本文档时添加事件侦听器,

     

在窗口名称空间中使用TextEditor事件。

窗口命名空间https://code.visualstudio.com/api/references/vscode-api#window上没有关闭事件,可能是盲目的,但是如果有人找到它,请在此处添加。

文档上的vscode.workspace.onDidCloseTextDocument((doc) => { console.log(doc) }) 道具也不可靠,因为打开文档时实际上是isClosed