Detect when document is closed

房东的猫 提交于 2019-12-08 15:44:41

问题


I'm working on Visual Studio Code extension and I need to detect when some document window is closed. I know about vscode.workspace.onDidCloseTextDocument event and it works at general.

But if I open a file from the workspace via API:

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

and then close it, the onDidCloseTextDocument doesn't fire as usual. Its fire but few minutes later.

I know if this is some bug or this is the way VSCode works but I need to know how to detect when document window is closed.

I was reading that opening file via API is some kind of "virtual" file. So, probably this cause the problem.


回答1:


‍‍vscode.workspace.onDidCloseTextDocument is emitted when a text document is disposed. To add an event listener when a visible text document is closed, use the TextEditor events in the window namespace. Note that this event is not emitted when a TextEditor is closed but the document remains open in another visible text editor.

For more information please see this.




回答2:


private _subscriptions: vscode.Disposable;

constructor() {

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

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


来源:https://stackoverflow.com/questions/48693666/detect-when-document-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!