|
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | 5 | from patchwork.step import Step, StepStatus |
| 6 | +from patchwork.common.utils.utils import open_with_chardet |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def save_file_contents(file_path, content): |
9 | | - """Utility function to save content to a file.""" |
10 | | - with open(file_path, "w") as file: |
11 | | - file.write(content) |
| 10 | + """Utility function to save content to a file while preserving line endings. |
| 11 | + |
| 12 | + Args: |
| 13 | + file_path: Path to the file to write to |
| 14 | + content: Content to write to the file |
| 15 | + """ |
| 16 | + # Convert content to bytes preserving the exact bytes of line endings |
| 17 | + content_bytes = content.encode('utf-8') |
| 18 | + with open(file_path, "wb") as file: |
| 19 | + file.write(content_bytes) |
12 | 20 |
|
13 | 21 |
|
14 | 22 | def handle_indent(src: list[str], target: list[str], start: int, end: int) -> list[str]: |
@@ -38,24 +46,60 @@ def replace_code_in_file( |
38 | 46 | end_line: int | None, |
39 | 47 | new_code: str, |
40 | 48 | ) -> None: |
| 49 | + """Replaces specified lines in a file with new code while preserving line endings. |
| 50 | + |
| 51 | + Args: |
| 52 | + file_path: Path to file to modify |
| 53 | + start_line: Starting line number (0-based) to replace |
| 54 | + end_line: Ending line number to replace (exclusive) |
| 55 | + new_code: New code to insert |
| 56 | + |
| 57 | + This function carefully preserves the original line endings (CRLF vs LF) of the file. |
| 58 | + """ |
41 | 59 | path = Path(file_path) |
42 | | - new_code_lines = new_code.splitlines(keepends=True) |
43 | | - if len(new_code_lines) > 0 and not new_code_lines[-1].endswith("\n"): |
44 | | - new_code_lines[-1] += "\n" |
45 | | - |
46 | | - if path.exists() and start_line is not None and end_line is not None: |
47 | | - """Replaces specified lines in a file with new code.""" |
48 | | - text = path.read_text() |
49 | | - |
50 | | - lines = text.splitlines(keepends=True) |
51 | | - |
52 | | - # Insert the new code at the start line after converting it into a list of lines |
53 | | - lines[start_line:end_line] = handle_indent(lines, new_code_lines, start_line, end_line) |
| 60 | + |
| 61 | + # Read existing file in binary mode if it exists to determine endings |
| 62 | + original_ending = b'\n' # default to LF |
| 63 | + original_content = b'' |
| 64 | + file_exists = path.exists() |
| 65 | + |
| 66 | + if file_exists: |
| 67 | + with open(path, 'rb') as f: |
| 68 | + original_content = f.read() |
| 69 | + if b'\r\n' in original_content: |
| 70 | + original_ending = b'\r\n' # CRLF |
| 71 | + |
| 72 | + # Prepare new code with the correct line endings |
| 73 | + new_code_lines = new_code.splitlines(keepends=False) |
| 74 | + if len(new_code_lines) > 0: |
| 75 | + # Encode each line and add detected/default ending |
| 76 | + new_code_lines = [line.encode('utf-8') + original_ending for line in new_code_lines] |
| 77 | + |
| 78 | + if file_exists and start_line is not None and end_line is not None: |
| 79 | + # Split existing content preserving original endings |
| 80 | + lines = original_content.split(original_ending) |
| 81 | + if original_content.endswith(original_ending): |
| 82 | + lines = lines[:-1] # Remove empty line from split if file ends with newline |
| 83 | + lines = [line + original_ending for line in lines] |
| 84 | + |
| 85 | + # Convert new code lines for indentation handling |
| 86 | + new_code_decoded = [line.decode('utf-8') for line in new_code_lines] |
| 87 | + lines_decoded = [line.decode('utf-8') for line in lines] |
| 88 | + |
| 89 | + # Handle indentation |
| 90 | + indented_lines = handle_indent(lines_decoded, new_code_decoded, start_line, end_line) |
| 91 | + |
| 92 | + # Convert back to bytes with correct endings |
| 93 | + new_code_lines = [line.encode('utf-8') for line in indented_lines] |
| 94 | + |
| 95 | + # Replace the lines |
| 96 | + lines[start_line:end_line] = new_code_lines |
54 | 97 | else: |
55 | 98 | lines = new_code_lines |
56 | 99 |
|
57 | | - # Save the modified contents back to the file |
58 | | - save_file_contents(file_path, "".join(lines)) |
| 100 | + # Join and write back in binary mode |
| 101 | + content = b''.join(lines) |
| 102 | + save_file_contents(file_path, content.decode('utf-8')) |
59 | 103 |
|
60 | 104 |
|
61 | 105 | class ModifyCode(Step): |
|
0 commit comments