mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 06:15:00 +00:00
Handle the case of first populating an empty file
This commit is contained in:
parent
d50b4c4de2
commit
cfd415c8f2
2 changed files with 18 additions and 11 deletions
28
coder.py
28
coder.py
|
@ -296,13 +296,14 @@ class Coder:
|
||||||
return ''.join(resp)
|
return ''.join(resp)
|
||||||
|
|
||||||
|
|
||||||
pattern = re.compile(r'(\S+)\s+<<<<<<< ORIGINAL\n(.*?)\n=======\n(.*?\n?)>>>>>>> UPDATED', re.MULTILINE | re.DOTALL)
|
pattern = re.compile(r'(\S+)\s+(```)?<<<<<<< ORIGINAL\n(.*?\n?)=======\n(.*?\n?)>>>>>>> UPDATED', re.MULTILINE | re.DOTALL)
|
||||||
|
|
||||||
def update_files(self, content, inp):
|
def update_files(self, content, inp):
|
||||||
|
|
||||||
edited = set()
|
edited = set()
|
||||||
for match in self.pattern.finditer(content):
|
for match in self.pattern.finditer(content):
|
||||||
path, original, updated = match.groups()
|
path, _, original, updated = match.groups()
|
||||||
|
|
||||||
edited.add(path)
|
edited.add(path)
|
||||||
if self.do_replace(path, original, updated):
|
if self.do_replace(path, original, updated):
|
||||||
continue
|
continue
|
||||||
|
@ -317,17 +318,22 @@ class Coder:
|
||||||
|
|
||||||
fname = Path(fname)
|
fname = Path(fname)
|
||||||
content = fname.read_text().splitlines()
|
content = fname.read_text().splitlines()
|
||||||
before_lines = [l.strip() for l in before_text.splitlines()]
|
|
||||||
stripped_content = [l.strip() for l in content]
|
|
||||||
where = find_index(stripped_content, before_lines)
|
|
||||||
|
|
||||||
if where < 0:
|
if not before_text and not content:
|
||||||
return
|
# first populating an empty file
|
||||||
|
new_content = after_text
|
||||||
|
else:
|
||||||
|
before_lines = [l.strip() for l in before_text.splitlines()]
|
||||||
|
stripped_content = [l.strip() for l in content]
|
||||||
|
where = find_index(stripped_content, before_lines)
|
||||||
|
|
||||||
new_content = content[:where]
|
if where < 0:
|
||||||
new_content += after_text.splitlines()
|
return
|
||||||
new_content += content[where+len(before_lines):]
|
|
||||||
new_content = '\n'.join(new_content) + '\n'
|
new_content = content[:where]
|
||||||
|
new_content += after_text.splitlines()
|
||||||
|
new_content += content[where+len(before_lines):]
|
||||||
|
new_content = '\n'.join(new_content) + '\n'
|
||||||
|
|
||||||
fname.write_text(new_content)
|
fname.write_text(new_content)
|
||||||
print('Applied edit to', fname)
|
print('Applied edit to', fname)
|
||||||
|
|
|
@ -28,6 +28,7 @@ some/dir/names/example.py
|
||||||
>>>>>>> UPDATED
|
>>>>>>> UPDATED
|
||||||
|
|
||||||
Be sure to include the correct path and filename for each edit, exactly as specified by the user.
|
Be sure to include the correct path and filename for each edit, exactly as specified by the user.
|
||||||
|
Don't use ``` to demarcate code blocks!
|
||||||
LEADING WHITESPACE IS IMPORTANT IN ALL CODE!
|
LEADING WHITESPACE IS IMPORTANT IN ALL CODE!
|
||||||
NEVER RETURN THE ENTIRE SOURCE FILE. ONLY RETURN CODE IN ORIGINAL/UPDATED FORMAT CHANGES!
|
NEVER RETURN THE ENTIRE SOURCE FILE. ONLY RETURN CODE IN ORIGINAL/UPDATED FORMAT CHANGES!
|
||||||
'''
|
'''
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue