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.
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.
Share Improve this question asked Feb 8, 2018 at 19:50 A. CheshirovA. Cheshirov 4,8961 gold badge16 silver badges14 bronze badges 4 |4 Answers
Reset to default 8 +25vscode.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.
private _subscriptions: vscode.Disposable;
constructor() {
// Listen to closeTextDocument
this._subscriptions = vscode.workspace.onDidCloseTextDocument(
// your code here
);
}
dispose() {
this._subscriptions.dispose();
}
sadly there is no way to catch the document close atm because believe it or not onDidCloseTextDocument
fires for more than just closing the file, to test add the below to ur ext active
vscode.workspace.onDidCloseTextDocument((doc) => {
console.log(doc)
})
and watch the magic in the console.
apart from the totally unrelated documentation
To add an event listener when a visible text document is closed,
use the TextEditor events in the window namespace.
there is no close event on the window namespace https://code.visualstudio.com/api/references/vscode-api#window maybe am blind but if anyone found it plz add it here.
also the isClosed
prop on the document is not reliable, because its actually true
when you open a document.
I've been looking for something like this and i think i finally found an answer to this question. By using vscode.window.tabGroups
you can check for any changes to the currently in a tab shown files:
vscode.window.tabGroups.onDidChangeTabs((changedEvent) => {
console.log(`Tab group changed`)
console.log(changedEvent.changed) // changed tabs
console.log(changedEvent.opened) // opened tabs
console.log(changedEvent.closed) // closed tabs
})
This seems to work for every opening and closing event that you are able to see (without problems like vscode.workspace.onDidCloseTextDocument
)
TextDocument.isClosed
property updates when you close theTextDocument
via the API? – HaaLeo Commented Feb 22, 2018 at 20:09Note that the lifecycle of the returned document is owned by the editor and not by the extension. That means an onDidClose-event can occur at any time after opening it.
? – Tarun Lalwani Commented Mar 1, 2018 at 19:11