| 137 | # -- Smart edit application ---------------------------------------------------- |
| 138 | |
| 139 | def _apply_edit(original: str, old_string: str, new_string: str, replace_all: bool) -> str: |
| 140 | if new_string != "": |
| 141 | if replace_all: |
| 142 | return original.replace(old_string, new_string) |
| 143 | idx = original.index(old_string) |
| 144 | return original[:idx] + new_string + original[idx + len(old_string):] |
| 145 | |
| 146 | strip_trailing_newline = ( |
| 147 | not old_string.endswith("\n") and (old_string + "\n") in original |
| 148 | ) |
| 149 | target = old_string + "\n" if strip_trailing_newline else old_string |
| 150 | if replace_all: |
| 151 | return original.replace(target, "") |
| 152 | idx = original.index(target) |
| 153 | return original[:idx] + original[idx + len(target):] |
| 154 | |
| 155 | |
| 156 | # -- Trailing whitespace stripping --------------------------------------------- |