mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-01 10:14:59 +00:00
on search/replace error ask the LLM did you mean?
This commit is contained in:
parent
4841f318c1
commit
cd78080fa5
1 changed files with 30 additions and 11 deletions
|
@ -57,8 +57,6 @@ class EditBlockCoder(Coder):
|
||||||
full_path = self.abs_root_path(path)
|
full_path = self.abs_root_path(path)
|
||||||
content = self.io.read_text(full_path)
|
content = self.io.read_text(full_path)
|
||||||
|
|
||||||
did_you_mean = find_similar_lines(original, content)
|
|
||||||
|
|
||||||
res += f"""
|
res += f"""
|
||||||
## SearchReplaceNoExactMatch: This SEARCH block failed to exactly match lines in {path}
|
## SearchReplaceNoExactMatch: This SEARCH block failed to exactly match lines in {path}
|
||||||
<<<<<<< SEARCH
|
<<<<<<< SEARCH
|
||||||
|
@ -66,10 +64,20 @@ class EditBlockCoder(Coder):
|
||||||
=======
|
=======
|
||||||
{updated}
|
{updated}
|
||||||
>>>>>>> REPLACE
|
>>>>>>> REPLACE
|
||||||
{did_you_mean}
|
|
||||||
"""
|
"""
|
||||||
|
did_you_mean = find_similar_lines(original, content)
|
||||||
|
if did_you_mean:
|
||||||
|
res += f"""Did you mean to match some of these actual lines from {path}?
|
||||||
|
|
||||||
|
{self.fence[0]}
|
||||||
|
{did_you_mean}
|
||||||
|
{self.fence[1]}
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
res += (
|
res += (
|
||||||
"\nThe SEARCH section must exactly match an existing block of lines including all white"
|
"The SEARCH section must exactly match an existing block of lines including all white"
|
||||||
" space, comments, indentation, docstrings, etc\n"
|
" space, comments, indentation, docstrings, etc\n"
|
||||||
)
|
)
|
||||||
if passed:
|
if passed:
|
||||||
|
@ -465,21 +473,32 @@ Tooooo
|
||||||
Hope you like it!
|
Hope you like it!
|
||||||
"""
|
"""
|
||||||
print(list(find_original_update_blocks(edit)))
|
print(list(find_original_update_blocks(edit)))
|
||||||
|
|
||||||
|
|
||||||
def find_similar_lines(search_lines, content_lines, threshold=0.8):
|
def find_similar_lines(search_lines, content_lines, threshold=0.8):
|
||||||
search_lines = search_lines.splitlines()
|
search_lines = search_lines.splitlines()
|
||||||
content_lines = content_lines.splitlines()
|
content_lines = content_lines.splitlines()
|
||||||
|
|
||||||
best_ratio = 0
|
best_ratio = 0
|
||||||
best_match = None
|
best_match = None
|
||||||
|
|
||||||
for i in range(len(content_lines) - len(search_lines) + 1):
|
for i in range(len(content_lines) - len(search_lines) + 1):
|
||||||
chunk = content_lines[i:i+len(search_lines)]
|
chunk = content_lines[i : i + len(search_lines)]
|
||||||
ratio = SequenceMatcher(None, search_lines, chunk).ratio()
|
ratio = SequenceMatcher(None, search_lines, chunk).ratio()
|
||||||
if ratio > best_ratio:
|
if ratio > best_ratio:
|
||||||
best_ratio = ratio
|
best_ratio = ratio
|
||||||
best_match = chunk
|
best_match = chunk
|
||||||
|
best_match_i = i
|
||||||
if best_ratio >= threshold:
|
|
||||||
return "\n".join(best_match)
|
if best_ratio < threshold:
|
||||||
else:
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
if best_match[0] == search_lines[0] and best_match[-1] == search_lines[-1]:
|
||||||
|
return "\n".join(best_match)
|
||||||
|
|
||||||
|
N = 5
|
||||||
|
best_match_end = min(len(content_lines), best_match_i + len(search_lines) + N)
|
||||||
|
best_match_i = max(0, best_match_i - N)
|
||||||
|
|
||||||
|
best = content_lines[best_match_i:best_match_end]
|
||||||
|
return "\n".join(best)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue