@@ -104,9 +104,29 @@ def apply(codeblock: str, content: str) -> str:
104
104
105
105
# TODO: maybe allow modified chunk to contain "// ..." to refer to chunks in the original,
106
106
# and then replace these with the original chunks?
107
+ re_placeholder = re .compile (r"^[ \t]*(#|//) \.\.\. ?.*$" , re .MULTILINE )
108
+ if re_placeholder .search (original ) or re_placeholder .search (modified ):
109
+ # raise ValueError("placeholders in modified chunk")
110
+ # split them by lines starting with "# ..."
111
+ originals = re_placeholder .split (original )
112
+ modifieds = re_placeholder .split (modified )
113
+ if len (originals ) != len (modifieds ):
114
+ raise ValueError (
115
+ "different number of placeholders in original and modified chunks"
116
+ f"\n { originals } \n { modifieds } "
117
+ )
118
+ new = content
119
+ for orig , mod in zip (originals , modifieds ):
120
+ if orig == mod :
121
+ continue
122
+ new = new .replace (orig , mod )
123
+ else :
124
+ if original not in content : # pragma: no cover
125
+ raise ValueError ("original chunk not found in file" , original )
126
+
127
+ # replace the original chunk with the modified chunk
128
+ new = content .replace (original , modified )
107
129
108
- # replace the original chunk with the modified chunk
109
- new = content .replace (original , modified )
110
130
if new == content : # pragma: no cover
111
131
raise ValueError ("patch did not change the file" )
112
132
0 commit comments