mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 06:15:00 +00:00
WIP: Refactor coder.py to use utils.replace_most_similar_chunk and add utils.strip_quoted_wrapping.
This commit is contained in:
parent
05c39b7329
commit
1b4266c33c
2 changed files with 28 additions and 5 deletions
13
coder.py
13
coder.py
|
@ -16,7 +16,7 @@ from dotenv import load_dotenv
|
|||
from tqdm import tqdm
|
||||
|
||||
from pathlib import Path
|
||||
from utils import replace_most_similar_chunk
|
||||
import utils
|
||||
|
||||
import os
|
||||
import git
|
||||
|
@ -418,11 +418,14 @@ class Coder:
|
|||
|
||||
content = fname.read_text()
|
||||
|
||||
if not before_text and not content:
|
||||
# first populating an empty file
|
||||
new_content = after_text
|
||||
if not before_text:
|
||||
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)
|
||||
new_content = utils.replace_most_similar_chunk(content, before_text, after_text)
|
||||
if not new_content:
|
||||
self.console.print(f"[red]Failed to apply edit to {fname}")
|
||||
return
|
||||
|
|
20
utils.py
20
utils.py
|
@ -1,6 +1,7 @@
|
|||
import math
|
||||
|
||||
from difflib import SequenceMatcher
|
||||
from pathlib import Path
|
||||
|
||||
# from dump import dump
|
||||
|
||||
|
@ -41,3 +42,22 @@ def replace_most_similar_chunk(whole, part, replace):
|
|||
)
|
||||
modified_whole = "\n".join(modified_whole)
|
||||
return modified_whole
|
||||
|
||||
|
||||
def strip_quoted_wrapping(res, fname=None):
|
||||
if not res:
|
||||
return res
|
||||
|
||||
res = res.splitlines()
|
||||
|
||||
if fname and res[0].strip().endswith(Path(fname).name):
|
||||
res = res[1:]
|
||||
|
||||
if res[0].startswith("```") and res[-1].startswith("```"):
|
||||
res = res[1:-1]
|
||||
|
||||
res = "\n".join(res)
|
||||
if res and res[-1] != "\n":
|
||||
res += "\n"
|
||||
|
||||
return res
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue