Ignore dot files on @-completion. (#978)

This commit is contained in:
DeWitt Clinton 2025-06-12 10:04:15 -07:00 committed by GitHub
parent af247a6cbd
commit a9e56ee460
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 2 deletions

View File

@ -241,7 +241,7 @@ describe('useCompletion git-aware filtering integration', () => {
expect(mockFileDiscoveryService.glob).toHaveBeenCalledWith('**/s*', {
cwd: testCwd,
dot: true,
dot: false,
});
expect(fs.readdir).not.toHaveBeenCalled(); // Ensure glob is used instead of readdir
expect(result.current.suggestions).toEqual([
@ -249,4 +249,32 @@ describe('useCompletion git-aware filtering integration', () => {
{ label: 'src/index.ts', value: 'src/index.ts' },
]);
});
it('should include dotfiles in glob search when input starts with a dot', async () => {
const globResults = [
`${testCwd}/.env`,
`${testCwd}/.gitignore`,
`${testCwd}/src/index.ts`,
];
mockFileDiscoveryService.glob.mockResolvedValue(globResults);
const { result } = renderHook(() =>
useCompletion('@.', testCwd, true, slashCommands, mockConfig),
);
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 150));
});
expect(mockFileDiscoveryService.glob).toHaveBeenCalledWith('**/.*', {
cwd: testCwd,
dot: true,
});
expect(fs.readdir).not.toHaveBeenCalled();
expect(result.current.suggestions).toEqual([
{ label: '.env', value: '.env' },
{ label: '.gitignore', value: '.gitignore' },
{ label: 'src/index.ts', value: 'src/index.ts' },
]);
});
});

View File

@ -210,6 +210,11 @@ export function useCompletion(
path.join(startDir, entry.name),
);
// Conditionally ignore dotfiles
if (!searchPrefix.startsWith('.') && entry.name.startsWith('.')) {
continue;
}
// Check if this entry should be ignored by git-aware filtering
if (
fileDiscovery &&
@ -260,7 +265,7 @@ export function useCompletion(
const globPattern = `**/${searchPrefix}*`;
const files = await fileDiscoveryService.glob(globPattern, {
cwd,
dot: true,
dot: searchPrefix.startsWith('.'),
});
const suggestions: Suggestion[] = files
@ -309,6 +314,10 @@ export function useCompletion(
// Filter entries using git-aware filtering
const filteredEntries = [];
for (const entry of entries) {
// Conditionally ignore dotfiles
if (!prefix.startsWith('.') && entry.name.startsWith('.')) {
continue;
}
if (!entry.name.toLowerCase().startsWith(lowerPrefix)) continue;
const relativePath = path.relative(