Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Fix newline deletion problem in tools set_window_text

carlosejimenezbcc7025b33e3
diff --git a/tools/defaults/lib/default_utils.py b/tools/defaults/lib/default_utils.py
index 3172f162..08cb5b48 100644
--- a/tools/defaults/lib/default_utils.py
+++ b/tools/defaults/lib/default_utils.py
@@ -209,7 +209,12 @@ class WindowedFile:
start, stop = line_range
else:
start, stop = self.line_range
- text[start : stop + 1] = new_text.splitlines()
+ # Preserve trailing newline if present
+ ends_with_newline = new_text.endswith('\n')
+ new_lines = new_text.splitlines()
+ if ends_with_newline:
+ new_lines.append('')
+ text[start : stop + 1] = new_lines
self.text = "\n".join(text)
def replace_in_window(
diff --git a/tools/edit_linting/bin/edit b/tools/edit_linting/bin/edit
index 7560bd4a..ed95375d 100644
--- a/tools/edit_linting/bin/edit
+++ b/tools/edit_linting/bin/edit
@@ -81,6 +81,15 @@ def main(line_range: str, replacement_text: Union[str, None] = None):
except EOFError:
break
replacement_text = "\n".join(replacement_lines)
+ if replacement_text == "":
+ # basically delete the lines
+ pass
+ else:
+ replacement_text += "\n" # add implicit newline at the end
+
+ import json
+
+ print(json.dumps(replacement_text))
if not replacement_text:
print(_USAGE_MSG)