mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 22:34:59 +00:00
Handle missing filenames by scanning for mentions
in the text
This commit is contained in:
parent
f2a03e917d
commit
ac560c2877
3 changed files with 61 additions and 7 deletions
|
@ -167,9 +167,10 @@ class Coder:
|
||||||
self.io.tool_output(f"Added {fname} to the chat.")
|
self.io.tool_output(f"Added {fname} to the chat.")
|
||||||
|
|
||||||
def find_common_root(self):
|
def find_common_root(self):
|
||||||
if self.abs_fnames:
|
if len(self.abs_fnames) == 1:
|
||||||
common_prefix = os.path.commonpath(list(self.abs_fnames))
|
self.root = os.path.dirname(list(self.abs_fnames)[0])
|
||||||
self.root = os.path.dirname(common_prefix)
|
elif self.abs_fnames:
|
||||||
|
self.root = os.path.commonpath(list(self.abs_fnames))
|
||||||
else:
|
else:
|
||||||
self.root = os.getcwd()
|
self.root = os.getcwd()
|
||||||
|
|
||||||
|
|
|
@ -32,13 +32,15 @@ class WholeFileCoder(Coder):
|
||||||
output = []
|
output = []
|
||||||
lines = content.splitlines(keepends=True)
|
lines = content.splitlines(keepends=True)
|
||||||
|
|
||||||
|
saw_fname = None
|
||||||
fname = None
|
fname = None
|
||||||
new_lines = []
|
new_lines = []
|
||||||
for i, line in enumerate(lines):
|
for i, line in enumerate(lines):
|
||||||
dump(repr(fname), repr(line), repr(new_lines))
|
|
||||||
if line.startswith("```"):
|
if line.startswith("```"):
|
||||||
if fname is not None:
|
if fname is not None:
|
||||||
# ending an existing block
|
# ending an existing block
|
||||||
|
saw_fname = None
|
||||||
|
|
||||||
full_path = (Path(self.root) / fname).absolute()
|
full_path = (Path(self.root) / fname).absolute()
|
||||||
|
|
||||||
if mode == "diff" and full_path.exists():
|
if mode == "diff" and full_path.exists():
|
||||||
|
@ -65,15 +67,24 @@ class WholeFileCoder(Coder):
|
||||||
if i > 0:
|
if i > 0:
|
||||||
fname = lines[i - 1].strip()
|
fname = lines[i - 1].strip()
|
||||||
if not fname: # blank line? or ``` was on first line i==0
|
if not fname: # blank line? or ``` was on first line i==0
|
||||||
if len(chat_files) == 1:
|
if saw_fname:
|
||||||
|
fname = saw_fname
|
||||||
|
elif len(chat_files) == 1:
|
||||||
fname = chat_files[0]
|
fname = chat_files[0]
|
||||||
else:
|
else:
|
||||||
# TODO: sense which file it is by diff size
|
# TODO: sense which file it is by diff size
|
||||||
raise ValueError("No filename provided before ``` block")
|
raise ValueError("No filename provided before ``` in file listing")
|
||||||
|
|
||||||
elif fname is not None:
|
elif fname is not None:
|
||||||
new_lines.append(line)
|
new_lines.append(line)
|
||||||
else:
|
else:
|
||||||
|
for word in line.strip().split():
|
||||||
|
word = word.rstrip(".:,;!")
|
||||||
|
for chat_file in chat_files:
|
||||||
|
quoted_chat_file = f"`{chat_file}`"
|
||||||
|
if word == quoted_chat_file:
|
||||||
|
saw_fname = chat_file
|
||||||
|
|
||||||
output.append(line)
|
output.append(line)
|
||||||
|
|
||||||
if mode == "diff":
|
if mode == "diff":
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from aider import models
|
from aider import models
|
||||||
from aider.coders.wholefile_coder import WholeFileCoder
|
from aider.coders.wholefile_coder import WholeFileCoder
|
||||||
|
@ -68,7 +69,7 @@ class TestWholeFileCoder(unittest.TestCase):
|
||||||
updated_content = f.read()
|
updated_content = f.read()
|
||||||
self.assertEqual(updated_content, "Updated content\n")
|
self.assertEqual(updated_content, "Updated content\n")
|
||||||
|
|
||||||
def test_update_files_earlier_filename(self):
|
def test_update_files_no_filename_single_file_in_chat(self):
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
os.chdir(temp_dir)
|
os.chdir(temp_dir)
|
||||||
|
|
||||||
|
@ -104,6 +105,47 @@ class TestWholeFileCoder(unittest.TestCase):
|
||||||
updated_content = f.read()
|
updated_content = f.read()
|
||||||
self.assertEqual(updated_content, content)
|
self.assertEqual(updated_content, content)
|
||||||
|
|
||||||
|
def test_update_files_earlier_filename(self):
|
||||||
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
|
os.chdir(temp_dir)
|
||||||
|
print(temp_dir)
|
||||||
|
|
||||||
|
fname_a = Path("a.txt")
|
||||||
|
fname_b = Path("b.txt")
|
||||||
|
|
||||||
|
fname_a.write_text("before a\n")
|
||||||
|
fname_b.write_text("before b\n")
|
||||||
|
|
||||||
|
response = """
|
||||||
|
Here is a new version of `a.txt` for you to consider:
|
||||||
|
|
||||||
|
```
|
||||||
|
after a
|
||||||
|
```
|
||||||
|
|
||||||
|
And here is `b.txt`:
|
||||||
|
|
||||||
|
```
|
||||||
|
after b
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
# Initialize WholeFileCoder with the temporary directory
|
||||||
|
io = InputOutput(yes=True)
|
||||||
|
coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[fname_a, fname_b])
|
||||||
|
|
||||||
|
# Set the partial response content with the updated content
|
||||||
|
coder.partial_response_content = response
|
||||||
|
|
||||||
|
# Call update_files method
|
||||||
|
edited_files = coder.update_files()
|
||||||
|
|
||||||
|
# Check if the sample file was updated
|
||||||
|
self.assertIn(str(fname_a), edited_files)
|
||||||
|
self.assertIn(str(fname_b), edited_files)
|
||||||
|
|
||||||
|
self.assertEqual(fname_a.read_text(), "after a\n")
|
||||||
|
self.assertEqual(fname_b.read_text(), "after b\n")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue