Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always add new line on modify #961

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions patchwork/steps/ModifyCode/ModifyCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ def replace_code_in_file(
new_code: str,
) -> None:
path = Path(file_path)
new_code_lines = new_code.splitlines(keepends=True)
if len(new_code_lines) > 0 and not new_code_lines[-1].endswith("\n"):
new_code_lines[-1] += "\n"

if path.exists() and start_line is not None and end_line is not None:
"""Replaces specified lines in a file with new code."""
text = path.read_text()

lines = text.splitlines(keepends=True)

# Insert the new code at the start line after converting it into a list of lines
lines[start_line:end_line] = handle_indent(lines, new_code.splitlines(keepends=True), start_line, end_line)
lines[start_line:end_line] = handle_indent(lines, new_code_lines, start_line, end_line)
else:
lines = new_code.splitlines(keepends=True)
lines = new_code_lines

# Save the modified contents back to the file
save_file_contents(file_path, "".join(lines))
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/test_ModifyCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_replace_code_in_file(tmp_path):
end_line = 3
new_code = "new line 1\nnew line 2"
replace_code_in_file(str(file_path), start_line, end_line, new_code)
assert file_path.read_text() == "line 1\nnew line 1\nnew line 2"
assert file_path.read_text() == "line 1\nnew line 1\nnew line 2\n"


def test_modify_code_init():
Expand Down
Loading