2025-04-29 08:29:09 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-07-24 21:41:35 -07:00
|
|
|
import { useState, useEffect, useCallback, useMemo } from 'react';
|
2025-04-29 08:29:09 -07:00
|
|
|
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-07-20 00:55:33 -07:00
|
|
|
DEFAULT_FILE_FILTERING_OPTIONS,
|
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-07-07 16:45:44 -04:00
|
|
|
import { CommandContext, SlashCommand } from '../commands/types.js';
|
2025-07-24 21:41:35 -07:00
|
|
|
import { TextBuffer } from '../components/shared/text-buffer.js';
|
|
|
|
|
import { isSlashCommand } from '../utils/commandUtils.js';
|
|
|
|
|
import { toCodePoints } from '../utils/textUtils.js';
|
2025-05-01 00:52:01 +00:00
|
|
|
|
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;
|
2025-07-18 00:55:29 -04:00
|
|
|
isPerfectMatch: boolean;
|
2025-04-29 08:29:09 -07:00
|
|
|
setActiveSuggestionIndex: React.Dispatch<React.SetStateAction<number>>;
|
|
|
|
|
setShowSuggestions: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
|
resetCompletionState: () => void;
|
|
|
|
|
navigateUp: () => void;
|
|
|
|
|
navigateDown: () => void;
|
2025-07-24 21:41:35 -07:00
|
|
|
handleAutocomplete: (indexToUse: number) => void;
|
2025-04-29 08:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useCompletion(
|
2025-07-24 21:41:35 -07:00
|
|
|
buffer: TextBuffer,
|
2025-04-29 08:29:09 -07:00
|
|
|
cwd: string,
|
2025-07-20 16:57:34 -04:00
|
|
|
slashCommands: readonly SlashCommand[],
|
2025-07-07 16:45:44 -04:00
|
|
|
commandContext: CommandContext,
|
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);
|
2025-07-18 00:55:29 -04:00
|
|
|
const [isPerfectMatch, setIsPerfectMatch] = useState<boolean>(false);
|
2025-04-29 08:29:09 -07:00
|
|
|
|
|
|
|
|
const resetCompletionState = useCallback(() => {
|
|
|
|
|
setSuggestions([]);
|
|
|
|
|
setActiveSuggestionIndex(-1);
|
|
|
|
|
setVisibleStartIndex(0);
|
|
|
|
|
setShowSuggestions(false);
|
|
|
|
|
setIsLoadingSuggestions(false);
|
2025-07-18 00:55:29 -04:00
|
|
|
setIsPerfectMatch(false);
|
2025-04-29 08:29:09 -07:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2025-07-24 21:41:35 -07:00
|
|
|
// Check if cursor is after @ or / without unescaped spaces
|
|
|
|
|
const isActive = useMemo(() => {
|
|
|
|
|
if (isSlashCommand(buffer.text.trim())) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For other completions like '@', we search backwards from the cursor.
|
|
|
|
|
const [row, col] = buffer.cursor;
|
|
|
|
|
const currentLine = buffer.lines[row] || '';
|
|
|
|
|
const codePoints = toCodePoints(currentLine);
|
|
|
|
|
|
|
|
|
|
for (let i = col - 1; i >= 0; i--) {
|
|
|
|
|
const char = codePoints[i];
|
|
|
|
|
|
|
|
|
|
if (char === ' ') {
|
|
|
|
|
// Check for unescaped spaces.
|
|
|
|
|
let backslashCount = 0;
|
|
|
|
|
for (let j = i - 1; j >= 0 && codePoints[j] === '\\'; j--) {
|
|
|
|
|
backslashCount++;
|
|
|
|
|
}
|
|
|
|
|
if (backslashCount % 2 === 0) {
|
|
|
|
|
return false; // Inactive on unescaped space.
|
|
|
|
|
}
|
|
|
|
|
} else if (char === '@') {
|
|
|
|
|
// Active if we find an '@' before any unescaped space.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}, [buffer.text, buffer.cursor, buffer.lines]);
|
|
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isActive) {
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 21:41:35 -07:00
|
|
|
const trimmedQuery = buffer.text.trimStart();
|
2025-05-01 00:52:01 +00:00
|
|
|
|
|
|
|
|
if (trimmedQuery.startsWith('/')) {
|
2025-07-18 00:55:29 -04:00
|
|
|
// Always reset perfect match at the beginning of processing.
|
|
|
|
|
setIsPerfectMatch(false);
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
const fullPath = trimmedQuery.substring(1);
|
|
|
|
|
const hasTrailingSpace = trimmedQuery.endsWith(' ');
|
2025-06-15 11:40:39 -07:00
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
// Get all non-empty parts of the command.
|
|
|
|
|
const rawParts = fullPath.split(/\s+/).filter((p) => p);
|
2025-06-15 11:40:39 -07:00
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
let commandPathParts = rawParts;
|
|
|
|
|
let partial = '';
|
|
|
|
|
|
|
|
|
|
// If there's no trailing space, the last part is potentially a partial segment.
|
|
|
|
|
// We tentatively separate it.
|
|
|
|
|
if (!hasTrailingSpace && rawParts.length > 0) {
|
|
|
|
|
partial = rawParts[rawParts.length - 1];
|
|
|
|
|
commandPathParts = rawParts.slice(0, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Traverse the Command Tree using the tentative completed path
|
2025-07-20 16:57:34 -04:00
|
|
|
let currentLevel: readonly SlashCommand[] | undefined = slashCommands;
|
2025-07-07 16:45:44 -04:00
|
|
|
let leafCommand: SlashCommand | null = null;
|
|
|
|
|
|
|
|
|
|
for (const part of commandPathParts) {
|
|
|
|
|
if (!currentLevel) {
|
|
|
|
|
leafCommand = null;
|
|
|
|
|
currentLevel = [];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
const found: SlashCommand | undefined = currentLevel.find(
|
2025-07-20 16:57:34 -04:00
|
|
|
(cmd) => cmd.name === part || cmd.altNames?.includes(part),
|
2025-07-07 16:45:44 -04:00
|
|
|
);
|
|
|
|
|
if (found) {
|
|
|
|
|
leafCommand = found;
|
2025-07-20 16:57:34 -04:00
|
|
|
currentLevel = found.subCommands as
|
|
|
|
|
| readonly SlashCommand[]
|
|
|
|
|
| undefined;
|
2025-07-07 16:45:44 -04:00
|
|
|
} else {
|
|
|
|
|
leafCommand = null;
|
|
|
|
|
currentLevel = [];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle the Ambiguous Case
|
|
|
|
|
if (!hasTrailingSpace && currentLevel) {
|
|
|
|
|
const exactMatchAsParent = currentLevel.find(
|
|
|
|
|
(cmd) =>
|
2025-07-20 16:57:34 -04:00
|
|
|
(cmd.name === partial || cmd.altNames?.includes(partial)) &&
|
2025-07-07 16:45:44 -04:00
|
|
|
cmd.subCommands,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (exactMatchAsParent) {
|
|
|
|
|
// It's a perfect match for a parent command. Override our initial guess.
|
|
|
|
|
// Treat it as a completed command path.
|
|
|
|
|
leafCommand = exactMatchAsParent;
|
|
|
|
|
currentLevel = exactMatchAsParent.subCommands;
|
|
|
|
|
partial = ''; // We now want to suggest ALL of its sub-commands.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 00:55:29 -04:00
|
|
|
// Check for perfect, executable match
|
|
|
|
|
if (!hasTrailingSpace) {
|
|
|
|
|
if (leafCommand && partial === '' && leafCommand.action) {
|
|
|
|
|
// Case: /command<enter> - command has action, no sub-commands were suggested
|
|
|
|
|
setIsPerfectMatch(true);
|
|
|
|
|
} else if (currentLevel) {
|
|
|
|
|
// Case: /command subcommand<enter>
|
|
|
|
|
const perfectMatch = currentLevel.find(
|
|
|
|
|
(cmd) =>
|
2025-07-20 16:57:34 -04:00
|
|
|
(cmd.name === partial || cmd.altNames?.includes(partial)) &&
|
|
|
|
|
cmd.action,
|
2025-07-18 00:55:29 -04:00
|
|
|
);
|
|
|
|
|
if (perfectMatch) {
|
|
|
|
|
setIsPerfectMatch(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
const depth = commandPathParts.length;
|
|
|
|
|
|
|
|
|
|
// Provide Suggestions based on the now-corrected context
|
|
|
|
|
|
|
|
|
|
// Argument Completion
|
|
|
|
|
if (
|
|
|
|
|
leafCommand?.completion &&
|
|
|
|
|
(hasTrailingSpace ||
|
|
|
|
|
(rawParts.length > depth && depth > 0 && partial !== ''))
|
|
|
|
|
) {
|
2025-06-15 11:40:39 -07:00
|
|
|
const fetchAndSetSuggestions = async () => {
|
|
|
|
|
setIsLoadingSuggestions(true);
|
2025-07-07 16:45:44 -04:00
|
|
|
const argString = rawParts.slice(depth).join(' ');
|
|
|
|
|
const results =
|
|
|
|
|
(await leafCommand!.completion!(commandContext, argString)) || [];
|
|
|
|
|
const finalSuggestions = results.map((s) => ({ label: s, value: s }));
|
|
|
|
|
setSuggestions(finalSuggestions);
|
|
|
|
|
setShowSuggestions(finalSuggestions.length > 0);
|
|
|
|
|
setActiveSuggestionIndex(finalSuggestions.length > 0 ? 0 : -1);
|
2025-06-15 11:40:39 -07:00
|
|
|
setIsLoadingSuggestions(false);
|
|
|
|
|
};
|
|
|
|
|
fetchAndSetSuggestions();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
// Command/Sub-command Completion
|
|
|
|
|
const commandsToSearch = currentLevel || [];
|
|
|
|
|
if (commandsToSearch.length > 0) {
|
|
|
|
|
let potentialSuggestions = commandsToSearch.filter(
|
2025-05-14 16:01:29 -07:00
|
|
|
(cmd) =>
|
2025-07-07 16:45:44 -04:00
|
|
|
cmd.description &&
|
2025-07-20 16:57:34 -04:00
|
|
|
(cmd.name.startsWith(partial) ||
|
|
|
|
|
cmd.altNames?.some((alt) => alt.startsWith(partial))),
|
2025-07-07 16:45:44 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// If a user's input is an exact match and it is a leaf command,
|
|
|
|
|
// enter should submit immediately.
|
|
|
|
|
if (potentialSuggestions.length > 0 && !hasTrailingSpace) {
|
|
|
|
|
const perfectMatch = potentialSuggestions.find(
|
2025-07-20 16:57:34 -04:00
|
|
|
(s) => s.name === partial || s.altNames?.includes(partial),
|
2025-05-14 16:01:29 -07:00
|
|
|
);
|
2025-07-18 00:55:29 -04:00
|
|
|
if (perfectMatch && perfectMatch.action) {
|
2025-07-07 16:45:44 -04:00
|
|
|
potentialSuggestions = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const finalSuggestions = potentialSuggestions.map((cmd) => ({
|
|
|
|
|
label: cmd.name,
|
|
|
|
|
value: cmd.name,
|
2025-05-14 16:01:29 -07:00
|
|
|
description: cmd.description,
|
2025-07-07 16:45:44 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
setSuggestions(finalSuggestions);
|
|
|
|
|
setShowSuggestions(finalSuggestions.length > 0);
|
|
|
|
|
setActiveSuggestionIndex(finalSuggestions.length > 0 ? 0 : -1);
|
|
|
|
|
setIsLoadingSuggestions(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we fall through, no suggestions are available.
|
|
|
|
|
resetCompletionState();
|
2025-05-01 00:52:01 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 16:45:44 -04:00
|
|
|
// Handle At Command Completion
|
2025-07-24 21:41:35 -07:00
|
|
|
const atIndex = buffer.text.lastIndexOf('@');
|
2025-04-29 08:29:09 -07:00
|
|
|
if (atIndex === -1) {
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 21:41:35 -07:00
|
|
|
const partialPath = buffer.text.substring(atIndex + 1);
|
2025-04-29 08:29:09 -07:00
|
|
|
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
|
2025-07-25 10:32:59 -07:00
|
|
|
.map((file: string) => ({
|
|
|
|
|
label: file,
|
|
|
|
|
value: escapePath(file),
|
|
|
|
|
}))
|
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-20 00:55:33 -07:00
|
|
|
const filterOptions =
|
|
|
|
|
config?.getFileFilteringOptions() ?? DEFAULT_FILE_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
|
|
|
|
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,
|
2025-07-25 10:32:59 -07:00
|
|
|
null,
|
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
|
|
|
}
|
|
|
|
|
|
2025-07-25 10:32:59 -07:00
|
|
|
// Like glob, we always return forwardslashes, even in windows.
|
|
|
|
|
fetchedSuggestions = fetchedSuggestions.map((suggestion) => ({
|
|
|
|
|
...suggestion,
|
|
|
|
|
label: suggestion.label.replace(/\\/g, '/'),
|
|
|
|
|
value: suggestion.value.replace(/\\/g, '/'),
|
|
|
|
|
}));
|
|
|
|
|
|
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;
|
|
|
|
|
|
2025-07-14 14:10:26 -07:00
|
|
|
// exclude extension when comparing
|
|
|
|
|
const filenameA = a.label.substring(
|
|
|
|
|
0,
|
|
|
|
|
a.label.length - path.extname(a.label).length,
|
|
|
|
|
);
|
|
|
|
|
const filenameB = b.label.substring(
|
|
|
|
|
0,
|
|
|
|
|
b.label.length - path.extname(b.label).length,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
filenameA.localeCompare(filenameB) || 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);
|
|
|
|
|
};
|
2025-07-07 16:45:44 -04:00
|
|
|
}, [
|
2025-07-24 21:41:35 -07:00
|
|
|
buffer.text,
|
2025-07-07 16:45:44 -04:00
|
|
|
cwd,
|
|
|
|
|
isActive,
|
|
|
|
|
resetCompletionState,
|
|
|
|
|
slashCommands,
|
|
|
|
|
commandContext,
|
|
|
|
|
config,
|
|
|
|
|
]);
|
2025-04-29 08:29:09 -07:00
|
|
|
|
2025-07-24 21:41:35 -07:00
|
|
|
const handleAutocomplete = useCallback(
|
|
|
|
|
(indexToUse: number) => {
|
|
|
|
|
if (indexToUse < 0 || indexToUse >= suggestions.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const query = buffer.text;
|
|
|
|
|
const suggestion = suggestions[indexToUse].value;
|
|
|
|
|
|
|
|
|
|
if (query.trimStart().startsWith('/')) {
|
|
|
|
|
const hasTrailingSpace = query.endsWith(' ');
|
|
|
|
|
const parts = query
|
|
|
|
|
.trimStart()
|
|
|
|
|
.substring(1)
|
|
|
|
|
.split(/\s+/)
|
|
|
|
|
.filter(Boolean);
|
|
|
|
|
|
|
|
|
|
let isParentPath = false;
|
|
|
|
|
// If there's no trailing space, we need to check if the current query
|
|
|
|
|
// is already a complete path to a parent command.
|
|
|
|
|
if (!hasTrailingSpace) {
|
|
|
|
|
let currentLevel: readonly SlashCommand[] | undefined = slashCommands;
|
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
|
const part = parts[i];
|
|
|
|
|
const found: SlashCommand | undefined = currentLevel?.find(
|
|
|
|
|
(cmd) => cmd.name === part || cmd.altNames?.includes(part),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
|
if (i === parts.length - 1 && found.subCommands) {
|
|
|
|
|
isParentPath = true;
|
|
|
|
|
}
|
|
|
|
|
currentLevel = found.subCommands as
|
|
|
|
|
| readonly SlashCommand[]
|
|
|
|
|
| undefined;
|
|
|
|
|
} else {
|
|
|
|
|
// Path is invalid, so it can't be a parent path.
|
|
|
|
|
currentLevel = undefined;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine the base path of the command.
|
|
|
|
|
// - If there's a trailing space, the whole command is the base.
|
|
|
|
|
// - If it's a known parent path, the whole command is the base.
|
2025-07-25 20:56:33 +00:00
|
|
|
// - If the last part is a complete argument, the whole command is the base.
|
2025-07-24 21:41:35 -07:00
|
|
|
// - Otherwise, the base is everything EXCEPT the last partial part.
|
2025-07-25 20:56:33 +00:00
|
|
|
const lastPart = parts.length > 0 ? parts[parts.length - 1] : '';
|
|
|
|
|
const isLastPartACompleteArg =
|
|
|
|
|
lastPart.startsWith('--') && lastPart.includes('=');
|
|
|
|
|
|
2025-07-24 21:41:35 -07:00
|
|
|
const basePath =
|
2025-07-25 20:56:33 +00:00
|
|
|
hasTrailingSpace || isParentPath || isLastPartACompleteArg
|
|
|
|
|
? parts
|
|
|
|
|
: parts.slice(0, -1);
|
|
|
|
|
const newValue = `/${[...basePath, suggestion].join(' ')} `;
|
2025-07-24 21:41:35 -07:00
|
|
|
|
|
|
|
|
buffer.setText(newValue);
|
|
|
|
|
} else {
|
|
|
|
|
const atIndex = query.lastIndexOf('@');
|
|
|
|
|
if (atIndex === -1) return;
|
|
|
|
|
const pathPart = query.substring(atIndex + 1);
|
|
|
|
|
const lastSlashIndexInPath = pathPart.lastIndexOf('/');
|
|
|
|
|
let autoCompleteStartIndex = atIndex + 1;
|
|
|
|
|
if (lastSlashIndexInPath !== -1) {
|
|
|
|
|
autoCompleteStartIndex += lastSlashIndexInPath + 1;
|
|
|
|
|
}
|
|
|
|
|
buffer.replaceRangeByOffset(
|
|
|
|
|
autoCompleteStartIndex,
|
|
|
|
|
buffer.text.length,
|
|
|
|
|
suggestion,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
resetCompletionState();
|
|
|
|
|
},
|
|
|
|
|
[resetCompletionState, buffer, suggestions, slashCommands],
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-29 08:29:09 -07:00
|
|
|
return {
|
|
|
|
|
suggestions,
|
|
|
|
|
activeSuggestionIndex,
|
|
|
|
|
visibleStartIndex,
|
|
|
|
|
showSuggestions,
|
|
|
|
|
isLoadingSuggestions,
|
2025-07-18 00:55:29 -04:00
|
|
|
isPerfectMatch,
|
2025-04-29 08:29:09 -07:00
|
|
|
setActiveSuggestionIndex,
|
|
|
|
|
setShowSuggestions,
|
|
|
|
|
resetCompletionState,
|
|
|
|
|
navigateUp,
|
|
|
|
|
navigateDown,
|
2025-07-24 21:41:35 -07:00
|
|
|
handleAutocomplete,
|
2025-04-29 08:29:09 -07:00
|
|
|
};
|
|
|
|
|
}
|