mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-25 14:55:00 +00:00
WIP: Refactor do_replace method to utils module and update references in coder module.
This commit is contained in:
parent
11a5379d61
commit
fb12b73975
2 changed files with 30 additions and 31 deletions
34
coder.py
34
coder.py
|
@ -377,41 +377,13 @@ class Coder:
|
||||||
self.fnames[path] = 0
|
self.fnames[path] = 0
|
||||||
|
|
||||||
edited.add(path)
|
edited.add(path)
|
||||||
if self.do_replace(path, original, updated):
|
if utils.do_replace(path, original, updated):
|
||||||
self.console.print(f"[red]Applied edit to {fname}")
|
self.console.print(f"[red]Applied edit to {path}")
|
||||||
else:
|
else:
|
||||||
self.console.print(f"[red]Failed to apply edit to {fname}")
|
self.console.print(f"[red]Failed to apply edit to {path}")
|
||||||
|
|
||||||
return edited
|
return edited
|
||||||
|
|
||||||
def do_replace(self, fname, before_text, after_text):
|
|
||||||
before_text = utils.strip_quoted_wrapping(before_text, fname)
|
|
||||||
after_text = utils.strip_quoted_wrapping(after_text, fname)
|
|
||||||
fname = Path(fname)
|
|
||||||
|
|
||||||
# does it want to make a new file?
|
|
||||||
if not fname.exists() and not before_text.strip():
|
|
||||||
print("Creating empty file:", fname)
|
|
||||||
fname.touch()
|
|
||||||
|
|
||||||
content = fname.read_text()
|
|
||||||
|
|
||||||
if not before_text.strip():
|
|
||||||
if content:
|
|
||||||
new_content = content + after_text
|
|
||||||
else:
|
|
||||||
# first populating an empty file
|
|
||||||
new_content = after_text
|
|
||||||
else:
|
|
||||||
new_content = utils.replace_most_similar_chunk(
|
|
||||||
content, before_text, after_text
|
|
||||||
)
|
|
||||||
if not new_content:
|
|
||||||
return
|
|
||||||
|
|
||||||
fname.write_text(new_content)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def commit(self, history=None, prefix=None, ask=False):
|
def commit(self, history=None, prefix=None, ask=False):
|
||||||
repo = self.repo
|
repo = self.repo
|
||||||
if not repo:
|
if not repo:
|
||||||
|
|
27
utils.py
27
utils.py
|
@ -70,3 +70,30 @@ def strip_quoted_wrapping(res, fname=None):
|
||||||
res += "\n"
|
res += "\n"
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
def do_replace(fname, before_text, after_text):
|
||||||
|
before_text = strip_quoted_wrapping(before_text, fname)
|
||||||
|
after_text = strip_quoted_wrapping(after_text, fname)
|
||||||
|
fname = Path(fname)
|
||||||
|
|
||||||
|
# does it want to make a new file?
|
||||||
|
if not fname.exists() and not before_text.strip():
|
||||||
|
print("Creating empty file:", fname)
|
||||||
|
fname.touch()
|
||||||
|
|
||||||
|
content = fname.read_text()
|
||||||
|
|
||||||
|
if not before_text.strip():
|
||||||
|
if content:
|
||||||
|
new_content = content + after_text
|
||||||
|
else:
|
||||||
|
# first populating an empty file
|
||||||
|
new_content = after_text
|
||||||
|
else:
|
||||||
|
new_content = replace_most_similar_chunk(
|
||||||
|
content, before_text, after_text
|
||||||
|
)
|
||||||
|
if not new_content:
|
||||||
|
return
|
||||||
|
|
||||||
|
fname.write_text(new_content)
|
||||||
|
return True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue