Skip to content

fix(edit): keep a file's own line endings - #66

Merged
he-yufeng merged 2 commits into
mainfrom
fix/edit-file-line-endings
Nov 18, 2026
Merged

he-yufeng merged 2 commits into
mainfrom
fix/edit-file-line-endings

Conversation

@he-yufeng

Copy link
Copy Markdown
Owner

Supersedes #60. Closes #59.

This carries @goranai's original line-ending fix and its commit history onto current main. The original PR branch had been deleted and the PR was dirty. The implementation is the same; the merge preserves main's undo snapshot recording after the line-ending-preserving write.

edit_file read and wrote with read_text/write_text. Those translate line endings by platform, so one edit rewrote every line ending in the file.

Now it reads with newline="". It matches on LF text, the same text as before, which is also what read_file shows.

  • A one-line edit (no line break in old_string or new_string) is applied to the file as read. Every other byte stays as it was, mixed endings included.
  • An edit that adds or removes line breaks writes the file with its most common line ending (LF, CRLF or CR). A file with no line break at all keeps today's behavior (os.linesep).
  • \r\n in old_string or new_string is read as \n. A CRLF old_string now matches, and a CRLF new_string is no longer written as \r\r\n on Windows.

Behavior changes, all of them:

  • LF files stay LF on Windows. CRLF files stay CRLF on Linux and macOS. CR-only files stay CR.
  • A multi-line edit to a mixed-ending file writes the whole file with its most common ending. Before, it was written with the platform ending.
  • old_string with \r\n used to give "not found". Now it matches the text as LF. The uniqueness check is unchanged.
  • A lone stray CR inside an LF file counts as a line break in a multi-line edit, the same as before.
  • Matching, the "not found" preview and the returned diff use the same LF text as before.
  • README.md, README_CN.md, article/00-index*.md and pyproject.toml: engine 1,528 lines, 3,359 physical, 2,775 net, edit.py 117 lines, 363 offline tests.

Tests:

  • Three new tests in tests/test_tools.py: CRLF kept with LF and CRLF old_string, LF kept with a multi-line new_string, and a one-line edit leaving mixed endings alone. On main on Windows all three fail. With the fix they pass.
  • Full suite on Windows, Python 3.14: main 353 passed, 1 skipped. The original fix(edit): keep a file's own line endings #60 head had 356 passed, 1 skipped.
  • The exact original PR head also passed on macOS arm64 with Python 3.14.2: 356 passed, 1 skipped.
  • After merging current v0.13.0 main, the suite is 363 passed, 1 skipped on macOS arm64 with Python 3.14.2. Ruff check and compileall pass.
  • Two Hypothesis runs of 3000 generated files each (LF, CRLF, CR and mixed, with and without a trailing newline, unicode) compared main with this branch. Every edit main applied, this branch also applies. The text is the same, except where new_string held \r\n and main wrote \r\r\n. A file with one line ending style keeps it. A one-line edit changes no byte outside the replaced text. 58 and 66 edits that main refused now apply. In the second run I checked them: each had \r\n in old_string.
  • The hosted CI matrix has been triggered by merge head ee56c0f92b33c8dcb55aed871ffda8e805790e1a; its result is tracked separately and is not assumed from local runs.

The original #60 validation was performed with Claude Opus 5.5.

goranai and others added 2 commits November 12, 2026 16:46
read_text/write_text translate line endings by platform, so one edit
turned an LF file into CRLF on Windows and a CRLF file into LF on
Linux. Read with newline="", match on LF text as before, and write
the file's own ending back. A one-line edit leaves every other byte
as it was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	README.md
#	README_CN.md
#	article/00-index.md
#	article/00-index_EN.md
#	corecoder/tools/edit.py
#	pyproject.toml
@he-yufeng
he-yufeng merged commit 7440ef5 into main Nov 18, 2026
14 checks passed
@he-yufeng he-yufeng mentioned this pull request Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

edit_file rewrites every line ending of the file it edits (LF to CRLF on Windows, CRLF to LF on Linux/macOS)

2 participants