Add context_before and context_after arguments to Grep tool

This commit is contained in:
Amar Sood (tekacs) 2025-04-12 13:57:06 -04:00
parent 7c0765d7f8
commit 9f8fde2411
4 changed files with 24 additions and 6 deletions

View file

@ -769,11 +769,14 @@ class NavigatorCoder(Coder):
directory = params.get('directory', '.') # Default to current directory directory = params.get('directory', '.') # Default to current directory
use_regex = params.get('use_regex', False) # Default to literal search use_regex = params.get('use_regex', False) # Default to literal search
case_insensitive = params.get('case_insensitive', False) # Default to case-sensitive case_insensitive = params.get('case_insensitive', False) # Default to case-sensitive
context_before = params.get('context_before', 5)
context_after = params.get('context_after', 5)
if pattern is not None: if pattern is not None:
# Import the function if not already imported (it should be) # Import the function if not already imported (it should be)
from aider.tools.grep import _execute_grep from aider.tools.grep import _execute_grep
result_message = _execute_grep(self, pattern, file_pattern, directory, use_regex, case_insensitive) result_message = _execute_grep(self, pattern, file_pattern, directory, use_regex, case_insensitive, context_before, context_after)
else: else:
result_message = "Error: Missing required 'pattern' parameter for Grep" result_message = "Error: Missing required 'pattern' parameter for Grep"

View file

@ -52,12 +52,14 @@ Act as an expert software engineer with the ability to autonomously navigate and
Find files containing a specific symbol (function, class, variable). **Found files are automatically added to context as read-only.** Find files containing a specific symbol (function, class, variable). **Found files are automatically added to context as read-only.**
Leverages the repo map for accurate symbol lookup. Leverages the repo map for accurate symbol lookup.
- **Grep**: `[tool_call(Grep, pattern="my_variable", file_pattern="*.py", directory="src", use_regex=False, case_insensitive=False)]` - **Grep**: `[tool_call(Grep, pattern="my_variable", file_pattern="*.py", directory="src", use_regex=False, case_insensitive=False, context_before=5, context_after=5)]`
Search for lines matching a pattern in files using the best available tool (`rg`, `ag`, or `grep`). Returns matching lines with line numbers. Search for lines matching a pattern in files using the best available tool (`rg`, `ag`, or `grep`). Returns matching lines with line numbers and context.
`file_pattern` (optional, default "*") filters files using glob syntax. `file_pattern` (optional, default "*") filters files using glob syntax.
`directory` (optional, default ".") specifies the search directory relative to the repo root. `directory` (optional, default ".") specifies the search directory relative to the repo root.
`use_regex` (optional, default False): If False, performs a literal/fixed string search. If True, uses basic Extended Regular Expression (ERE) syntax. `use_regex` (optional, default False): If False, performs a literal/fixed string search. If True, uses basic Extended Regular Expression (ERE) syntax.
`case_insensitive` (optional, default False): If False (default), the search is case-sensitive. If True, the search is case-insensitive. `case_insensitive` (optional, default False): If False (default), the search is case-sensitive. If True, the search is case-insensitive.
`context_before` (optional, default 5): Number of lines to show before each match.
`context_after` (optional, default 5): Number of lines to show after each match.
### Context Management Tools ### Context Management Tools
- **View**: `[tool_call(View, file_path="src/main.py")]` - **View**: `[tool_call(View, file_path="src/main.py")]`

View file

@ -52,12 +52,14 @@ Act as an expert software engineer with the ability to autonomously navigate and
Find files containing a specific symbol (function, class, variable). **Found files are automatically added to context as read-only.** Find files containing a specific symbol (function, class, variable). **Found files are automatically added to context as read-only.**
Leverages the repo map for accurate symbol lookup. Leverages the repo map for accurate symbol lookup.
- **Grep**: `[tool_call(Grep, pattern="my_variable", file_pattern="*.py", directory="src", use_regex=False, case_insensitive=False)]` - **Grep**: `[tool_call(Grep, pattern="my_variable", file_pattern="*.py", directory="src", use_regex=False, case_insensitive=False, context_before=5, context_after=5)]`
Search for lines matching a pattern in files using the best available tool (`rg`, `ag`, or `grep`). Returns matching lines with line numbers. Search for lines matching a pattern in files using the best available tool (`rg`, `ag`, or `grep`). Returns matching lines with line numbers and context.
`file_pattern` (optional, default "*") filters files using glob syntax. `file_pattern` (optional, default "*") filters files using glob syntax.
`directory` (optional, default ".") specifies the search directory relative to the repo root. `directory` (optional, default ".") specifies the search directory relative to the repo root.
`use_regex` (optional, default False): If False, performs a literal/fixed string search. If True, uses basic Extended Regular Expression (ERE) syntax. `use_regex` (optional, default False): If False, performs a literal/fixed string search. If True, uses basic Extended Regular Expression (ERE) syntax.
`case_insensitive` (optional, default False): If False (default), the search is case-sensitive. If True, the search is case-insensitive. `case_insensitive` (optional, default False): If False (default), the search is case-sensitive. If True, the search is case-insensitive.
`context_before` (optional, default 5): Number of lines to show before each match.
`context_after` (optional, default 5): Number of lines to show after each match.
### Context Management Tools ### Context Management Tools
- **View**: `[tool_call(View, file_path="src/main.py")]` - **View**: `[tool_call(View, file_path="src/main.py")]`

View file

@ -14,7 +14,7 @@ def _find_search_tool():
else: else:
return None, None return None, None
def _execute_grep(coder, pattern, file_pattern="*", directory=".", use_regex=False, case_insensitive=False): def _execute_grep(coder, pattern, file_pattern="*", directory=".", use_regex=False, case_insensitive=False, context_before=5, context_after=5):
""" """
Search for lines matching a pattern in files within the project repository. Search for lines matching a pattern in files within the project repository.
Uses rg (ripgrep), ag (the silver searcher), or grep, whichever is available. Uses rg (ripgrep), ag (the silver searcher), or grep, whichever is available.
@ -25,6 +25,9 @@ def _execute_grep(coder, pattern, file_pattern="*", directory=".", use_regex=Fal
file_pattern (str, optional): Glob pattern to filter files. Defaults to "*". file_pattern (str, optional): Glob pattern to filter files. Defaults to "*".
directory (str, optional): Directory to search within relative to repo root. Defaults to ".". directory (str, optional): Directory to search within relative to repo root. Defaults to ".".
use_regex (bool, optional): Whether the pattern is a regular expression. Defaults to False. use_regex (bool, optional): Whether the pattern is a regular expression. Defaults to False.
case_insensitive (bool, optional): Whether the search should be case-insensitive. Defaults to False.
context_before (int, optional): Number of context lines to show before matches. Defaults to 5.
context_after (int, optional): Number of context lines to show after matches. Defaults to 5.
Returns: Returns:
str: Formatted result indicating success or failure, including matching lines or error message. str: Formatted result indicating success or failure, including matching lines or error message.
@ -53,6 +56,14 @@ def _execute_grep(coder, pattern, file_pattern="*", directory=".", use_regex=Fal
cmd_args.append("-n") # Line numbers for rg and grep cmd_args.append("-n") # Line numbers for rg and grep
# ag includes line numbers by default # ag includes line numbers by default
# Context lines (Before and After)
if context_before > 0:
# All tools use -B for lines before
cmd_args.extend(["-B", str(context_before)])
if context_after > 0:
# All tools use -A for lines after
cmd_args.extend(["-A", str(context_after)])
# Case sensitivity # Case sensitivity
if case_insensitive: if case_insensitive:
cmd_args.append("-i") # Add case-insensitivity flag for all tools cmd_args.append("-i") # Add case-insensitivity flag for all tools