@@ -23,6 +23,32 @@ export type Options = typeof Options.Type
2323export class Service extends Context . Service < Service , Interface > ( ) ( "@opencode/FileSystem/Search" ) { }
2424
2525const REFRESH_INTERVAL = Duration . toMillis ( "10 seconds" )
26+ type Prepared = ReturnType < typeof fuzzysort . prepare >
27+
28+ function emptyIndex ( ) {
29+ return { files : new Map < string , Prepared > ( ) , directories : new Map < string , Prepared > ( ) }
30+ }
31+
32+ function search ( index : ReturnType < typeof emptyIndex > , input : FileSystem . FindInput ) {
33+ const items =
34+ input . type === "file"
35+ ? Array . from ( index . files . values ( ) )
36+ : input . type === "directory"
37+ ? Array . from ( index . directories . values ( ) )
38+ : [ ...index . files . values ( ) , ...index . directories . values ( ) ]
39+ const result = fuzzysort . go ( input . query , items , { limit : input . limit ?? 50 } )
40+ // Targets are owned by the current location index. The only global fuzzysort
41+ // state left is its query cache, which must not retain every query forever.
42+ fuzzysort . cleanup ( )
43+ return result . map ( ( item ) => {
44+ const relative = item . target
45+ const type = relative . endsWith ( path . sep ) ? ( "directory" as const ) : ( "file" as const )
46+ return FileSystem . Entry . make ( {
47+ path : RelativePath . make ( relative ) ,
48+ type,
49+ } )
50+ } )
51+ }
2652
2753export const ripgrepLayer = Layer . effect (
2854 Service ,
@@ -32,12 +58,13 @@ export const ripgrepLayer = Layer.effect(
3258 const scope = yield * Scope . Scope
3359 const clock = yield * Clock . Clock
3460 const home = Protected . isHome ( location . directory )
35- let index = { files : [ ] as string [ ] , directories : new Set < string > ( ) }
61+ let index = emptyIndex ( )
3662 let initialized = false
3763 let settledAt = Number . NEGATIVE_INFINITY
3864 let refreshing = false
3965 const scan = Effect . gen ( function * ( ) {
40- const next = { files : [ ] as string [ ] , directories : new Set < string > ( ) }
66+ const next = emptyIndex ( )
67+ const previous = index
4168 if ( ! initialized ) index = next
4269 yield * ripgrep . find ( {
4370 cwd : location . directory ,
@@ -46,11 +73,13 @@ export const ripgrepLayer = Layer.effect(
4673 exclude : home ? [ ...Protected . names ( ) ] . map ( ( name ) => `${ name } /**` ) : undefined ,
4774 onEntry : ( entry ) =>
4875 Effect . sync ( ( ) => {
49- next . files . push ( entry . path )
76+ next . files . set ( entry . path , previous . files . get ( entry . path ) ?? fuzzysort . prepare ( entry . path ) )
5077 const parts = entry . path . split ( "/" )
51- parts
52- . slice ( 0 , - 1 )
53- . forEach ( ( _ , offset ) => next . directories . add ( parts . slice ( 0 , offset + 1 ) . join ( "/" ) + path . sep ) )
78+ parts . slice ( 0 , - 1 ) . forEach ( ( _ , offset ) => {
79+ const directory = parts . slice ( 0 , offset + 1 ) . join ( "/" ) + path . sep
80+ if ( ! next . directories . has ( directory ) )
81+ next . directories . set ( directory , previous . directories . get ( directory ) ?? fuzzysort . prepare ( directory ) )
82+ } )
5483 } ) ,
5584 } )
5685 index = next
@@ -74,20 +103,7 @@ export const ripgrepLayer = Layer.effect(
74103 find : ( input ) =>
75104 Effect . gen ( function * ( ) {
76105 yield * refresh
77- const items =
78- input . type === "file"
79- ? index . files
80- : input . type === "directory"
81- ? Array . from ( index . directories )
82- : [ ...index . files , ...index . directories ]
83- return fuzzysort . go ( input . query , items , { limit : input . limit ?? 50 } ) . map ( ( item ) => {
84- const relative = item . target
85- const type = relative . endsWith ( path . sep ) ? ( "directory" as const ) : ( "file" as const )
86- return FileSystem . Entry . make ( {
87- path : RelativePath . make ( relative ) ,
88- type,
89- } )
90- } )
106+ return search ( index , input )
91107 } ) ,
92108 } )
93109 } ) ,
0 commit comments