From c5e86e8f5856b072a1dc72894ea2b47c7ceff5db Mon Sep 17 00:00:00 2001 From: adarshramiyer Date: Mon, 9 Jun 2025 18:21:25 -0700 Subject: [PATCH] restored files/closed data passing and unified util functions for getting open tabs --- core/core.ts | 27 ++++++++++--------- core/protocol/core.ts | 2 +- .../vscode/src/extension/VsCodeExtension.ts | 17 ++++++------ extensions/vscode/src/util/ideUtils.ts | 24 +++++------------ 4 files changed, 31 insertions(+), 39 deletions(-) diff --git a/core/core.ts b/core/core.ts index b35c03dfb..cbbaa11c3 100644 --- a/core/core.ts +++ b/core/core.ts @@ -601,22 +601,25 @@ export class Core { on("files/closed", async ({ data }) => { try { - const filepaths = data.fileUris; - if (!prevFilepaths.filepaths.length) { - prevFilepaths.filepaths = filepaths; - } + const fileUris = await this.ide.getOpenFiles(); + if (fileUris) { + const filepaths = fileUris.map((uri) => uri.toString()); - // If there is a removal, including if the number of tabs is the same (which can happen with temp tabs) - if (filepaths.length <= prevFilepaths.filepaths.length) { - // Remove files from cache that are no longer open (i.e. in the cache but not in the list of opened tabs) - for (const [key, _] of openedFilesLruCache.entriesDescending()) { - if (!filepaths.includes(key)) { - openedFilesLruCache.delete(key); + if (!prevFilepaths.filepaths.length) { + prevFilepaths.filepaths = filepaths; + } + + // If there is a removal, including if the number of tabs is the same (which can happen with temp tabs) + if (filepaths.length <= prevFilepaths.filepaths.length) { + // Remove files from cache that are no longer open (i.e. in the cache but not in the list of opened tabs) + for (const [key, _] of openedFilesLruCache.entriesDescending()) { + if (!filepaths.includes(key)) { + openedFilesLruCache.delete(key); + } } } + prevFilepaths.filepaths = filepaths; } - - prevFilepaths.filepaths = filepaths; } catch (e) { console.error( `didChangeVisibleTextEditors: failed to update openedFilesLruCache`, diff --git a/core/protocol/core.ts b/core/protocol/core.ts index c46429663..ba3736daa 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -181,7 +181,7 @@ export type ToCoreFromIdeOrWebviewProtocol = { "files/opened": [{ uris?: string[] }, void]; "files/created": [{ uris?: string[] }, void]; "files/deleted": [{ uris?: string[] }, void]; - "files/closed": [{ fileUris: string[]; uris?: string[] }, void]; + "files/closed": [{ uris?: string[] }, void]; // Docs etc. Indexing. TODO move codebase to this "indexing/reindex": [{ type: string; id: string }, void]; diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index 652e67bdd..d823c6650 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -295,14 +295,10 @@ export class VsCodeExtension { }); }); - vscode.workspace.onDidCloseTextDocument((document) => { - if (document) { - let openFiles = this.ideUtils.getOpenFileUrisAsStrings(); - this.core.invoke("files/closed", { - uris: [document.uri.toString()], - fileUris: openFiles, - }); - } + vscode.workspace.onDidCloseTextDocument(async (event) => { + this.core.invoke("files/closed", { + uris: [event.uri.toString()], + }); }); vscode.workspace.onDidCreateFiles(async (event) => { @@ -393,7 +389,10 @@ export class VsCodeExtension { }); // initializes openedFileLruCache with files that are already open when the extension is activated - let initialOpenedFilePaths = this.ideUtils.getOpenFileUrisAsStrings(); + let initialOpenedFilePaths = this.ideUtils + .getOpenFiles() + .map((uri) => uri.toString()); + console.log("initial", initialOpenedFilePaths); this.core.invoke("files/opened", { uris: initialOpenedFilePaths }); vscode.workspace.onDidChangeConfiguration(async (event) => { diff --git a/extensions/vscode/src/util/ideUtils.ts b/extensions/vscode/src/util/ideUtils.ts index 2176b5346..6f213b302 100644 --- a/extensions/vscode/src/util/ideUtils.ts +++ b/extensions/vscode/src/util/ideUtils.ts @@ -267,25 +267,15 @@ export class VsCodeIdeUtils { return uri.scheme === "file" || uri.scheme === "vscode-remote"; } - // Gets the URIs of open tabs in the editor - // Stricter criteria than getOpenFiles() - getOpenFileUrisAsStrings(): string[] { - return vscode.window.tabGroups.all - .flatMap((group) => group.tabs) - .filter((tab) => tab.input instanceof vscode.TabInputText) - .map((tab) => (tab.input as vscode.TabInputText).uri.toString()); - } - getOpenFiles(): vscode.Uri[] { return vscode.window.tabGroups.all - .map((group) => { - return group.tabs.map((tab) => { - return (tab.input as any)?.uri; - }); - }) - .flat() - .filter(Boolean) // filter out undefined values - .filter((uri) => this.documentIsCode(uri)); // Filter out undesired documents + .flatMap((group) => group.tabs) + .filter( + (tab) => + tab.input instanceof vscode.TabInputText && + this.documentIsCode((tab.input as vscode.TabInputText).uri), + ) + .map((tab) => (tab.input as vscode.TabInputText).uri); } saveFile(uri: vscode.Uri) {