mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-31 01:35:00 +00:00
use valid_fnames to improve find_filename
This commit is contained in:
parent
ce2324c0c6
commit
8c766f81b2
1 changed files with 43 additions and 6 deletions
|
@ -23,7 +23,13 @@ class EditBlockCoder(Coder):
|
||||||
content = self.partial_response_content
|
content = self.partial_response_content
|
||||||
|
|
||||||
# might raise ValueError for malformed ORIG/UPD blocks
|
# might raise ValueError for malformed ORIG/UPD blocks
|
||||||
edits = list(find_original_update_blocks(content, self.fence))
|
edits = list(
|
||||||
|
find_original_update_blocks(
|
||||||
|
content,
|
||||||
|
self.fence,
|
||||||
|
self.get_inchat_relative_files(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
self.shell_commands += [edit[1] for edit in edits if edit[0] is None]
|
self.shell_commands += [edit[1] for edit in edits if edit[0] is None]
|
||||||
edits = [edit for edit in edits if edit[0] is not None]
|
edits = [edit for edit in edits if edit[0] is not None]
|
||||||
|
@ -414,7 +420,7 @@ def strip_filename(filename, fence):
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
def find_original_update_blocks(content, fence=DEFAULT_FENCE):
|
def find_original_update_blocks(content, fence=DEFAULT_FENCE, valid_fnames=None):
|
||||||
lines = content.splitlines(keepends=True)
|
lines = content.splitlines(keepends=True)
|
||||||
i = 0
|
i = 0
|
||||||
current_filename = None
|
current_filename = None
|
||||||
|
@ -454,7 +460,7 @@ def find_original_update_blocks(content, fence=DEFAULT_FENCE):
|
||||||
# Check for SEARCH/REPLACE blocks
|
# Check for SEARCH/REPLACE blocks
|
||||||
if line.strip() == HEAD:
|
if line.strip() == HEAD:
|
||||||
try:
|
try:
|
||||||
filename = find_filename(lines[max(0, i - 3) : i], fence)
|
filename = find_filename(lines[max(0, i - 3) : i], fence, valid_fnames)
|
||||||
if not filename:
|
if not filename:
|
||||||
if current_filename:
|
if current_filename:
|
||||||
filename = current_filename
|
filename = current_filename
|
||||||
|
@ -491,7 +497,7 @@ def find_original_update_blocks(content, fence=DEFAULT_FENCE):
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
def find_filename(lines, fence):
|
def find_filename(lines, fence, valid_fnames):
|
||||||
"""
|
"""
|
||||||
Deepseek Coder v2 has been doing this:
|
Deepseek Coder v2 has been doing this:
|
||||||
|
|
||||||
|
@ -505,19 +511,50 @@ def find_filename(lines, fence):
|
||||||
|
|
||||||
This is a more flexible search back for filenames.
|
This is a more flexible search back for filenames.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if valid_fnames is None:
|
||||||
|
valid_fnames = []
|
||||||
|
|
||||||
# Go back through the 3 preceding lines
|
# Go back through the 3 preceding lines
|
||||||
lines.reverse()
|
lines.reverse()
|
||||||
lines = lines[:3]
|
lines = lines[:3]
|
||||||
|
|
||||||
|
filenames = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
# If we find a filename, done
|
# If we find a filename, done
|
||||||
filename = strip_filename(line, fence)
|
filename = strip_filename(line, fence)
|
||||||
if filename:
|
if filename:
|
||||||
return filename
|
filenames.append(filename)
|
||||||
|
|
||||||
# Only continue as long as we keep seeing fences
|
# Only continue as long as we keep seeing fences
|
||||||
if not line.startswith(fence[0]):
|
if not line.startswith(fence[0]):
|
||||||
return
|
break
|
||||||
|
|
||||||
|
if not len(filenames):
|
||||||
|
return
|
||||||
|
|
||||||
|
if len(filenames) == 1:
|
||||||
|
return filenames[0]
|
||||||
|
|
||||||
|
# pick the *best* filename found
|
||||||
|
|
||||||
|
# pick a valid fname
|
||||||
|
for fname in filenames:
|
||||||
|
if fname in valid_fnames:
|
||||||
|
return fname
|
||||||
|
|
||||||
|
# match just on basename, return full valid fname
|
||||||
|
for fname in filenames:
|
||||||
|
for vfn in valid_fnames:
|
||||||
|
if fname == vfn.name:
|
||||||
|
return vfn
|
||||||
|
|
||||||
|
# look for a file w/extension
|
||||||
|
for fname in filenames:
|
||||||
|
if "." in fname:
|
||||||
|
return fname
|
||||||
|
|
||||||
|
return filenames[0]
|
||||||
|
|
||||||
|
|
||||||
def find_similar_lines(search_lines, content_lines, threshold=0.6):
|
def find_similar_lines(search_lines, content_lines, threshold=0.6):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue