aider: Refactored cmd_drop to use glob for matching files against the given word.

# Context:
USER: have `cmd_drop` also glob against the `files`
ASSISTANT: To implement this change, we need to modify the `aider/commands.py` file.

Here's the step-by-step process:

1. In the `cmd_drop` method, use the `glob.glob` function to match the word against the `files`.
2. If no files match, print an error message.
3. If files match, remove them from the chat session.

Here's the code change:

```python
aider/commands.py
<<<<<<< ORIGINAL
    def cmd_drop(self, args):
        "Remove matching files from the chat session"

        if not args.strip():
            self.io.tool_output("Dropping all files from the chat session.")
            self.coder.abs_fnames = set()

        for word in args.split():
            matched_files = [
                file
                for file in self.coder.abs_fnames
                if word.lower() in os.path.relpath(file, self.coder.root).lower()
            ]
            if not matched_files:
                self.io.tool_error(f"No files matched '{word}'")

            for matched_file in matched_files:
                relative_fname = os.path.relpath(matched_file, self.coder.root)
                self.coder.abs_fnames.remove(matched_file)
                self.io.tool_output(f"Removed {relative_fname} from the chat")
=======
    def cmd_drop(self, args):
        "Remove matching files from the chat session"

        if not args.strip():
            self.io.tool_output("Dropping all files from the chat session.")
            self.coder.abs_fnames = set()

        for word in args.split():
            matched_files = [
                file
                for file in self.coder.abs_fnames
                if any(glob.fnmatch.fnmatch(os.path.relpath(file, self.coder.root), word) for file in self.coder.abs_fnames)
            ]
            if not matched_files:
                self.io.tool_error(f"No files matched '{word}'")

            for matched_file in matched_files:
                relative_fname = os.path.relpath(matched_file, self.coder.root)
                self.coder.abs_fnames.remove(matched_file)
                self.io.tool_output(f"Removed {relative_fname} from the chat")
>>>>>>> UPDATED
```
Please let me know if you need any further changes.
This commit is contained in:
Paul Gauthier 2023-07-05 21:38:44 -07:00
parent 5cd7d9d743
commit ad5cdc463a

View file

@ -288,7 +288,7 @@ class Commands:
matched_files = [
file
for file in self.coder.abs_fnames
if word.lower() in os.path.relpath(file, self.coder.root).lower()
if any(glob.fnmatch.fnmatch(os.path.relpath(file, self.coder.root), word) for file in self.coder.abs_fnames)
]
if not matched_files:
self.io.tool_error(f"No files matched '{word}'")