mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 22:34:59 +00:00
refactor to use io.write_text
This commit is contained in:
parent
0ea52b412f
commit
b46dffbb20
5 changed files with 16 additions and 15 deletions
|
@ -924,7 +924,7 @@ class Coder:
|
|||
|
||||
if full_path in self.abs_fnames:
|
||||
if not self.dry_run and write_content:
|
||||
Path(full_path).write_text(write_content)
|
||||
self.io.write_text(full_path, write_content)
|
||||
return full_path
|
||||
|
||||
if not Path(full_path).exists():
|
||||
|
@ -950,7 +950,7 @@ class Coder:
|
|||
self.repo.git.add(full_path)
|
||||
|
||||
if not self.dry_run and write_content:
|
||||
Path(full_path).write_text(write_content)
|
||||
self.io.write_text(full_path, write_content)
|
||||
|
||||
return full_path
|
||||
|
||||
|
|
|
@ -27,7 +27,10 @@ class EditBlockCoder(Coder):
|
|||
if not full_path:
|
||||
continue
|
||||
content = self.io.read_text(full_path)
|
||||
if do_replace(full_path, content, original, updated, self.dry_run):
|
||||
content = do_replace(full_path, content, original, updated)
|
||||
if content:
|
||||
if not self.dry_run:
|
||||
self.io.write_text(full_path, content)
|
||||
edited.add(path)
|
||||
continue
|
||||
self.io.tool_error(f"Failed to apply edit to {path}")
|
||||
|
@ -212,7 +215,7 @@ def strip_quoted_wrapping(res, fname=None):
|
|||
return res
|
||||
|
||||
|
||||
def do_replace(fname, content, before_text, after_text, dry_run=False):
|
||||
def do_replace(fname, content, before_text, after_text):
|
||||
before_text = strip_quoted_wrapping(before_text, fname)
|
||||
after_text = strip_quoted_wrapping(after_text, fname)
|
||||
fname = Path(fname)
|
||||
|
@ -230,13 +233,8 @@ def do_replace(fname, content, before_text, after_text, dry_run=False):
|
|||
new_content = content + after_text
|
||||
else:
|
||||
new_content = replace_most_similar_chunk(content, before_text, after_text)
|
||||
if not new_content:
|
||||
return
|
||||
|
||||
if not dry_run:
|
||||
fname.write_text(new_content)
|
||||
|
||||
return True
|
||||
return new_content
|
||||
|
||||
|
||||
ORIGINAL = "<<<<<<< ORIGINAL"
|
||||
|
|
|
@ -136,7 +136,10 @@ class EditBlockFunctionCoder(Coder):
|
|||
if not full_path:
|
||||
continue
|
||||
content = self.io.read_text(full_path)
|
||||
if do_replace(full_path, content, original, updated, self.dry_run):
|
||||
content = do_replace(full_path, content, original, updated)
|
||||
if content:
|
||||
if not self.dry_run:
|
||||
self.io.write_text(full_path, content)
|
||||
edited.add(path)
|
||||
continue
|
||||
self.io.tool_error(f"Failed to apply edit to {path}")
|
||||
|
|
|
@ -66,7 +66,7 @@ class WholeFileCoder(Coder):
|
|||
edited.add(fname)
|
||||
if not self.dry_run:
|
||||
new_lines = "".join(new_lines)
|
||||
full_path.write_text(new_lines)
|
||||
self.io.write_text(full_path, new_lines)
|
||||
|
||||
fname = None
|
||||
new_lines = []
|
||||
|
@ -125,6 +125,6 @@ class WholeFileCoder(Coder):
|
|||
edited.add(fname)
|
||||
if not self.dry_run:
|
||||
new_lines = "".join(new_lines)
|
||||
Path(full_path).write_text(new_lines)
|
||||
self.io.write_text(full_path, new_lines)
|
||||
|
||||
return edited
|
||||
|
|
|
@ -137,14 +137,14 @@ class InputOutput:
|
|||
|
||||
def read_text(self, filename):
|
||||
try:
|
||||
with open(filename, "r", encoding=self.encoding) as f:
|
||||
with open(str(filename), "r", encoding=self.encoding) as f:
|
||||
return f.read()
|
||||
except (FileNotFoundError, UnicodeError) as e:
|
||||
self.tool_error(f"{filename}: {e}")
|
||||
return
|
||||
|
||||
def write_text(self, filename, content):
|
||||
with open(filename, "w", encoding=self.encoding) as f:
|
||||
with open(str(filename), "w", encoding=self.encoding) as f:
|
||||
f.write(content)
|
||||
|
||||
def get_input(self, root, rel_fnames, addable_rel_fnames, commands):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue