From 9f8fde24111c56f3f99eecbed401f87426300315 Mon Sep 17 00:00:00 2001 From: "Amar Sood (tekacs)" Date: Sat, 12 Apr 2025 13:57:06 -0400 Subject: [PATCH] Add context_before and context_after arguments to Grep tool --- aider/coders/navigator_coder.py | 5 ++++- aider/coders/navigator_legacy_prompts.py | 6 ++++-- aider/coders/navigator_prompts.py | 6 ++++-- aider/tools/grep.py | 13 ++++++++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/aider/coders/navigator_coder.py b/aider/coders/navigator_coder.py index 4f93f41aa..48f280864 100644 --- a/aider/coders/navigator_coder.py +++ b/aider/coders/navigator_coder.py @@ -769,11 +769,14 @@ class NavigatorCoder(Coder): directory = params.get('directory', '.') # Default to current directory use_regex = params.get('use_regex', False) # Default to literal search 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: # Import the function if not already imported (it should be) 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: result_message = "Error: Missing required 'pattern' parameter for Grep" diff --git a/aider/coders/navigator_legacy_prompts.py b/aider/coders/navigator_legacy_prompts.py index eb24d3a8d..2c5b60a46 100644 --- a/aider/coders/navigator_legacy_prompts.py +++ b/aider/coders/navigator_legacy_prompts.py @@ -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.** 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)]` - Search for lines matching a pattern in files using the best available tool (`rg`, `ag`, or `grep`). Returns matching lines with line numbers. +- **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 and context. `file_pattern` (optional, default "*") filters files using glob syntax. `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. `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 - **View**: `[tool_call(View, file_path="src/main.py")]` diff --git a/aider/coders/navigator_prompts.py b/aider/coders/navigator_prompts.py index ae1e82736..335fd3ec6 100644 --- a/aider/coders/navigator_prompts.py +++ b/aider/coders/navigator_prompts.py @@ -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.** 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)]` - Search for lines matching a pattern in files using the best available tool (`rg`, `ag`, or `grep`). Returns matching lines with line numbers. +- **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 and context. `file_pattern` (optional, default "*") filters files using glob syntax. `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. `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 - **View**: `[tool_call(View, file_path="src/main.py")]` diff --git a/aider/tools/grep.py b/aider/tools/grep.py index 79667459e..26f9581e6 100644 --- a/aider/tools/grep.py +++ b/aider/tools/grep.py @@ -14,7 +14,7 @@ def _find_search_tool(): else: 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. 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 "*". 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. + 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: 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 # 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 if case_insensitive: cmd_args.append("-i") # Add case-insensitivity flag for all tools