@@ -202,14 +202,17 @@ function matchToken(
202202) : TokenMatch | null {
203203 if ( token . length === 0 ) return null ;
204204 let best : TokenMatch | null = null ;
205- const ownBoundary = ! ALPHANUMERIC . test ( token [ 0 ] ! ) && ! SYMBOL . test ( token [ 0 ] ! ) ;
205+ let ownBoundary : boolean | undefined ;
206206 for (
207207 let index = field . text . indexOf ( token ) ;
208208 index !== - 1 ;
209209 index = field . text . indexOf ( token , index + 1 )
210210 ) {
211211 // A token that opens with punctuation (".ts") brings its own boundary, as in messages.
212- if ( field . wordStarts [ index ] || ownBoundary ) {
212+ if (
213+ field . wordStarts [ index ] ||
214+ ( ownBoundary ??= ! ALPHANUMERIC . test ( token [ 0 ] ! ) && ! SYMBOL . test ( token [ 0 ] ! ) )
215+ ) {
213216 // Whole when the match ends where a word ends: "t3c" is a prefix of
214217 // "t3code", ".ts" whole in "index.ts" but a prefix in "parser.tsx".
215218 const end = index + token . length ;
@@ -275,9 +278,12 @@ function matchToken(
275278 * Allow them at its start ("t3co"), before three letters ("gomod"), or a whole word ("vscode").
276279 */
277280function matchJoinedWords ( field : SearchField , token : string ) : TokenMatch | null {
278- const firstWord = field . wordStarts . indexOf ( 1 ) ;
279- for ( let start = 0 ; start < field . text . length ; start ++ ) {
280- if ( ! field . wordStarts [ start ] || field . text [ start ] !== token [ 0 ] ) continue ;
281+ for (
282+ let start = field . text . indexOf ( token [ 0 ] ! ) ;
283+ start !== - 1 ;
284+ start = field . text . indexOf ( token [ 0 ] ! , start + 1 )
285+ ) {
286+ if ( ! field . wordStarts [ start ] ) continue ;
281287 const runs : SearchRange [ ] = [ ] ;
282288 let at = start ;
283289 let rest = token ;
@@ -300,7 +306,7 @@ function matchJoinedWords(field: SearchField, token: string): TokenMatch | null
300306 if (
301307 last . end === wordEnd ( field , last . start ) ||
302308 last . end - last . start >= 3 ||
303- start === firstWord ||
309+ start === field . wordStarts . indexOf ( 1 ) ||
304310 runs . slice ( 0 , - 1 ) . every ( ( run ) => run . end - run . start >= 3 )
305311 ) {
306312 return { score : JOINED_WORDS , start, end : last . end , letters : runs } ;
@@ -311,6 +317,8 @@ function matchJoinedWords(field: SearchField, token: string): TokenMatch | null
311317
312318interface WeightedSearchField {
313319 readonly value : string ;
320+ /** `value` folded; set on first use, so fields kept across keystrokes fold once. */
321+ prepared ?: SearchField ;
314322 readonly weight : number ;
315323 /** Only a whole-word match counts, as for a short query against keywords. */
316324 readonly wholeWords ?: boolean ;
@@ -342,6 +350,10 @@ interface SearchFieldsMatch {
342350 readonly primaryRanges : ReadonlyArray < SearchRange > ;
343351}
344352
353+ function preparedField ( weighted : WeightedSearchField ) : SearchField {
354+ return ( weighted . prepared ??= prepareSearchField ( weighted . value ) ) ;
355+ }
356+
345357const FILLER_WORDS = new Set ( [ "a" , "an" , "and" , "for" , "in" , "of" , "on" , "the" , "to" , "with" ] ) ;
346358
347359// Sorted, with overlapping ranges joined.
@@ -358,6 +370,25 @@ function mergeRanges(ranges: ReadonlyArray<SearchRange>): SearchRange[] {
358370 return merged ;
359371}
360372
373+ // A keystroke scores every thread for one query, so its tokens are split once.
374+ let lastQuery :
375+ | { readonly query : string ; readonly tokens : string [ ] ; readonly fillerOptional : boolean }
376+ | undefined ;
377+
378+ function queryTokens ( normalizedQuery : string ) {
379+ if ( lastQuery ?. query !== normalizedQuery ) {
380+ const tokens = normalizedQuery . split ( " " ) . filter ( ( token ) => token . length > 0 ) ;
381+ // A phrase-style query ("fix the sidebar") carries filler words a name may
382+ // leave out; they count where they match but are not required.
383+ lastQuery = {
384+ query : normalizedQuery ,
385+ tokens,
386+ fillerOptional : tokens . some ( ( token ) => ! FILLER_WORDS . has ( token ) ) ,
387+ } ;
388+ }
389+ return lastQuery ;
390+ }
391+
361392/**
362393 * Every query token must match some field. Quality is the mean of each token's
363394 * best weighted match, plus a bonus when the first field holds the whole query
@@ -369,23 +400,10 @@ function matchSearchFields(
369400 options ?: { readonly typos ?: boolean } ,
370401) : SearchFieldsMatch | null {
371402 if ( normalizedQuery . length === 0 ) return null ;
372- const prepared = fields
373- . filter ( ( field ) => field . value . length > 0 )
374- . map ( ( field ) => ( {
375- field : prepareSearchField ( field . value ) ,
376- weight : field . weight ,
377- wholeWords : field . wholeWords ?? false ,
378- keyword : field . keyword ?? false ,
379- context : field . context ?? false ,
380- allowMidWord : field . midWord ?? false ,
381- } ) ) ;
382- const primary = fields [ 0 ] ?. value ? prepared [ 0 ] ?. field : undefined ;
403+ const primary = fields [ 0 ] ?. value ? preparedField ( fields [ 0 ] ) : undefined ;
383404 // An empty token would match everywhere, and loop forever in `matchToken`.
384- const tokens = normalizedQuery . split ( " " ) . filter ( ( token ) => token . length > 0 ) ;
405+ const { tokens, fillerOptional } = queryTokens ( normalizedQuery ) ;
385406 if ( tokens . length === 0 ) return null ;
386- // A phrase-style query ("fix the sidebar") carries filler words a name may
387- // leave out; they count where they match but are not required.
388- const fillerOptional = tokens . some ( ( token ) => ! FILLER_WORDS . has ( token ) ) ;
389407 const primaryRanges : SearchRange [ ] = [ ] ;
390408 let total = 0 ;
391409 let counted = 0 ;
@@ -394,11 +412,14 @@ function matchSearchFields(
394412 let best = 0 ;
395413 let bestPrimary : TokenMatch | null = null ;
396414 let atWordStart = false ;
397- for ( const { field, weight, wholeWords, keyword, context, allowMidWord } of prepared ) {
415+ for ( const weighted of fields ) {
416+ if ( weighted . value . length === 0 ) continue ;
417+ const { weight, wholeWords, keyword, context, midWord } = weighted ;
418+ const field = preparedField ( weighted ) ;
398419 if ( best >= weight && field !== primary && atWordStart ) continue ;
399420 const typos = ( options ?. typos ?? false ) && ! keyword ;
400421 // Joined words only count in the name.
401- const match = matchToken ( field , token , typos , field === primary , allowMidWord ) ;
422+ const match = matchToken ( field , token , typos , field === primary , midWord ?? false ) ;
402423 if ( match === null || match . score < ( wholeWords ? WHOLE_WORD : keyword ? 0.7 : 0 ) ) continue ;
403424 if ( field === primary ) bestPrimary = match ;
404425 if ( match . score >= 0.7 && ! context ) atWordStart = true ;
@@ -801,29 +822,28 @@ function threadUsageBoost(thread: ThreadSearchSubject, now: number): number {
801822
802823const REFERENCE_PREFIX_MIN = 8 ;
803824
825+ // A number is never cut short: ".../pull/2288" is not pull request 22889.
826+ const notCut = ( text : string , end : number ) => ! DIGIT . test ( text [ end ] ?? "" ) ;
827+
804828// IDs and URLs have many word starts, so field matching would let short queries hit every thread.
805829// Require a long prefix, or any long part containing a digit. Both are ASCII; lowercasing suffices.
806830function matchReference (
807831 normalizedQuery : string ,
808832 thread : ThreadSearchSubject ,
809833) : SearchFieldsMatch | null {
810834 if ( normalizedQuery . length < REFERENCE_PREFIX_MIN ) return null ;
811- const references = [
812- ...( thread . id ? [ thread . id ] : [ ] ) ,
813- ...( thread . pullRequests ?? [ ] ) . flatMap ( ( pullRequest ) => pullRequest . references ?? [ ] ) ,
814- ] ;
815835 const anywhere = DIGIT . test ( normalizedQuery ) ;
816- // A number is never cut short: ".../pull/2288" is not pull request 22889.
817- const notCut = ( text : string , end : number ) => ! DIGIT . test ( text [ end ] ?? "" ) ;
818- return references . some ( ( reference ) => {
836+ const matches = ( reference : string ) => {
819837 const folded = reference . toLowerCase ( ) ;
820838 const at = folded . indexOf ( normalizedQuery ) ;
821839 // A pasted link may go on past the reference: ".../pull/22889/files".
822840 return anywhere
823841 ? ( at !== - 1 && notCut ( folded , at + normalizedQuery . length ) ) ||
824842 ( normalizedQuery . startsWith ( folded ) && notCut ( normalizedQuery , folded . length ) )
825843 : at === 0 ;
826- } )
844+ } ;
845+ return ( thread . id && matches ( thread . id ) ) ||
846+ thread . pullRequests ?. some ( ( pullRequest ) => pullRequest . references ?. some ( matches ) )
827847 ? { quality : 0.6 , strong : true , primaryRanges : [ ] }
828848 : null ;
829849}
@@ -934,6 +954,45 @@ function matchHint(
934954 return rest && rest !== normalizedQuery ? hintFor ( rest ) : undefined ;
935955}
936956
957+ // A keystroke scores each thread's fields again, so they are listed once per subject.
958+ const threadFieldsCache = new WeakMap < ThreadSearchSubject , ReadonlyArray < WeightedSearchField > > ( ) ;
959+
960+ function threadSearchFields ( thread : ThreadSearchSubject ) : ReadonlyArray < WeightedSearchField > {
961+ const cached = threadFieldsCache . get ( thread ) ;
962+ if ( cached ) return cached ;
963+ const fields : WeightedSearchField [ ] = [ { value : thread . title , weight : 1 , midWord : true } ] ;
964+ for ( const pullRequest of thread . pullRequests ?? [ ] ) {
965+ fields . push ( { value : `#${ pullRequest . number } ` , weight : 1 } ) ;
966+ if ( pullRequest . title ) fields . push ( { value : pullRequest . title , weight : 0.5 } ) ;
967+ }
968+ const branch = searchableBranch ( thread . branch ) ;
969+ if ( branch ) fields . push ( { value : branch , weight : 0.6 } ) ;
970+ if ( thread . projectTitle ) {
971+ fields . push ( { value : thread . projectTitle , weight : 0.25 , context : true } ) ;
972+ }
973+ if ( thread . environmentLabel ) {
974+ fields . push ( { value : thread . environmentLabel , weight : 0.25 , context : true } ) ;
975+ }
976+ threadFieldsCache . set ( thread , fields ) ;
977+ return fields ;
978+ }
979+
980+ let lastFieldQuery : { readonly query : string ; readonly fieldQuery : string } | undefined ;
981+
982+ // Strip pull request prefixes and the worktree prefix so queries match the stored fields.
983+ function threadFieldQuery ( normalizedQuery : string ) : string {
984+ if ( lastFieldQuery ?. query !== normalizedQuery ) {
985+ lastFieldQuery = {
986+ query : normalizedQuery ,
987+ fieldQuery : normalizedQuery
988+ . replace ( / ^ (?: p r | p u l l r e q u e s t | p u l l ) \s * (? = # ? \d ) / , "" )
989+ . replace ( / ^ # \s * / , "" )
990+ . replace ( WORKTREE_BRANCH_PREFIX_PATTERN , "" ) ,
991+ } ;
992+ }
993+ return lastFieldQuery . fieldQuery ;
994+ }
995+
937996/**
938997 * Scores a thread for a normalized query, or null when it does not match.
939998 * Titles and pull request numbers count fully, pull request titles and
@@ -952,24 +1011,8 @@ export function scoreThreadSearch(
9521011 readonly picks ?: boolean ;
9531012 } ,
9541013) : ThreadSearchMatch | null {
955- const fields : WeightedSearchField [ ] = [ { value : thread . title , weight : 1 , midWord : true } ] ;
956- for ( const pullRequest of thread . pullRequests ?? [ ] ) {
957- fields . push ( { value : `#${ pullRequest . number } ` , weight : 1 } ) ;
958- if ( pullRequest . title ) fields . push ( { value : pullRequest . title , weight : 0.5 } ) ;
959- }
960- const branch = searchableBranch ( thread . branch ) ;
961- if ( branch ) fields . push ( { value : branch , weight : 0.6 } ) ;
962- if ( thread . projectTitle ) {
963- fields . push ( { value : thread . projectTitle , weight : 0.25 , context : true } ) ;
964- }
965- if ( thread . environmentLabel ) {
966- fields . push ( { value : thread . environmentLabel , weight : 0.25 , context : true } ) ;
967- }
968- // Strip pull request prefixes and the worktree prefix so queries match the stored fields.
969- const fieldQuery = normalizedQuery
970- . replace ( / ^ (?: p r | p u l l r e q u e s t | p u l l ) \s * (? = # ? \d ) / , "" )
971- . replace ( / ^ # \s * / , "" )
972- . replace ( WORKTREE_BRANCH_PREFIX_PATTERN , "" ) ;
1014+ const fields = threadSearchFields ( thread ) ;
1015+ const fieldQuery = threadFieldQuery ( normalizedQuery ) ;
9731016 const fieldMatch = matchSearchFields ( fieldQuery , fields , options ) ;
9741017 const referenceMatch = fieldMatch ? null : matchReference ( normalizedQuery , thread ) ;
9751018 const match = fieldMatch ?? referenceMatch ;
@@ -1102,13 +1145,15 @@ export function rankSearchGroups<
11021145 score : ( item : T , options : { readonly typos : boolean } , group : G ) => M | null ,
11031146) : Array < { readonly group : G ; readonly ranked : ReadonlyArray < RankedSearchItem < T , M > > } > {
11041147 const rank = ( typos : boolean ) =>
1105- groups . map ( ( { group, items } ) => ( {
1106- group,
1107- ranked : items . flatMap ( ( item ) => {
1108- const match = score ( item , { typos } , group ) ;
1109- return match === null ? [ ] : [ { item, match } ] ;
1110- } ) ,
1111- } ) ) ;
1148+ groups . map ( ( { group, items } ) => {
1149+ const options = { typos } ;
1150+ const ranked : Array < RankedSearchItem < T , M > > = [ ] ;
1151+ for ( const item of items ) {
1152+ const match = score ( item , options , group ) ;
1153+ if ( match !== null ) ranked . push ( { item, match } ) ;
1154+ }
1155+ return { group, ranked } ;
1156+ } ) ;
11121157 let ranked = rank ( false ) ;
11131158 if ( ! ranked . some ( ( entry ) => entry . ranked . some ( ( { match } ) => match . strong ) ) ) {
11141159 ranked = rank ( true ) ;
0 commit comments