An open source design system that's fully customizable and agent ready
TypeScript
cli: suggest a component named inside a longer name (#7975)
Joey6e4e335fa144
diff --git a/.changeset/lookup-suggestion-recall.md b/.changeset/lookup-suggestion-recall.mdnew file mode 100644index 0000000000..4fcb8ba61d--- /dev/null+++ b/.changeset/lookup-suggestion-recall.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx component <name>` now suggests a component whose whole name is part of the typed name (a wrapper or prefixed name) when nothing else matches, instead of no suggestions.++@josephfarinadiff --git a/packages/cli/foundation/text/string-utils.mjs b/packages/cli/foundation/text/string-utils.mjsindex e3be844a84..a17b87366a 100644--- a/packages/cli/foundation/text/string-utils.mjs+++ b/packages/cli/foundation/text/string-utils.mjs@@ -209,6 +209,39 @@ export async function searchComponents(needle, coreDir, components) {}}+ // --- Pass 3: a name made of other words around a component's name ---+ // Only when nothing above matched: a contiguous run of the needle's words+ // ("Primitive Button", "Meta Employee Typeahead") that is a component's+ // whole name. Longer runs and runs nearer the end score higher.+ if (scored.size === 0) {+ const words = String(needle)+ .replace(/([a-z0-9])([A-Z])/g, '$1 $2')+ .replace(/([A-Z])([A-Z][a-z])/g, '$1 $2')+ .toLowerCase()+ .split(/[^a-z0-9]+/)+ .filter(Boolean);+ const byLower = new Map(allNames.map(n => [n.toLowerCase(), n]));+ const longest = Math.max(0, ...allNames.map(n => n.length));+ for (let i = 0; i < words.length; i++) {+ let run = '';+ for (let j = i + 1; j <= words.length; j++) {+ // Grow the run one word at a time; stop once no name is that long.+ run += words[j - 1];+ if (run.length > longest) break;+ if (run.length < 4 || run === term) continue;+ const comp = byLower.get(run);+ if (!comp) continue;+ const span = j - i;+ const atEnd = j === words.length ? 1 : 0;+ addMatch(comp, 10 + Math.min(span, 4) + atEnd, 'name part "' + run + '"');+ }+ }+ // Keep only the strongest name parts, so a weaker part never sits+ // beside the best one.+ const best = Math.max(0, ...[...scored.values()].map(r => r.score));+ for (const [name, r] of scored) if (r.score < best) scored.delete(name);+ }+const results = Array.from(scored.values()).map(r =>keywordAt.has(r.name)diff --git a/packages/cli/foundation/text/string-utils.test.mjs b/packages/cli/foundation/text/string-utils.test.mjsindex 7d89981c9f..7fff27712a 100644--- a/packages/cli/foundation/text/string-utils.test.mjs+++ b/packages/cli/foundation/text/string-utils.test.mjs@@ -116,6 +116,23 @@ describe('searchComponents', () => {}});+ it('suggests a component whose whole name is a run of the needle\'s words when nothing else matches', async () => {+ const results = await searchComponents('FancyWidgetPanel', NO_CORE, {+ g: ['Panel', 'Widget', 'Sheet'],+ });+ expect(results.map(r => r.name)).toEqual(['Panel']);+ expect(results[0].reason).toBe('name part "panel"');+ expect(results.every(r => r.score < 20)).toBe(true);+ });++ it('adds no name-part matches when another signal matched', async () => {+ const results = await searchComponents('Panl', NO_CORE, {+ g: ['Panel', 'Sheet'],+ });+ expect(results.map(r => r.name)).toEqual(['Panel']);+ expect(results[0].score).toBe(80);+ });+it('breaks a score tie alphabetically', async () => {// Both names are edit-distance 2 from "bcd" -> both score 40 -> alpha order.const results = await searchComponents('bcd', NO_CORE, {g: ['Xbc', 'Abc']});