mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 12:54:07 -07:00
feat(cli): include /dir add directories in @ autocomplete suggestions (#19246)
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
||||||
import { act, useState } from 'react';
|
import { act, useState } from 'react';
|
||||||
|
import * as path from 'node:path';
|
||||||
import { renderHook } from '../../test-utils/render.js';
|
import { renderHook } from '../../test-utils/render.js';
|
||||||
import { waitFor } from '../../test-utils/async.js';
|
import { waitFor } from '../../test-utils/async.js';
|
||||||
import { useAtCompletion } from './useAtCompletion.js';
|
import { useAtCompletion } from './useAtCompletion.js';
|
||||||
@@ -589,4 +590,120 @@ describe('useAtCompletion', () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Multi-directory workspace support', () => {
|
||||||
|
const multiDirTmpDirs: string[] = [];
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await Promise.all(multiDirTmpDirs.map((dir) => cleanupTmpDir(dir)));
|
||||||
|
multiDirTmpDirs.length = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should include files from workspace directories beyond cwd', async () => {
|
||||||
|
const cwdStructure: FileSystemStructure = { 'main.txt': '' };
|
||||||
|
const addedDirStructure: FileSystemStructure = { 'added-file.txt': '' };
|
||||||
|
const cwdDir = await createTmpDir(cwdStructure);
|
||||||
|
multiDirTmpDirs.push(cwdDir);
|
||||||
|
const addedDir = await createTmpDir(addedDirStructure);
|
||||||
|
multiDirTmpDirs.push(addedDir);
|
||||||
|
|
||||||
|
const multiDirConfig = {
|
||||||
|
...mockConfig,
|
||||||
|
getWorkspaceContext: vi.fn().mockReturnValue({
|
||||||
|
getDirectories: () => [cwdDir, addedDir],
|
||||||
|
onDirectoriesChanged: vi.fn(() => () => {}),
|
||||||
|
}),
|
||||||
|
} as unknown as Config;
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useTestHarnessForAtCompletion(true, '', multiDirConfig, cwdDir),
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const values = result.current.suggestions.map((s) => s.value);
|
||||||
|
expect(values).toContain('main.txt');
|
||||||
|
expect(values).toContain(
|
||||||
|
escapePath(path.join(addedDir, 'added-file.txt')),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pick up newly added directories via onDirectoriesChanged', async () => {
|
||||||
|
const cwdStructure: FileSystemStructure = { 'original.txt': '' };
|
||||||
|
const addedStructure: FileSystemStructure = { 'new-file.txt': '' };
|
||||||
|
const cwdDir = await createTmpDir(cwdStructure);
|
||||||
|
multiDirTmpDirs.push(cwdDir);
|
||||||
|
const addedDir = await createTmpDir(addedStructure);
|
||||||
|
multiDirTmpDirs.push(addedDir);
|
||||||
|
|
||||||
|
let dirChangeListener: (() => void) | null = null;
|
||||||
|
const directories = [cwdDir];
|
||||||
|
|
||||||
|
const dynamicConfig = {
|
||||||
|
...mockConfig,
|
||||||
|
getWorkspaceContext: vi.fn().mockReturnValue({
|
||||||
|
getDirectories: () => [...directories],
|
||||||
|
onDirectoriesChanged: vi.fn((listener: () => void) => {
|
||||||
|
dirChangeListener = listener;
|
||||||
|
return () => {
|
||||||
|
dirChangeListener = null;
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
} as unknown as Config;
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useTestHarnessForAtCompletion(true, '', dynamicConfig, cwdDir),
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const values = result.current.suggestions.map((s) => s.value);
|
||||||
|
expect(values).toContain('original.txt');
|
||||||
|
expect(values.every((v) => !v.includes('new-file.txt'))).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
directories.push(addedDir);
|
||||||
|
act(() => {
|
||||||
|
dirChangeListener?.();
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const values = result.current.suggestions.map((s) => s.value);
|
||||||
|
expect(values).toContain(
|
||||||
|
escapePath(path.join(addedDir, 'new-file.txt')),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show same-named files from different directories without false deduplication', async () => {
|
||||||
|
const dir1Structure: FileSystemStructure = { 'readme.md': '' };
|
||||||
|
const dir2Structure: FileSystemStructure = { 'readme.md': '' };
|
||||||
|
const dir1 = await createTmpDir(dir1Structure);
|
||||||
|
multiDirTmpDirs.push(dir1);
|
||||||
|
const dir2 = await createTmpDir(dir2Structure);
|
||||||
|
multiDirTmpDirs.push(dir2);
|
||||||
|
|
||||||
|
const multiDirConfig = {
|
||||||
|
...mockConfig,
|
||||||
|
getWorkspaceContext: vi.fn().mockReturnValue({
|
||||||
|
getDirectories: () => [dir1, dir2],
|
||||||
|
onDirectoriesChanged: vi.fn(() => () => {}),
|
||||||
|
}),
|
||||||
|
} as unknown as Config;
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useTestHarnessForAtCompletion(true, 'readme', multiDirConfig, dir1),
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const values = result.current.suggestions.map((s) => s.value);
|
||||||
|
const readmeEntries = values.filter((v) => v.includes('readme.md'));
|
||||||
|
expect(readmeEntries.length).toBe(2);
|
||||||
|
expect(readmeEntries).toContain('readme.md');
|
||||||
|
expect(readmeEntries).toContain(
|
||||||
|
escapePath(path.join(dir2, 'readme.md')),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
import { useEffect, useReducer, useRef } from 'react';
|
import { useEffect, useReducer, useRef } from 'react';
|
||||||
import { setTimeout as setTimeoutPromise } from 'node:timers/promises';
|
import { setTimeout as setTimeoutPromise } from 'node:timers/promises';
|
||||||
|
import * as path from 'node:path';
|
||||||
import type { Config, FileSearch } from '@google/gemini-cli-core';
|
import type { Config, FileSearch } from '@google/gemini-cli-core';
|
||||||
import {
|
import {
|
||||||
FileSearchFactory,
|
FileSearchFactory,
|
||||||
@@ -203,7 +204,8 @@ export function useAtCompletion(props: UseAtCompletionProps): void {
|
|||||||
setIsLoadingSuggestions,
|
setIsLoadingSuggestions,
|
||||||
} = props;
|
} = props;
|
||||||
const [state, dispatch] = useReducer(atCompletionReducer, initialState);
|
const [state, dispatch] = useReducer(atCompletionReducer, initialState);
|
||||||
const fileSearch = useRef<FileSearch | null>(null);
|
const fileSearchMap = useRef<Map<string, FileSearch>>(new Map());
|
||||||
|
const initEpoch = useRef(0);
|
||||||
const searchAbortController = useRef<AbortController | null>(null);
|
const searchAbortController = useRef<AbortController | null>(null);
|
||||||
const slowSearchTimer = useRef<NodeJS.Timeout | null>(null);
|
const slowSearchTimer = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
@@ -215,10 +217,26 @@ export function useAtCompletion(props: UseAtCompletionProps): void {
|
|||||||
setIsLoadingSuggestions(state.isLoading);
|
setIsLoadingSuggestions(state.isLoading);
|
||||||
}, [state.isLoading, setIsLoadingSuggestions]);
|
}, [state.isLoading, setIsLoadingSuggestions]);
|
||||||
|
|
||||||
useEffect(() => {
|
const resetFileSearchState = () => {
|
||||||
|
fileSearchMap.current.clear();
|
||||||
|
initEpoch.current += 1;
|
||||||
dispatch({ type: 'RESET' });
|
dispatch({ type: 'RESET' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
resetFileSearchState();
|
||||||
}, [cwd, config]);
|
}, [cwd, config]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const workspaceContext = config?.getWorkspaceContext?.();
|
||||||
|
if (!workspaceContext) return;
|
||||||
|
|
||||||
|
const unsubscribe =
|
||||||
|
workspaceContext.onDirectoriesChanged(resetFileSearchState);
|
||||||
|
|
||||||
|
return unsubscribe;
|
||||||
|
}, [config]);
|
||||||
|
|
||||||
// Reacts to user input (`pattern`) ONLY.
|
// Reacts to user input (`pattern`) ONLY.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
@@ -250,38 +268,64 @@ export function useAtCompletion(props: UseAtCompletionProps): void {
|
|||||||
// The "Worker" that performs async operations based on status.
|
// The "Worker" that performs async operations based on status.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initialize = async () => {
|
const initialize = async () => {
|
||||||
|
const currentEpoch = initEpoch.current;
|
||||||
try {
|
try {
|
||||||
const searcher = FileSearchFactory.create({
|
const directories = config
|
||||||
projectRoot: cwd,
|
?.getWorkspaceContext?.()
|
||||||
ignoreDirs: [],
|
?.getDirectories() ?? [cwd];
|
||||||
fileDiscoveryService: new FileDiscoveryService(
|
|
||||||
cwd,
|
const initPromises: Array<Promise<void>> = [];
|
||||||
config?.getFileFilteringOptions(),
|
|
||||||
),
|
for (const dir of directories) {
|
||||||
cache: true,
|
if (fileSearchMap.current.has(dir)) continue;
|
||||||
cacheTtl: 30, // 30 seconds
|
|
||||||
enableRecursiveFileSearch:
|
const searcher = FileSearchFactory.create({
|
||||||
config?.getEnableRecursiveFileSearch() ?? true,
|
projectRoot: dir,
|
||||||
enableFuzzySearch:
|
ignoreDirs: [],
|
||||||
config?.getFileFilteringEnableFuzzySearch() ?? true,
|
fileDiscoveryService: new FileDiscoveryService(
|
||||||
maxFiles: config?.getFileFilteringOptions()?.maxFileCount,
|
dir,
|
||||||
});
|
config?.getFileFilteringOptions(),
|
||||||
await searcher.initialize();
|
),
|
||||||
fileSearch.current = searcher;
|
cache: true,
|
||||||
|
cacheTtl: 30,
|
||||||
|
enableRecursiveFileSearch:
|
||||||
|
config?.getEnableRecursiveFileSearch() ?? true,
|
||||||
|
enableFuzzySearch:
|
||||||
|
config?.getFileFilteringEnableFuzzySearch() ?? true,
|
||||||
|
maxFiles: config?.getFileFilteringOptions()?.maxFileCount,
|
||||||
|
});
|
||||||
|
|
||||||
|
initPromises.push(
|
||||||
|
searcher.initialize().then(() => {
|
||||||
|
if (initEpoch.current === currentEpoch) {
|
||||||
|
fileSearchMap.current.set(dir, searcher);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(initPromises);
|
||||||
|
|
||||||
|
if (initEpoch.current !== currentEpoch) return;
|
||||||
|
|
||||||
dispatch({ type: 'INITIALIZE_SUCCESS' });
|
dispatch({ type: 'INITIALIZE_SUCCESS' });
|
||||||
if (state.pattern !== null) {
|
if (state.pattern !== null) {
|
||||||
dispatch({ type: 'SEARCH', payload: state.pattern });
|
dispatch({ type: 'SEARCH', payload: state.pattern });
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
dispatch({ type: 'ERROR' });
|
if (initEpoch.current === currentEpoch) {
|
||||||
|
dispatch({ type: 'ERROR' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const search = async () => {
|
const search = async () => {
|
||||||
if (!fileSearch.current || state.pattern === null) {
|
if (fileSearchMap.current.size === 0 || state.pattern === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const currentPattern = state.pattern;
|
||||||
|
|
||||||
if (slowSearchTimer.current) {
|
if (slowSearchTimer.current) {
|
||||||
clearTimeout(slowSearchTimer.current);
|
clearTimeout(slowSearchTimer.current);
|
||||||
}
|
}
|
||||||
@@ -310,10 +354,26 @@ export function useAtCompletion(props: UseAtCompletionProps): void {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const results = await fileSearch.current.search(state.pattern, {
|
const directories = config
|
||||||
signal: controller.signal,
|
?.getWorkspaceContext?.()
|
||||||
maxResults: MAX_SUGGESTIONS_TO_SHOW * 3,
|
?.getDirectories() ?? [cwd];
|
||||||
});
|
const cwdRealpath = directories[0];
|
||||||
|
|
||||||
|
const allSearchPromises = [...fileSearchMap.current.entries()].map(
|
||||||
|
async ([dir, searcher]): Promise<string[]> => {
|
||||||
|
const results = await searcher.search(currentPattern, {
|
||||||
|
signal: controller.signal,
|
||||||
|
maxResults: MAX_SUGGESTIONS_TO_SHOW * 3,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (dir !== cwdRealpath) {
|
||||||
|
return results.map((p: string) => path.join(dir, p));
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const allResults = await Promise.all(allSearchPromises);
|
||||||
|
|
||||||
if (slowSearchTimer.current) {
|
if (slowSearchTimer.current) {
|
||||||
clearTimeout(slowSearchTimer.current);
|
clearTimeout(slowSearchTimer.current);
|
||||||
@@ -323,7 +383,9 @@ export function useAtCompletion(props: UseAtCompletionProps): void {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileSuggestions = results.map((p) => ({
|
const mergedResults = allResults.flat();
|
||||||
|
|
||||||
|
const fileSuggestions = mergedResults.map((p) => ({
|
||||||
label: p,
|
label: p,
|
||||||
value: escapePath(p),
|
value: escapePath(p),
|
||||||
}));
|
}));
|
||||||
@@ -331,7 +393,7 @@ export function useAtCompletion(props: UseAtCompletionProps): void {
|
|||||||
const resourceCandidates = buildResourceCandidates(config);
|
const resourceCandidates = buildResourceCandidates(config);
|
||||||
const resourceSuggestions = (
|
const resourceSuggestions = (
|
||||||
await searchResourceCandidates(
|
await searchResourceCandidates(
|
||||||
state.pattern ?? '',
|
currentPattern ?? '',
|
||||||
resourceCandidates,
|
resourceCandidates,
|
||||||
)
|
)
|
||||||
).map((suggestion) => ({
|
).map((suggestion) => ({
|
||||||
@@ -342,10 +404,15 @@ export function useAtCompletion(props: UseAtCompletionProps): void {
|
|||||||
|
|
||||||
const agentCandidates = buildAgentCandidates(config);
|
const agentCandidates = buildAgentCandidates(config);
|
||||||
const agentSuggestions = await searchAgentCandidates(
|
const agentSuggestions = await searchAgentCandidates(
|
||||||
state.pattern ?? '',
|
currentPattern ?? '',
|
||||||
agentCandidates,
|
agentCandidates,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Re-check after resource/agent searches which are not abort-aware
|
||||||
|
if (controller.signal.aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const combinedSuggestions = [
|
const combinedSuggestions = [
|
||||||
...agentSuggestions,
|
...agentSuggestions,
|
||||||
...fileSuggestions,
|
...fileSuggestions,
|
||||||
|
|||||||
Reference in New Issue
Block a user