Skip to content

Commit e68482d

Browse files
committed
fix: Preserve trailing newlines in insert_block tool
1 parent 031e702 commit e68482d

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

aider/tools/insert_block.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ def execute(
176176
new_lines = lines[:insertion_line_idx] + content_lines + lines[insertion_line_idx:]
177177
new_content = "\n".join(new_lines)
178178

179+
# Restore trailing newline if original file had one
180+
if original_content.endswith("\n"):
181+
new_content += '\n'
182+
179183
if original_content == new_content:
180184
coder.io.tool_warning("No changes made: insertion would not change file")
181185
return "Warning: No changes made (insertion would not change file)"

tests/tools/test_insert_block.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,35 @@ def test_mutually_exclusive_parameters_raise(coder_with_file):
115115
assert result.startswith("Error: Must specify exactly one of")
116116
assert file_path.read_text().startswith("first line")
117117
coder.io.tool_error.assert_called()
118+
119+
120+
def test_trailing_newline_preservation(coder_with_file):
121+
coder, file_path = coder_with_file
122+
insert_block.Tool.execute(
123+
coder,
124+
file_path="example.txt",
125+
content="inserted line",
126+
position="top",
127+
)
128+
129+
content = file_path.read_text()
130+
assert content.endswith("\n"), "File should preserve trailing newline"
131+
coder.io.tool_error.assert_not_called()
132+
133+
134+
def test_no_trailing_newline_preservation(coder_with_file):
135+
coder, file_path = coder_with_file
136+
137+
content_without_trailing_newline = "first line\nsecond line"
138+
file_path.write_text(content_without_trailing_newline)
139+
140+
insert_block.Tool.execute(
141+
coder,
142+
file_path="example.txt",
143+
content="inserted line",
144+
position="top",
145+
)
146+
147+
content = file_path.read_text()
148+
assert not content.endswith("\n"), "File should preserve lack of trailing newline"
149+
coder.io.tool_error.assert_not_called()

0 commit comments

Comments
 (0)