From 42d48bcf351949f8a8993777f90be3af61bb1ea2 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 May 2023 19:36:51 -0700 Subject: [PATCH] aider: Added functionality to ask the user if they want to create a new file when no matching files are found. # Context: USER: Please note that I shared content of these additional files: aider/commands.py ASSISTANT: Yes, I see the content of the `aider/commands.py` file. If you need any changes or improvements to the code, please let me know. USER: in cmd_add if word matches no files, use Confirm.ask to ask the user if they would like to create the file. If so, touch the file and put it into matched_files so the for loop adds it ASSISTANT: To implement this feature, we will use `Confirm.ask` from the `prompt_toolkit` library to ask the user if they want to create the file. If the user confirms, we will create the file using `open` with the 'w' flag and add it to `matched_files` so that it gets added in the for loop. Here are the changes to the `aider/commands.py` file: ```python aider/commands.py <<<<<<< ORIGINAL from prompt_toolkit.completion import Completion from aider import prompts ======= from prompt_toolkit.completion import Completion from prompt_toolkit.shortcuts import Confirm from aider import prompts >>>>>>> UPDATED aider/commands.py <<<<<<< ORIGINAL for word in args.split(): matched_files = [file for file in files if word in file] if not matched_files: self.console.print(f"[red]No files matched '{word}'") ======= for word in args.split(): matched_files = [file for file in files if word in file] if not matched_files: create_file = Confirm.ask(f"No files matched '{word}'. Do you want to create ^C KeyboardInterrupt USER: in cmd_add if word matches no files, use rich.prompt.Confirm.ask to ask the user if they would like to create the file. If so, touch the file and put it into matched_files so the for loop adds it ASSISTANT: To implement this feature, we will use `Confirm.ask` from the `rich.prompt` library to ask the user if they want to create the file. If the user confirms, we will create the file using `open` with the 'w' flag and add it to `matched_files` so that it gets added in the for loop. Here are the changes to the `aider/commands.py` file: ```python aider/commands.py <<<<<<< ORIGINAL from rich.text import Text from prompt_toolkit.completion import Completion from aider import prompts ======= from rich.text import Text from rich.prompt import Confirm from prompt_toolkit.completion import Completion from aider import prompts >>>>>>> UPDATED aider/commands.py <<<<<<< ORIGINAL for word in args.split(): matched_files = [file for file in files if word in file] if not matched_files: self.console.print(f"[red]No files matched '{word}'") ======= for word in args.split(): matched_files = [file for file in files if word in file] if not matched_files: create_file = Confirm.ask(f"No files matched '{word}'. Do you want to create the file?") if create_file: with open(os.path.join(self.coder.root, word), 'w') as new_file: pass matched_files = [word] else: self.console.print(f"[red]No files matched '{word}'") >>>>>>> UPDATED ``` These changes import `Confirm` from `rich.prompt` and use it to ask the user if they want to create a new file when no matching files are found. If the user confirms, the new file is created and added to `matched_files`. --- aider/commands.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/aider/commands.py b/aider/commands.py index 9feaea58e..199986c87 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -1,5 +1,6 @@ import os from rich.text import Text +from rich.prompt import Confirm from prompt_toolkit.completion import Completion from aider import prompts @@ -149,7 +150,13 @@ class Commands: for word in args.split(): matched_files = [file for file in files if word in file] if not matched_files: - self.console.print(f"[red]No files matched '{word}'") + create_file = Confirm.ask(f"No files matched '{word}'. Do you want to create the file?") + if create_file: + with open(os.path.join(self.coder.root, word), 'w') as new_file: + pass + matched_files = [word] + else: + self.console.print(f"[red]No files matched '{word}'") for matched_file in matched_files: abs_file_path = os.path.abspath( os.path.join(self.coder.root, matched_file) @@ -185,6 +192,7 @@ class Commands: ] if not matched_files: self.console.print(f"[red]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)