summaryrefslogtreecommitdiff
path: root/packages/vscode-ide-companion/src/recent-files-manager.ts
diff options
context:
space:
mode:
authorShreya Keshive <[email protected]>2025-07-28 14:20:56 -0400
committerGitHub <[email protected]>2025-07-28 18:20:56 +0000
commitcfe3753d4c956ffcf13e227c0619069db672adbf (patch)
tree5d0b1ad9bfe3950861aff072e9cec91b5c20570e /packages/vscode-ide-companion/src/recent-files-manager.ts
parent9aef0a8e6c133d3bf1ff43f80119664c154ffdf2 (diff)
Refactors companion VS Code extension to import & use notification schema defined in gemini-cli (#5059)
Diffstat (limited to 'packages/vscode-ide-companion/src/recent-files-manager.ts')
-rw-r--r--packages/vscode-ide-companion/src/recent-files-manager.ts111
1 files changed, 0 insertions, 111 deletions
diff --git a/packages/vscode-ide-companion/src/recent-files-manager.ts b/packages/vscode-ide-companion/src/recent-files-manager.ts
deleted file mode 100644
index 317cc903..00000000
--- a/packages/vscode-ide-companion/src/recent-files-manager.ts
+++ /dev/null
@@ -1,111 +0,0 @@
-/**
- * @license
- * Copyright 2025 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import * as vscode from 'vscode';
-
-export const MAX_FILES = 10;
-export const MAX_FILE_AGE_MINUTES = 5;
-
-interface RecentFile {
- uri: vscode.Uri;
- timestamp: number;
-}
-
-/**
- * Keeps track of the 10 most recently-opened files
- * opened less than 5 min ago. If a file is closed or deleted,
- * it will be removed. If the max length is reached, older files will get removed first.
- */
-export class RecentFilesManager {
- private readonly files: RecentFile[] = [];
- private readonly onDidChangeEmitter = new vscode.EventEmitter<void>();
- readonly onDidChange = this.onDidChangeEmitter.event;
- private debounceTimer: NodeJS.Timeout | undefined;
-
- constructor(private readonly context: vscode.ExtensionContext) {
- const editorWatcher = vscode.window.onDidChangeActiveTextEditor(
- (editor) => {
- if (editor) {
- this.add(editor.document.uri);
- }
- },
- );
- const deleteWatcher = vscode.workspace.onDidDeleteFiles((event) => {
- for (const uri of event.files) {
- this.remove(uri);
- }
- });
- const closeWatcher = vscode.workspace.onDidCloseTextDocument((document) => {
- this.remove(document.uri);
- });
- const renameWatcher = vscode.workspace.onDidRenameFiles((event) => {
- for (const { oldUri, newUri } of event.files) {
- this.remove(oldUri, false);
- this.add(newUri);
- }
- });
-
- const selectionWatcher = vscode.window.onDidChangeTextEditorSelection(
- () => {
- this.fireWithDebounce();
- },
- );
-
- context.subscriptions.push(
- editorWatcher,
- deleteWatcher,
- closeWatcher,
- renameWatcher,
- selectionWatcher,
- );
- }
-
- private fireWithDebounce() {
- if (this.debounceTimer) {
- clearTimeout(this.debounceTimer);
- }
- this.debounceTimer = setTimeout(() => {
- this.onDidChangeEmitter.fire();
- }, 50); // 50ms
- }
-
- private remove(uri: vscode.Uri, fireEvent = true) {
- const index = this.files.findIndex(
- (file) => file.uri.fsPath === uri.fsPath,
- );
- if (index !== -1) {
- this.files.splice(index, 1);
- if (fireEvent) {
- this.fireWithDebounce();
- }
- }
- }
-
- add(uri: vscode.Uri) {
- if (uri.scheme !== 'file') {
- return;
- }
-
- this.remove(uri, false);
- this.files.unshift({ uri, timestamp: Date.now() });
-
- if (this.files.length > MAX_FILES) {
- this.files.pop();
- }
- this.fireWithDebounce();
- }
-
- get recentFiles(): Array<{ filePath: string; timestamp: number }> {
- const now = Date.now();
- const maxAgeInMs = MAX_FILE_AGE_MINUTES * 60 * 1000;
- return this.files
- .filter((file) => now - file.timestamp < maxAgeInMs)
- .map((file) => ({
- filePath: file.uri.fsPath,
- timestamp: file.timestamp,
- }));
- }
-}