restored files/closed data passing and unified util functions for getting open tabs

This commit is contained in:
adarshramiyer 2025-06-09 18:21:25 -07:00
parent 039404a0ab
commit c5e86e8f58
4 changed files with 31 additions and 39 deletions

View file

@ -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`,

View file

@ -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];

View file

@ -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) => {

View file

@ -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) {