最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect when document is closed - Stack Overflow

programmeradmin11浏览0评论

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
  • Have you checked if the TextDocument.isClosed property updates when you close the TextDocument via the API? – HaaLeo Commented Feb 22, 2018 at 20:09
  • 1 Yes, I tried but it returns "false" even when the file is closed. Again, that bug appears only when a document is loaded via API. When a document is open via UI isClosed return real state. – A. Cheshirov Commented Feb 23, 2018 at 18:54
  • Haven't worked with workspace-documents, but do they fire the DocumentClosing-event just before closing? You might do some magic inside that event, even thou it's not really meant to be used that way. If you control the closing yourself, maybe you could rely on checking if file is open (physically) or some other trick? – Stacking For Heap Commented Feb 28, 2018 at 22:26
  • Could it be because the documentation says Note 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
Add a comment  | 

4 Answers 4

Reset to default 8 +25

‍‍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.

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)

发布评论

评论列表(0)

  1. 暂无评论