11import type { ProjectEntry } from "@t3tools/contracts" ;
2- import { normalizeSearchQuery , scoreQueryMatch } from "@t3tools/shared/searchRanking" ;
32
43export interface FileTreeNode {
54 readonly path : string ;
65 readonly name : string ;
76 readonly kind : ProjectEntry [ "kind" ] ;
87 readonly ignored ?: boolean ;
98 readonly children : ReadonlyArray < FileTreeNode > ;
10- readonly searchSegments : ReadonlyArray < string > ;
11- readonly searchWords : ReadonlyArray < string > ;
129}
1310
1411export interface VisibleFileTreeNode {
@@ -37,43 +34,13 @@ function createMutableNode(
3734 } ;
3835}
3936
40- function splitSearchWords ( value : string ) : ReadonlyArray < string > {
41- return value
42- . replace ( / ( [ A - Z ] + ) ( [ A - Z ] [ a - z ] ) / g, "$1 $2" )
43- . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, "$1 $2" )
44- . split ( / [ ^ A - Z a - z 0 - 9 ] + / )
45- . filter ( Boolean )
46- . map ( ( word ) => word . toLowerCase ( ) ) ;
47- }
48-
49- function buildNodeSearchTerms ( path : string ) : {
50- readonly segments : ReadonlyArray < string > ;
51- readonly words : ReadonlyArray < string > ;
52- } {
53- const segments : string [ ] = [ ] ;
54- const words : string [ ] = [ ] ;
55-
56- for ( const segment of path . split ( "/" ) ) {
57- if ( ! segment ) {
58- continue ;
59- }
60- segments . push ( segment . toLowerCase ( ) ) ;
61- words . push ( ...splitSearchWords ( segment ) ) ;
62- }
63-
64- return { segments, words } ;
65- }
66-
6737function freezeNode ( node : MutableFileTreeNode ) : FileTreeNode {
68- const searchTerms = buildNodeSearchTerms ( node . path ) ;
6938 return {
7039 path : node . path ,
7140 name : node . name ,
7241 kind : node . kind ,
7342 ...( node . ignored ? { ignored : true } : { } ) ,
7443 children : [ ...node . children . values ( ) ] . sort ( compareNodes ) . map ( freezeNode ) ,
75- searchSegments : searchTerms . segments ,
76- searchWords : searchTerms . words ,
7744 } ;
7845}
7946
@@ -133,44 +100,21 @@ export function defaultExpandedTreePaths(nodes: ReadonlyArray<FileTreeNode>): Re
133100 return expanded ;
134101}
135102
136- function valueMatchesSearchToken ( value : string , token : string , fuzzy : boolean ) : boolean {
137- return (
138- scoreQueryMatch ( {
139- value,
140- query : token ,
141- exactBase : 0 ,
142- prefixBase : 2 ,
143- boundaryBase : 4 ,
144- includesBase : 6 ,
145- ...( fuzzy ? { fuzzyBase : 100 } : { } ) ,
146- boundaryMarkers : [ "/" , "-" , "_" , "." ] ,
147- } ) !== null
148- ) ;
149- }
150-
151- function nodeMatchesSearch ( node : FileTreeNode , tokens : ReadonlyArray < string > ) : boolean {
152- return tokens . every (
153- ( token ) =>
154- node . searchSegments . some ( ( segment ) => valueMatchesSearchToken ( segment , token , false ) ) ||
155- node . searchWords . some ( ( word ) => valueMatchesSearchToken ( word , token , true ) ) ,
156- ) ;
157- }
158-
159103function flattenNode (
160104 output : VisibleFileTreeNode [ ] ,
161105 node : FileTreeNode ,
162106 depth : number ,
163107 expanded : ReadonlySet < string > ,
164- searchTokens : ReadonlyArray < string > ,
108+ searchResultPaths : ReadonlySet < string > | null ,
165109) : boolean {
166- const isSearching = searchTokens . length > 0 ;
167- const matches = isSearching && nodeMatchesSearch ( node , searchTokens ) ;
110+ const isSearching = searchResultPaths !== null ;
111+ const matches = searchResultPaths ?. has ( node . path ) ?? false ;
168112 let descendantMatches = false ;
169113 const childOutput : VisibleFileTreeNode [ ] = [ ] ;
170114
171115 if ( node . kind === "directory" && ( expanded . has ( node . path ) || isSearching ) ) {
172116 for ( const child of node . children ) {
173- if ( flattenNode ( childOutput , child , depth + 1 , expanded , searchTokens ) ) {
117+ if ( flattenNode ( childOutput , child , depth + 1 , expanded , searchResultPaths ) ) {
174118 descendantMatches = true ;
175119 }
176120 }
@@ -189,13 +133,12 @@ function flattenNode(
189133export function flattenFileTree ( input : {
190134 readonly nodes : ReadonlyArray < FileTreeNode > ;
191135 readonly expanded : ReadonlySet < string > ;
192- readonly searchQuery ?: string ;
136+ // Null restores normal directory expansion; an empty set is a search with no results.
137+ readonly searchResultPaths ?: ReadonlySet < string > | null ;
193138} ) : ReadonlyArray < VisibleFileTreeNode > {
194139 const output : VisibleFileTreeNode [ ] = [ ] ;
195- const normalizedSearch = normalizeSearchQuery ( input . searchQuery ?? "" ) ;
196- const searchTokens = normalizedSearch . split ( / [ \s / \\ . _ - ] + / ) . filter ( Boolean ) ;
197140 for ( const node of input . nodes ) {
198- flattenNode ( output , node , 0 , input . expanded , searchTokens ) ;
141+ flattenNode ( output , node , 0 , input . expanded , input . searchResultPaths ?? null ) ;
199142 }
200143 return output ;
201144}
0 commit comments