diff --git a/packages/shared/src/paletteSearch.test.ts b/packages/shared/src/paletteSearch.test.ts index c2fcbeba3316..364e34b0a0fa 100644 --- a/packages/shared/src/paletteSearch.test.ts +++ b/packages/shared/src/paletteSearch.test.ts @@ -590,6 +590,14 @@ describe("rankSearchGroups with keywords and picks", () => { scoreThreadSearch("arch", thread("Notes", { branch: "research-extension-system" }), NOW), ).toBeNull(); expect(scoreThreadSearch("arch", thread("Research notes"), NOW)?.quality).toBe(0.3); + // Except a number's last digits in a name, never in a pull request number. + expect(scoreSearchTerms("120", ["33120", "~/courses/33120"])?.quality).toBe(0.3); + expect(scoreThreadSearch("120", thread("33120 notes"), NOW)?.quality).toBe(0.3); + expect(scoreSearchTerms("312", ["33120"])).toBeNull(); + expect(scoreSearchTerms("20", ["33120"])).toBeNull(); + expect( + scoreThreadSearch("189", thread("Stats", { pullRequests: [{ number: 22189 }] }), NOW), + ).toBeNull(); }); it("forgives typos in names but not in hidden keywords", () => { diff --git a/packages/shared/src/paletteSearch.ts b/packages/shared/src/paletteSearch.ts index a2de064ca496..6bfa448a140a 100644 --- a/packages/shared/src/paletteSearch.ts +++ b/packages/shared/src/paletteSearch.ts @@ -188,8 +188,22 @@ interface TokenMatch extends SearchRange { readonly letters?: ReadonlyArray; } +const NUMBER = /^\p{N}+$/u; + +// A name's number ends with what people remember of it: "120" finds course "33120". +function endsNumber(field: SearchField, token: string, index: number): boolean { + const end = index + token.length; + return ( + token.length >= 3 && + NUMBER.test(token) && + DIGIT.test(field.text[index - 1] ?? "") && + !DIGIT.test(field.text[end] ?? "") + ); +} + /** - * Mid-word hits need four or more characters and `midWord`, so "del" does not find "model". + * Mid-word hits need four or more characters and `midWord`, so "del" does not find "model", + * or a name's number ending in the token. * Typos preserve the first letter except for swaps; short tokens allow fewer edits to limit noise. * Numbers never match with a typo because different pull request numbers must stay distinct. */ @@ -227,7 +241,10 @@ function matchToken( best = { score, start: index, end: index + token.length }; } if (score === WHOLE_WORD) return best; - } else if (midWord && token.length >= 4 && best === null) { + } else if ( + best === null && + ((midWord && token.length >= 4) || (isPrimary && endsNumber(field, token, index))) + ) { best = { score: MID_WORD, start: index, end: index + token.length }; } }