fix: patch tool

pull/12/head
hanchchch 1 year ago
parent 9ad753cae7
commit a72fa29c8e

@ -106,6 +106,12 @@ class CodeEditor(BaseToolSet):
)
+ "Each patch has to be formatted like below.\n"
"<filepath>|<start_line>,<start_col>|<end_line>,<end_col>|<new_code>"
"Here is an example. If the original code is:\n"
"print('hello world')\n"
"and you want to change it to:\n"
"print('hi corca')\n"
"then the patch should be:\n"
"test.py|1,8|1,19|hi corca\n"
"Code between start and end will be replaced with new_code. "
"The output will be written/deleted bytes or error message. ",
)

@ -57,6 +57,7 @@ test.py|11,16|11,16|_titles
"""
import os
import re
from pathlib import Path
from typing import Tuple
@ -70,10 +71,13 @@ class Position:
self.line: int = line
self.col: int = col
def __str__(self):
return f"(Ln {self.line}, Col {self.col})"
@staticmethod
def from_str(pos: str) -> "Position":
line, col = pos.split(Position.separator)
return Position(int(line), int(col))
return Position(int(line) - 1, int(col) - 1)
class PatchCommand:
@ -122,10 +126,22 @@ class PatchCommand:
@staticmethod
def from_str(command: str) -> "PatchCommand":
filepath, start, end = command.split(PatchCommand.separator)[:3]
content = command[len(filepath + start + end) + 3 :]
match = re.search(
r"(.*)\|([0-9])*,([0-9])*\|([0-9]*),([0-9]*)(\||\n)(.*)",
command,
re.DOTALL,
)
filepath = match.group(1)
start_line = match.group(2)
start_col = match.group(3)
end_line = match.group(4)
end_col = match.group(5)
content = match.group(7)
return PatchCommand(
filepath, Position.from_str(start), Position.from_str(end), content
filepath,
Position.from_str(f"{start_line},{start_col}"),
Position.from_str(f"{end_line},{end_col}"),
content,
)

Loading…
Cancel
Save