Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/ui/hooks/useAtCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ describe('useAtCompletion', () => {

expect(result.current.suggestions.map((s) => s.value)).toEqual([
'src/',
'src/components/',
'src/index.js',
'src/components/',
'src/components/Button.tsx',
]);
});
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/utils/filesearch/fileSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,47 @@ describe('FileSearch', () => {
);
});

it('should prioritize filenames closer to the end of the path and shorter paths', async () => {
tmpDir = await createTmpDir({
src: {
'hooks.ts': '',
hooks: {
'index.ts': '',
},
utils: {
'hooks.tsx': '',
},
'hooks-dev': {
'test.ts': '',
},
},
});

const fileSearch = FileSearchFactory.create({
projectRoot: tmpDir,
fileDiscoveryService: new FileDiscoveryService(tmpDir, {
respectGitIgnore: false,
respectGeminiIgnore: false,
}),
ignoreDirs: [],
cache: false,
cacheTtl: 0,
enableRecursiveFileSearch: true,
enableFuzzySearch: true,
});

await fileSearch.initialize();
const results = await fileSearch.search('hooks');

// The order should prioritize matches closer to the end and shorter strings.
// FZF matches right-to-left.
expect(results[0]).toBe('src/hooks/');
expect(results[1]).toBe('src/hooks.ts');
expect(results[2]).toBe('src/utils/hooks.tsx');
expect(results[3]).toBe('src/hooks-dev/');
expect(results[4]).toBe('src/hooks/index.ts');
expect(results[5]).toBe('src/hooks-dev/test.ts');
});
it('should return empty array when no matches are found', async () => {
tmpDir = await createTmpDir({
src: ['file1.js'],
Expand Down
40 changes: 40 additions & 0 deletions packages/core/src/utils/filesearch/fileSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@ import { AsyncFzf, type FzfResultItem } from 'fzf';
import { unescapePath } from '../paths.js';
import type { FileDiscoveryService } from '../../services/fileDiscoveryService.js';

// Tiebreaker: Prefers shorter paths.
const byLengthAsc = (a: { item: string }, b: { item: string }) =>
a.item.length - b.item.length;

// Tiebreaker: Prefers matches at the start of the filename (basename prefix).
const byBasenamePrefix = (
a: { item: string; positions: Set<number> },
b: { item: string; positions: Set<number> },
) => {
const getBasenameStart = (p: string) => {
const trimmed = p.endsWith('/') ? p.slice(0, -1) : p;
return Math.max(trimmed.lastIndexOf('/'), trimmed.lastIndexOf('\\')) + 1;
};
const aDiff = Math.min(...a.positions) - getBasenameStart(a.item);
const bDiff = Math.min(...b.positions) - getBasenameStart(b.item);

const aIsFilenameMatch = aDiff >= 0;
const bIsFilenameMatch = bDiff >= 0;

if (aIsFilenameMatch && !bIsFilenameMatch) return -1;
if (!aIsFilenameMatch && bIsFilenameMatch) return 1;
if (aIsFilenameMatch && bIsFilenameMatch) return aDiff - bDiff;

return 0; // Both are directory matches, let subsequent tiebreakers decide.
};

// Tiebreaker: Prefers matches closer to the end of the path.
const byMatchPosFromEnd = (
a: { item: string; positions: Set<number> },
b: { item: string; positions: Set<number> },
) => {
const maxPosA = Math.max(-1, ...a.positions);
const maxPosB = Math.max(-1, ...b.positions);
const distA = a.item.length - maxPosA;
const distB = b.item.length - maxPosB;
return distA - distB;
};

export interface FileSearchOptions {
projectRoot: string;
ignoreDirs: string[];
Expand Down Expand Up @@ -192,6 +230,8 @@ class RecursiveFileSearch implements FileSearch {
// files, because the v2 algorithm is just too slow in those cases.
this.fzf = new AsyncFzf(this.allFiles, {
fuzzy: this.allFiles.length > 20000 ? 'v1' : 'v2',
forward: false,
tiebreakers: [byBasenamePrefix, byMatchPosFromEnd, byLengthAsc],
});
}
}
Expand Down
Loading