Detect when document is closed

I am working on a Visual Studio Code extension, and I need to determine when a document window closes. I know about the vscode.workspace.onDidCloseTextDocument event and it works in general.

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

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

and then close it, onDidCloseTextDocument does not start as usual. His fire, but a few minutes later.

I know if this is some kind of error, or how VSCode works, but I need to know how to detect when the document window is closed.

I read that opening a file through the API is a kind of "virtual" file. Thus, perhaps this is causing the problem.

+10
source share
2
private _subscriptions: vscode.Disposable;

constructor() {

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

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

All Articles