refactor to enumerate files to be edited, then make the edits

This commit is contained in:
Paul Gauthier 2023-08-17 10:07:22 -07:00
parent 43047c3835
commit 2455676a44
3 changed files with 42 additions and 31 deletions

View file

@ -22,11 +22,11 @@ class WholeFileCoder(Coder):
def render_incremental_response(self, final):
try:
return self.update_files(mode="diff")
return self.get_edits(mode="diff")
except ValueError:
return self.partial_response_content
def update_files(self, mode="update"):
def get_edits(self, mode="update"):
content = self.partial_response_content
chat_files = self.get_inchat_relative_files()
@ -104,22 +104,26 @@ class WholeFileCoder(Coder):
if fname:
edits.append((fname, fname_source, new_lines))
edited = set()
seen = set()
refined_edits = []
# process from most reliable filename, to least reliable
for source in ("block", "saw", "chat"):
for fname, fname_source, new_lines in edits:
if fname_source != source:
continue
# if a higher priority source already edited the file, skip
if fname in edited:
if fname in seen:
continue
# we have a winner
new_lines = "".join(new_lines)
if self.allowed_to_edit(fname, new_lines):
edited.add(fname)
seen.add(fname)
refined_edits.append((fname, fname_source, new_lines))
return edited
return refined_edits
def apply_edits(self, edits):
for path, full_path, fname_source, new_lines in edits:
new_lines = "".join(new_lines)
self.io.write_text(full_path, new_lines)
def do_live_diff(self, full_path, new_lines, final):
if full_path.exists():