From 874b5f8c7a18f9d51161d038935dd8e5466b6de9 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 5 Jul 2023 21:28:53 -0700 Subject: [PATCH] aider: The `cmd_add` method in the `Commands` class has been modified to allow the `/add` command to accept glob patterns. # Context: USER: Feature Description Currently, the /add function accepts directory paths to add the entire directory or spefic file paths to the program's context. A useful enhancement would be to support the use of regex or glob expressions with the /add function. This would give users more flexibility and control to specify exactly which files to include. Proposed Solution Allow the /add function to interpret glob or regex patterns. For example, a user could use /add src/*.py to include only Python files from the src directory or /add src/[A-C]* to add only files that start with A, B, or C. Expected Behavior When a user runs /add with a regex or glob pattern, the program should add to the context only the files that match the pattern. The program should handle non-matching files or non-existent paths gracefully, preferably by ignoring them and printing a warning message. Alternatives Considered An alternative could be providing an additional function, say /addPattern, that accepts regex/glob patterns. This approach would leave the existing /add functionality unchanged for users who don't need the enhanced functionality. Additional Context Adding this feature would boost the flexibility of the /add function, making the program more convenient to use for a variety of tasks. Users could, for instance, exclude certain types of files or select a subset of files based on their naming conventions. ASSISTANT: To implement this feature, we need to modify the `cmd_add` method in the `Commands` class in the `aider/commands.py` file. The changes will involve: 1. Interpreting the input argument as a glob pattern. 2. Using the `glob` module to find all files that match the pattern. 3. Adding the matched files to the chat session. Here is the proposed change: ```python aider/commands.py <<<<<<< ORIGINAL def cmd_add(self, args): "Add matching files to the chat session" added_fnames = [] files = self.coder.get_all_relative_files() for word in args.split(): matched_files = [file for file in files if word in file] ======= import glob def cmd_add(self, args): "Add matching files to the chat session using glob patterns" added_fnames = [] for pattern in args.split(): matched_files = glob.glob(pattern, recursive=True) >>>>>>> UPDATED ``` This change will allow the `/add` command to accept glob patterns. For example, `/add src/*.py` will add all Python files in the `src` directory to the chat session. --- aider/commands.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 29f4bc63b..b053cba4e 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -216,13 +216,14 @@ class Commands: if partial.lower() in fname.lower(): yield Completion(fname, start_position=-len(partial)) + import glob + def cmd_add(self, args): - "Add matching files to the chat session" + "Add matching files to the chat session using glob patterns" added_fnames = [] - files = self.coder.get_all_relative_files() - for word in args.split(): - matched_files = [file for file in files if word in file] + for pattern in args.split(): + matched_files = glob.glob(pattern, recursive=True) if not matched_files: if self.coder.repo is not None: