aider/utils.py
Paul Gauthier 85c902843a Move quoted_file function from coder.py to utils.py and update get_files_content function in coder.py to use utils.quoted_file.
# Context:
USER: move quoted_file to utils
ASSISTANT: To move the `quoted_file` function from `coder.py` to `utils.py`, we will remove it from `coder.py` and add it to `utils.py`.

```python
coder.py
<<<<<<< ORIGINAL
    def quoted_file(self, fname):
        prompt = "\n"
        prompt += fname
        prompt += "\n```\n"
        prompt += Path(fname).read_text()
        prompt += "\n```\n"
        return prompt

    def get_files_content(self):
        prompt = ""
        for fname in self.fnames:
            prompt += self.quoted_file(fname)
        return prompt
=======
    def get_files_content(self):
        prompt = ""
        for fname in self.fnames:
            prompt += utils.quoted_file(fname)
        return prompt
>>>>>>> UPDATED
```

```python
utils.py
<<<<<<< ORIGINAL
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
=======
def quoted_file(fname):
    prompt = "\n"
    prompt += fname
    prompt += "\n```\n"
    prompt += Path(fname).read_text()
    prompt += "\n```\n"
    return prompt

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
>>>>>>> UPDATED
```

Now the `quoted_file` function has been moved to `utils.py`, and the `get_files_content` function in `coder.py` has been updated to use `utils.quoted_file`.
2023-05-09 00:00:48 -07:00

71 lines
No EOL
1.8 KiB
Python

import math
from difflib import SequenceMatcher
from pathlib import Path
# from dump import dump
def replace_most_similar_chunk(whole, part, replace):
similarity_thresh = 0.8
max_similarity = 0
most_similar_chunk_start = -1
most_similar_chunk_end = -1
whole_lines = whole.splitlines()
part_lines = part.splitlines()
scale = 0.1
min_len = math.floor(len(part_lines) * (1 - scale))
max_len = math.ceil(len(part_lines) * (1 + scale))
for length in range(min_len, max_len):
for i in range(len(whole_lines) - length + 1):
chunk = whole_lines[i : i + length + 1]
chunk = "\n".join(chunk)
similarity = SequenceMatcher(None, chunk, part).ratio()
if similarity > max_similarity and similarity:
max_similarity = similarity
most_similar_chunk_start = i
most_similar_chunk_end = i + length + 1
if max_similarity < similarity_thresh:
return
replace_lines = replace.splitlines()
modified_whole = (
whole_lines[:most_similar_chunk_start]
+ replace_lines
+ whole_lines[most_similar_chunk_end:]
)
modified_whole = "\n".join(modified_whole)
return modified_whole
def quoted_file(fname):
prompt = "\n"
prompt += fname
prompt += "\n```\n"
prompt += Path(fname).read_text()
prompt += "\n```\n"
return prompt
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