问题
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