diff --git a/src/ui/FileSearchUI.tsx b/src/ui/FileSearchUI.tsx index 816152105..9b093c73d 100644 --- a/src/ui/FileSearchUI.tsx +++ b/src/ui/FileSearchUI.tsx @@ -1,5 +1,5 @@ import { Box, Text } from "ink"; -import React, { useEffect, useState } from "react"; +import React, { useEffect, useState, useRef } from "react"; import { glob } from "glob"; interface FileSearchUIProps { @@ -14,6 +14,11 @@ interface FileItem { displayName: string; } +// Cache for file search results +const fileCache = new Map(); +let allFiles: FileItem[] = []; +let cacheInitialized = false; + const FileSearchUI: React.FC = ({ filter, selectedIndex, @@ -22,28 +27,38 @@ const FileSearchUI: React.FC = ({ }) => { const [files, setFiles] = useState([]); const [isLoading, setIsLoading] = useState(false); + const initializingRef = useRef(false); - useEffect(() => { - const searchFiles = async () => { - setIsLoading(true); - try { - // Search for files matching the filter - const patterns = [ - "**/*.{ts,tsx,js,jsx,py,java,cpp,c,h,hpp,cs,go,rs,rb,php,swift,kt,scala,clj,hs,ml,sh,md,json,yaml,yml,xml,html,css,scss,sass,less,sql,r,m,mm,pl,lua,dart,jl,ex,exs,elm,fs,fsx,ml,mli,v,sv,vhd,vhdl,asm,s,S,f,f90,f95,f03,f08,for,ftn,fpp,F,F90,F95,F03,F08,FOR,FTN,FPP,ada,adb,ads,pas,pp,inc,lpr,dpr,dfm,fmx,lfm,lrs,lpi,lpk,lps,lrt,cfg,conf,config,toml,ini,properties,env,dockerfile,Dockerfile,makefile,Makefile,CMakeLists.txt,BUILD,WORKSPACE,bzl,bazel,gradle,pom.xml,package.json,Cargo.toml,requirements.txt,setup.py,pyproject.toml,composer.json,Gemfile,Podfile,Package.swift,project.pbxproj,*.xcodeproj,*.xcworkspace,*.sln,*.csproj,*.vbproj,*.fsproj,*.vcxproj,*.vcproj,*.dsp,*.dsw,*.pro,*.pri,*.qbs,*.cmake,*.am,*.in,*.m4,*.ac,*.mk,*.mak,*.ninja,*.gyp,*.gypi,*.gn,*.gni,*.bp,*.mm,*.pch,*.plist,*.entitlements,*.storyboard,*.xib,*.nib,*.strings,*.stringsdict,*.lproj,*.bundle,*.framework,*.dylib,*.so,*.dll,*.lib,*.a,*.o,*.obj,*.exe,*.app,*.deb,*.rpm,*.dmg,*.pkg,*.msi,*.tar.gz,*.tgz,*.tar.bz2,*.tbz2,*.tar.xz,*.txz,*.zip,*.7z,*.rar,*.iso,*.img,*.bin,*.hex,*.elf,*.out,*.com,*.bat,*.cmd,*.ps1,*.psm1,*.psd1,*.ps1xml,*.pssc,*.psrc,*.cdxml}", - "**/*.txt", - "**/*.log", - "**/README*", - "**/LICENSE*", - "**/CHANGELOG*", - "**/CONTRIBUTING*", - "**/.gitignore", - "**/.env*", - ]; + // Initialize cache once + const initializeCache = async () => { + if (cacheInitialized || initializingRef.current) { + return; + } + + initializingRef.current = true; + + try { + // Use a single, more efficient pattern + const patterns = [ + "**/*.{ts,tsx,js,jsx,py,java,cpp,c,h,hpp,cs,go,rs,rb,php,swift,kt,scala,md,json,yaml,yml,xml,html,css,scss,sass,less,sql,sh,dockerfile,makefile,cmake,gradle,toml,ini,env,txt,log}", + "**/README*", + "**/LICENSE*", + "**/CHANGELOG*", + "**/package.json", + "**/Cargo.toml", + "**/pyproject.toml", + "**/composer.json", + "**/Gemfile", + "**/.gitignore", + "**/.env*", + ]; - const allFiles: string[] = []; - - for (const pattern of patterns) { - const matches = await glob(pattern, { + const allMatches: string[] = []; + + // Use Promise.all for parallel glob searches + const results = await Promise.all( + patterns.map(pattern => + glob(pattern, { ignore: [ "**/node_modules/**", "**/dist/**", @@ -90,81 +105,111 @@ const FileSearchUI: React.FC = ({ ], dot: true, absolute: false, - }); - allFiles.push(...matches); - } + }) + ) + ); - // Remove duplicates and sort - const uniqueFiles = [...new Set(allFiles)]; + // Flatten and deduplicate results + for (const matches of results) { + allMatches.push(...matches); + } + + const uniqueFiles = [...new Set(allMatches)]; + + allFiles = uniqueFiles.map((path) => ({ + path, + displayName: path, + })); + + cacheInitialized = true; + } catch (error) { + console.error("Error initializing file cache:", error); + } finally { + initializingRef.current = false; + } + }; + + // Filter files from cache + const filterFiles = (filterText: string): FileItem[] => { + if (!cacheInitialized) { + return []; + } + + if (filterText.length === 0) { + // Show most recently modified files when no filter + return allFiles + .sort((a, b) => { + const aFileName = a.path.split('/').pop() || a.path; + const bFileName = b.path.split('/').pop() || b.path; + return aFileName.localeCompare(bFileName); + }) + .slice(0, 10); + } + + const lowerFilter = filterText.toLowerCase(); + + // Check cache first + const cacheKey = lowerFilter; + if (fileCache.has(cacheKey)) { + return fileCache.get(cacheKey)!; + } + + const filteredFiles = allFiles + .filter((file) => { + return ( + file.displayName.toLowerCase().includes(lowerFilter) || + file.path.toLowerCase().includes(lowerFilter) + ); + }) + .sort((a, b) => { + const aFileName = a.path.split('/').pop() || a.path; + const bFileName = b.path.split('/').pop() || b.path; - const fileItems: FileItem[] = uniqueFiles.map((path) => { - // Show the full path from workspace root - const displayName = path; - - return { - path, - displayName, - }; - }); + // Prioritize exact matches in file name + const aNameMatch = aFileName.toLowerCase().includes(lowerFilter); + const bNameMatch = bFileName.toLowerCase().includes(lowerFilter); + + if (aNameMatch && !bNameMatch) return -1; + if (!aNameMatch && bNameMatch) return 1; + + // Then prioritize files that start with the filter + const aStartsWith = aFileName.toLowerCase().startsWith(lowerFilter); + const bStartsWith = bFileName.toLowerCase().startsWith(lowerFilter); + + if (aStartsWith && !bStartsWith) return -1; + if (!aStartsWith && bStartsWith) return 1; + + // Finally, sort by file name + return aFileName.localeCompare(bFileName); + }) + .slice(0, 10); - // Filter files based on the current filter - const filteredFiles = fileItems - .filter((file) => { - if (filter.length === 0) { - return true; // Show all files when no filter - } - const lowerFilter = filter.toLowerCase(); - return ( - file.displayName.toLowerCase().includes(lowerFilter) || - file.path.toLowerCase().includes(lowerFilter) - ); - }) - .sort((a, b) => { - if (filter.length === 0) { - // When no filter, sort by file name (not full path) - const aFileName = a.path.split('/').pop() || a.path; - const bFileName = b.path.split('/').pop() || b.path; - return aFileName.localeCompare(bFileName); - } - - const lowerFilter = filter.toLowerCase(); - - // Get file names for comparison - const aFileName = a.path.split('/').pop() || a.path; - const bFileName = b.path.split('/').pop() || b.path; - - // Prioritize exact matches in file name - const aNameMatch = aFileName.toLowerCase().includes(lowerFilter); - const bNameMatch = bFileName.toLowerCase().includes(lowerFilter); - - if (aNameMatch && !bNameMatch) return -1; - if (!aNameMatch && bNameMatch) return 1; - - // Then prioritize files that start with the filter - const aStartsWith = aFileName.toLowerCase().startsWith(lowerFilter); - const bStartsWith = bFileName.toLowerCase().startsWith(lowerFilter); - - if (aStartsWith && !bStartsWith) return -1; - if (!aStartsWith && bStartsWith) return 1; - - // Finally, sort by file name - return aFileName.localeCompare(bFileName); - }) - .slice(0, 10); // Limit to 10 results for screen space + // Cache the result + fileCache.set(cacheKey, filteredFiles); + + // Limit cache size + if (fileCache.size > 100) { + const firstKey = fileCache.keys().next().value; + fileCache.delete(firstKey); + } - setFiles(filteredFiles); - if (onFilesUpdated) { - onFilesUpdated(filteredFiles); - } - } catch (error) { - console.error("Error searching files:", error); - setFiles([]); - if (onFilesUpdated) { - onFilesUpdated([]); - } - } finally { + return filteredFiles; + }; + + useEffect(() => { + const searchFiles = async () => { + if (!cacheInitialized) { + setIsLoading(true); + await initializeCache(); setIsLoading(false); } + + const filteredFiles = filterFiles(filter); + setFiles(filteredFiles); + + if (onFilesUpdated) { + onFilesUpdated(filteredFiles); + } }; searchFiles(); @@ -173,7 +218,7 @@ const FileSearchUI: React.FC = ({ if (isLoading) { return ( - Searching files... + Indexing files... ); }