2025-04-29 08:29:09 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
|
|
|
import * as fs from 'fs/promises';
|
|
|
|
|
import * as path from 'path';
|
2025-06-14 10:25:34 -04:00
|
|
|
import { glob } from 'glob';
|
2025-05-15 23:51:53 -07:00
|
|
|
import {
|
|
|
|
|
isNodeError,
|
|
|
|
|
escapePath,
|
|
|
|
|
unescapePath,
|
|
|
|
|
getErrorMessage,
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
Config,
|
2025-06-12 07:09:38 -07:00
|
|
|
FileDiscoveryService,
|
2025-06-25 05:41:11 -07:00
|
|
|
} from '@google/gemini-cli-core';
|
2025-05-01 18:02:04 -07:00
|
|
|
import {
|
|
|
|
|
MAX_SUGGESTIONS_TO_SHOW,
|
|
|
|
|
Suggestion,
|
|
|
|
|
} from '../components/SuggestionsDisplay.js';
|
2025-05-01 00:52:01 +00:00
|
|
|
import { SlashCommand } from './slashCommandProcessor.js';
|
|
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
export interface UseCompletionReturn {
|
2025-05-01 18:02:04 -07:00
|
|
|
suggestions: Suggestion[];
|
2025-04-29 08:29:09 -07:00
|
|
|
activeSuggestionIndex: number;
|
|
|
|
|
visibleStartIndex: number;
|
|
|
|
|
showSuggestions: boolean;
|
|
|
|
|
isLoadingSuggestions: boolean;
|
|
|
|
|
setActiveSuggestionIndex: React.Dispatch<React.SetStateAction<number>>;
|
|
|
|
|
setShowSuggestions: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
|
resetCompletionState: () => void;
|
|
|
|
|
navigateUp: () => void;
|
|
|
|
|
navigateDown: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useCompletion(
|
|
|
|
|
query: string,
|
|
|
|
|
cwd: string,
|
|
|
|
|
isActive: boolean,
|
2025-05-01 00:52:01 +00:00
|
|
|
slashCommands: SlashCommand[],
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
config?: Config,
|
2025-04-29 08:29:09 -07:00
|
|
|
): UseCompletionReturn {
|
2025-05-01 18:02:04 -07:00
|
|
|
const [suggestions, setSuggestions] = useState<Suggestion[]>([]);
|
2025-04-29 08:29:09 -07:00
|
|
|
const [activeSuggestionIndex, setActiveSuggestionIndex] =
|
|
|
|
|
useState<number>(-1);
|
|
|
|
|
const [visibleStartIndex, setVisibleStartIndex] = useState<number>(0);
|
|
|
|
|
const [showSuggestions, setShowSuggestions] = useState<boolean>(false);
|
|
|
|
|
const [isLoadingSuggestions, setIsLoadingSuggestions] =
|
|
|
|
|
useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
const resetCompletionState = useCallback(() => {
|
|
|
|
|
setSuggestions([]);
|
|
|
|
|
setActiveSuggestionIndex(-1);
|
|
|
|
|
setVisibleStartIndex(0);
|
|
|
|
|
setShowSuggestions(false);
|
|
|
|
|
setIsLoadingSuggestions(false);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const navigateUp = useCallback(() => {
|
|
|
|
|
if (suggestions.length === 0) return;
|
|
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
setActiveSuggestionIndex((prevActiveIndex) => {
|
|
|
|
|
// Calculate new active index, handling wrap-around
|
|
|
|
|
const newActiveIndex =
|
|
|
|
|
prevActiveIndex <= 0 ? suggestions.length - 1 : prevActiveIndex - 1;
|
|
|
|
|
|
|
|
|
|
// Adjust scroll position based on the new active index
|
|
|
|
|
setVisibleStartIndex((prevVisibleStart) => {
|
|
|
|
|
// Case 1: Wrapped around to the last item
|
|
|
|
|
if (
|
|
|
|
|
newActiveIndex === suggestions.length - 1 &&
|
|
|
|
|
suggestions.length > MAX_SUGGESTIONS_TO_SHOW
|
|
|
|
|
) {
|
|
|
|
|
return Math.max(0, suggestions.length - MAX_SUGGESTIONS_TO_SHOW);
|
|
|
|
|
}
|
|
|
|
|
// Case 2: Scrolled above the current visible window
|
|
|
|
|
if (newActiveIndex < prevVisibleStart) {
|
|
|
|
|
return newActiveIndex;
|
|
|
|
|
}
|
|
|
|
|
// Otherwise, keep the current scroll position
|
|
|
|
|
return prevVisibleStart;
|
|
|
|
|
});
|
2025-04-29 08:29:09 -07:00
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
return newActiveIndex;
|
2025-04-29 08:29:09 -07:00
|
|
|
});
|
2025-04-30 08:31:32 -07:00
|
|
|
}, [suggestions.length]);
|
2025-04-29 08:29:09 -07:00
|
|
|
|
|
|
|
|
const navigateDown = useCallback(() => {
|
|
|
|
|
if (suggestions.length === 0) return;
|
|
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
setActiveSuggestionIndex((prevActiveIndex) => {
|
|
|
|
|
// Calculate new active index, handling wrap-around
|
|
|
|
|
const newActiveIndex =
|
|
|
|
|
prevActiveIndex >= suggestions.length - 1 ? 0 : prevActiveIndex + 1;
|
|
|
|
|
|
|
|
|
|
// Adjust scroll position based on the new active index
|
|
|
|
|
setVisibleStartIndex((prevVisibleStart) => {
|
|
|
|
|
// Case 1: Wrapped around to the first item
|
|
|
|
|
if (
|
|
|
|
|
newActiveIndex === 0 &&
|
|
|
|
|
suggestions.length > MAX_SUGGESTIONS_TO_SHOW
|
|
|
|
|
) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// Case 2: Scrolled below the current visible window
|
|
|
|
|
const visibleEndIndex = prevVisibleStart + MAX_SUGGESTIONS_TO_SHOW;
|
|
|
|
|
if (newActiveIndex >= visibleEndIndex) {
|
|
|
|
|
return newActiveIndex - MAX_SUGGESTIONS_TO_SHOW + 1;
|
|
|
|
|
}
|
|
|
|
|
// Otherwise, keep the current scroll position
|
|
|
|
|
return prevVisibleStart;
|
|
|
|
|
});
|
2025-04-29 08:29:09 -07:00
|
|
|
|
2025-04-30 08:31:32 -07:00
|
|
|
return newActiveIndex;
|
2025-04-29 08:29:09 -07:00
|
|
|
});
|
2025-04-30 08:31:32 -07:00
|
|
|
}, [suggestions.length]);
|
2025-04-29 08:29:09 -07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isActive) {
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 00:52:01 +00:00
|
|
|
const trimmedQuery = query.trimStart(); // Trim leading whitespace
|
|
|
|
|
|
|
|
|
|
// --- Handle Slash Command Completion ---
|
|
|
|
|
if (trimmedQuery.startsWith('/')) {
|
2025-06-15 11:40:39 -07:00
|
|
|
const parts = trimmedQuery.substring(1).split(' ');
|
|
|
|
|
const commandName = parts[0];
|
|
|
|
|
const subCommand = parts.slice(1).join(' ');
|
|
|
|
|
|
|
|
|
|
const command = slashCommands.find(
|
|
|
|
|
(cmd) => cmd.name === commandName || cmd.altName === commandName,
|
|
|
|
|
);
|
|
|
|
|
|
2025-07-01 15:51:43 -07:00
|
|
|
// Continue to show command help until user types past command name.
|
|
|
|
|
if (command && command.completion && parts.length > 1) {
|
2025-06-15 11:40:39 -07:00
|
|
|
const fetchAndSetSuggestions = async () => {
|
|
|
|
|
setIsLoadingSuggestions(true);
|
|
|
|
|
if (command.completion) {
|
|
|
|
|
const results = await command.completion();
|
|
|
|
|
const filtered = results.filter((r) => r.startsWith(subCommand));
|
|
|
|
|
const newSuggestions = filtered.map((s) => ({
|
|
|
|
|
label: s,
|
|
|
|
|
value: s,
|
|
|
|
|
}));
|
|
|
|
|
setSuggestions(newSuggestions);
|
|
|
|
|
setShowSuggestions(newSuggestions.length > 0);
|
|
|
|
|
setActiveSuggestionIndex(newSuggestions.length > 0 ? 0 : -1);
|
|
|
|
|
}
|
|
|
|
|
setIsLoadingSuggestions(false);
|
|
|
|
|
};
|
|
|
|
|
fetchAndSetSuggestions();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 00:52:01 +00:00
|
|
|
const partialCommand = trimmedQuery.substring(1);
|
2025-05-14 16:01:29 -07:00
|
|
|
const filteredSuggestions = slashCommands
|
|
|
|
|
.filter(
|
|
|
|
|
(cmd) =>
|
|
|
|
|
cmd.name.startsWith(partialCommand) ||
|
|
|
|
|
cmd.altName?.startsWith(partialCommand),
|
|
|
|
|
)
|
|
|
|
|
// Filter out ? and any other single character commands unless it's the only char
|
|
|
|
|
.filter((cmd) => {
|
|
|
|
|
const nameMatch = cmd.name.startsWith(partialCommand);
|
|
|
|
|
const altNameMatch = cmd.altName?.startsWith(partialCommand);
|
|
|
|
|
if (partialCommand.length === 1) {
|
|
|
|
|
return nameMatch || altNameMatch; // Allow single char match if query is single char
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
(nameMatch && cmd.name.length > 1) ||
|
|
|
|
|
(altNameMatch && cmd.altName && cmd.altName.length > 1)
|
|
|
|
|
);
|
|
|
|
|
})
|
2025-05-17 21:57:27 -07:00
|
|
|
.filter((cmd) => cmd.description)
|
2025-05-14 16:01:29 -07:00
|
|
|
.map((cmd) => ({
|
|
|
|
|
label: cmd.name, // Always show the main name as label
|
|
|
|
|
value: cmd.name, // Value should be the main command name for execution
|
|
|
|
|
description: cmd.description,
|
|
|
|
|
}))
|
|
|
|
|
.sort((a, b) => a.label.localeCompare(b.label));
|
2025-05-01 00:52:01 +00:00
|
|
|
|
|
|
|
|
setSuggestions(filteredSuggestions);
|
|
|
|
|
setShowSuggestions(filteredSuggestions.length > 0);
|
2025-05-09 15:38:19 -07:00
|
|
|
setActiveSuggestionIndex(filteredSuggestions.length > 0 ? 0 : -1);
|
2025-05-01 00:52:01 +00:00
|
|
|
setVisibleStartIndex(0);
|
|
|
|
|
setIsLoadingSuggestions(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Handle At Command Completion ---
|
2025-04-29 08:29:09 -07:00
|
|
|
const atIndex = query.lastIndexOf('@');
|
|
|
|
|
if (atIndex === -1) {
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const partialPath = query.substring(atIndex + 1);
|
|
|
|
|
const lastSlashIndex = partialPath.lastIndexOf('/');
|
|
|
|
|
const baseDirRelative =
|
|
|
|
|
lastSlashIndex === -1
|
|
|
|
|
? '.'
|
|
|
|
|
: partialPath.substring(0, lastSlashIndex + 1);
|
2025-05-01 18:02:04 -07:00
|
|
|
const prefix = unescapePath(
|
2025-04-29 08:29:09 -07:00
|
|
|
lastSlashIndex === -1
|
|
|
|
|
? partialPath
|
2025-05-01 18:02:04 -07:00
|
|
|
: partialPath.substring(lastSlashIndex + 1),
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
const baseDirAbsolute = path.resolve(cwd, baseDirRelative);
|
|
|
|
|
|
|
|
|
|
let isMounted = true;
|
2025-05-21 12:22:18 -07:00
|
|
|
|
|
|
|
|
const findFilesRecursively = async (
|
|
|
|
|
startDir: string,
|
|
|
|
|
searchPrefix: string,
|
2025-07-07 13:48:39 +08:00
|
|
|
fileDiscovery: FileDiscoveryService | null,
|
|
|
|
|
filterOptions: {
|
|
|
|
|
respectGitIgnore?: boolean;
|
|
|
|
|
respectGeminiIgnore?: boolean;
|
|
|
|
|
},
|
2025-05-21 12:22:18 -07:00
|
|
|
currentRelativePath = '',
|
|
|
|
|
depth = 0,
|
|
|
|
|
maxDepth = 10, // Limit recursion depth
|
|
|
|
|
maxResults = 50, // Limit number of results
|
|
|
|
|
): Promise<Suggestion[]> => {
|
|
|
|
|
if (depth > maxDepth) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 16:19:14 -07:00
|
|
|
const lowerSearchPrefix = searchPrefix.toLowerCase();
|
2025-05-21 12:22:18 -07:00
|
|
|
let foundSuggestions: Suggestion[] = [];
|
|
|
|
|
try {
|
|
|
|
|
const entries = await fs.readdir(startDir, { withFileTypes: true });
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
|
if (foundSuggestions.length >= maxResults) break;
|
|
|
|
|
|
|
|
|
|
const entryPathRelative = path.join(currentRelativePath, entry.name);
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
const entryPathFromRoot = path.relative(
|
|
|
|
|
cwd,
|
|
|
|
|
path.join(startDir, entry.name),
|
|
|
|
|
);
|
|
|
|
|
|
2025-06-12 10:04:15 -07:00
|
|
|
// Conditionally ignore dotfiles
|
|
|
|
|
if (!searchPrefix.startsWith('.') && entry.name.startsWith('.')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 13:48:39 +08:00
|
|
|
// Check if this entry should be ignored by filtering options
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
if (
|
|
|
|
|
fileDiscovery &&
|
2025-07-07 13:48:39 +08:00
|
|
|
fileDiscovery.shouldIgnoreFile(entryPathFromRoot, filterOptions)
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 16:19:14 -07:00
|
|
|
if (entry.name.toLowerCase().startsWith(lowerSearchPrefix)) {
|
2025-05-21 12:22:18 -07:00
|
|
|
foundSuggestions.push({
|
|
|
|
|
label: entryPathRelative + (entry.isDirectory() ? '/' : ''),
|
|
|
|
|
value: escapePath(
|
|
|
|
|
entryPathRelative + (entry.isDirectory() ? '/' : ''),
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
entry.isDirectory() &&
|
|
|
|
|
entry.name !== 'node_modules' &&
|
|
|
|
|
!entry.name.startsWith('.')
|
|
|
|
|
) {
|
|
|
|
|
if (foundSuggestions.length < maxResults) {
|
|
|
|
|
foundSuggestions = foundSuggestions.concat(
|
|
|
|
|
await findFilesRecursively(
|
|
|
|
|
path.join(startDir, entry.name),
|
2025-05-31 16:19:14 -07:00
|
|
|
searchPrefix, // Pass original searchPrefix for recursive calls
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
fileDiscovery,
|
2025-07-07 13:48:39 +08:00
|
|
|
filterOptions,
|
2025-05-21 12:22:18 -07:00
|
|
|
entryPathRelative,
|
|
|
|
|
depth + 1,
|
|
|
|
|
maxDepth,
|
|
|
|
|
maxResults - foundSuggestions.length,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (_err) {
|
|
|
|
|
// Ignore errors like permission denied or ENOENT during recursive search
|
|
|
|
|
}
|
|
|
|
|
return foundSuggestions.slice(0, maxResults);
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-12 07:09:38 -07:00
|
|
|
const findFilesWithGlob = async (
|
|
|
|
|
searchPrefix: string,
|
|
|
|
|
fileDiscoveryService: FileDiscoveryService,
|
2025-07-07 13:48:39 +08:00
|
|
|
filterOptions: {
|
|
|
|
|
respectGitIgnore?: boolean;
|
|
|
|
|
respectGeminiIgnore?: boolean;
|
|
|
|
|
},
|
2025-06-12 07:09:38 -07:00
|
|
|
maxResults = 50,
|
|
|
|
|
): Promise<Suggestion[]> => {
|
|
|
|
|
const globPattern = `**/${searchPrefix}*`;
|
2025-06-14 10:25:34 -04:00
|
|
|
const files = await glob(globPattern, {
|
2025-06-12 07:09:38 -07:00
|
|
|
cwd,
|
2025-06-12 10:04:15 -07:00
|
|
|
dot: searchPrefix.startsWith('.'),
|
2025-06-14 10:25:34 -04:00
|
|
|
nocase: true,
|
2025-06-12 07:09:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const suggestions: Suggestion[] = files
|
|
|
|
|
.map((file: string) => {
|
|
|
|
|
const relativePath = path.relative(cwd, file);
|
|
|
|
|
return {
|
|
|
|
|
label: relativePath,
|
|
|
|
|
value: escapePath(relativePath),
|
|
|
|
|
};
|
|
|
|
|
})
|
2025-06-18 01:05:47 -04:00
|
|
|
.filter((s) => {
|
|
|
|
|
if (fileDiscoveryService) {
|
2025-07-07 13:48:39 +08:00
|
|
|
return !fileDiscoveryService.shouldIgnoreFile(
|
|
|
|
|
s.label,
|
|
|
|
|
filterOptions,
|
|
|
|
|
); // relative path
|
2025-06-18 01:05:47 -04:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
})
|
2025-06-12 07:09:38 -07:00
|
|
|
.slice(0, maxResults);
|
|
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
const fetchSuggestions = async () => {
|
|
|
|
|
setIsLoadingSuggestions(true);
|
2025-05-21 12:22:18 -07:00
|
|
|
let fetchedSuggestions: Suggestion[] = [];
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
|
2025-06-14 10:25:34 -04:00
|
|
|
const fileDiscoveryService = config ? config.getFileService() : null;
|
2025-06-21 18:23:35 -07:00
|
|
|
const enableRecursiveSearch =
|
|
|
|
|
config?.getEnableRecursiveFileSearch() ?? true;
|
2025-07-07 13:48:39 +08:00
|
|
|
const filterOptions = {
|
|
|
|
|
respectGitIgnore: config?.getFileFilteringRespectGitIgnore() ?? true,
|
|
|
|
|
respectGeminiIgnore: true,
|
|
|
|
|
};
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
try {
|
2025-05-21 12:22:18 -07:00
|
|
|
// If there's no slash, or it's the root, do a recursive search from cwd
|
2025-06-21 18:23:35 -07:00
|
|
|
if (
|
|
|
|
|
partialPath.indexOf('/') === -1 &&
|
|
|
|
|
prefix &&
|
|
|
|
|
enableRecursiveSearch
|
|
|
|
|
) {
|
2025-06-12 07:09:38 -07:00
|
|
|
if (fileDiscoveryService) {
|
|
|
|
|
fetchedSuggestions = await findFilesWithGlob(
|
|
|
|
|
prefix,
|
|
|
|
|
fileDiscoveryService,
|
2025-07-07 13:48:39 +08:00
|
|
|
filterOptions,
|
2025-06-12 07:09:38 -07:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
fetchedSuggestions = await findFilesRecursively(
|
|
|
|
|
cwd,
|
|
|
|
|
prefix,
|
|
|
|
|
fileDiscoveryService,
|
2025-07-07 13:48:39 +08:00
|
|
|
filterOptions,
|
2025-06-12 07:09:38 -07:00
|
|
|
);
|
|
|
|
|
}
|
2025-05-21 12:22:18 -07:00
|
|
|
} else {
|
|
|
|
|
// Original behavior: list files in the specific directory
|
2025-05-31 16:19:14 -07:00
|
|
|
const lowerPrefix = prefix.toLowerCase();
|
2025-05-21 12:22:18 -07:00
|
|
|
const entries = await fs.readdir(baseDirAbsolute, {
|
|
|
|
|
withFileTypes: true,
|
|
|
|
|
});
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
|
|
|
|
|
// Filter entries using git-aware filtering
|
|
|
|
|
const filteredEntries = [];
|
|
|
|
|
for (const entry of entries) {
|
2025-06-12 10:04:15 -07:00
|
|
|
// Conditionally ignore dotfiles
|
|
|
|
|
if (!prefix.startsWith('.') && entry.name.startsWith('.')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
if (!entry.name.toLowerCase().startsWith(lowerPrefix)) continue;
|
|
|
|
|
|
|
|
|
|
const relativePath = path.relative(
|
|
|
|
|
cwd,
|
|
|
|
|
path.join(baseDirAbsolute, entry.name),
|
|
|
|
|
);
|
2025-06-12 07:09:38 -07:00
|
|
|
if (
|
|
|
|
|
fileDiscoveryService &&
|
2025-07-07 13:48:39 +08:00
|
|
|
fileDiscoveryService.shouldIgnoreFile(relativePath, filterOptions)
|
2025-06-12 07:09:38 -07:00
|
|
|
) {
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filteredEntries.push(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchedSuggestions = filteredEntries.map((entry) => {
|
|
|
|
|
const label = entry.isDirectory() ? entry.name + '/' : entry.name;
|
|
|
|
|
return {
|
|
|
|
|
label,
|
|
|
|
|
value: escapePath(label), // Value for completion should be just the name part
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-05-21 12:22:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort by depth, then directories first, then alphabetically
|
|
|
|
|
fetchedSuggestions.sort((a, b) => {
|
|
|
|
|
const depthA = (a.label.match(/\//g) || []).length;
|
|
|
|
|
const depthB = (b.label.match(/\//g) || []).length;
|
|
|
|
|
|
|
|
|
|
if (depthA !== depthB) {
|
|
|
|
|
return depthA - depthB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const aIsDir = a.label.endsWith('/');
|
|
|
|
|
const bIsDir = b.label.endsWith('/');
|
|
|
|
|
if (aIsDir && !bIsDir) return -1;
|
|
|
|
|
if (!aIsDir && bIsDir) return 1;
|
|
|
|
|
|
|
|
|
|
return a.label.localeCompare(b.label);
|
2025-04-29 08:29:09 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (isMounted) {
|
2025-05-21 12:22:18 -07:00
|
|
|
setSuggestions(fetchedSuggestions);
|
|
|
|
|
setShowSuggestions(fetchedSuggestions.length > 0);
|
|
|
|
|
setActiveSuggestionIndex(fetchedSuggestions.length > 0 ? 0 : -1);
|
2025-04-30 08:31:32 -07:00
|
|
|
setVisibleStartIndex(0);
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
2025-05-15 23:51:53 -07:00
|
|
|
} catch (error: unknown) {
|
2025-04-29 08:29:09 -07:00
|
|
|
if (isNodeError(error) && error.code === 'ENOENT') {
|
|
|
|
|
if (isMounted) {
|
|
|
|
|
setSuggestions([]);
|
|
|
|
|
setShowSuggestions(false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.error(
|
2025-05-21 12:22:18 -07:00
|
|
|
`Error fetching completion suggestions for ${partialPath}: ${getErrorMessage(error)}`,
|
2025-04-29 08:29:09 -07:00
|
|
|
);
|
|
|
|
|
if (isMounted) {
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isMounted) {
|
|
|
|
|
setIsLoadingSuggestions(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const debounceTimeout = setTimeout(fetchSuggestions, 100);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted = false;
|
|
|
|
|
clearTimeout(debounceTimeout);
|
|
|
|
|
};
|
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
2025-06-03 21:40:46 -07:00
|
|
|
}, [query, cwd, isActive, resetCompletionState, slashCommands, config]);
|
2025-04-29 08:29:09 -07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
suggestions,
|
|
|
|
|
activeSuggestionIndex,
|
|
|
|
|
visibleStartIndex,
|
|
|
|
|
showSuggestions,
|
|
|
|
|
isLoadingSuggestions,
|
|
|
|
|
setActiveSuggestionIndex,
|
|
|
|
|
setShowSuggestions,
|
|
|
|
|
resetCompletionState,
|
|
|
|
|
navigateUp,
|
|
|
|
|
navigateDown,
|
|
|
|
|
};
|
|
|
|
|
}
|