Skip to content

fix: strip trailing CR in diff content to remove stray "CR" box#2536

Closed
gadfly3173 wants to merge 1 commit into
sourcegit-scm:developfrom
gadfly3173:fix/diff-trailing-cr
Closed

fix: strip trailing CR in diff content to remove stray "CR" box#2536
gadfly3173 wants to merge 1 commit into
sourcegit-scm:developfrom
gadfly3173:fix/diff-trailing-cr

Conversation

@gadfly3173

@gadfly3173 gadfly3173 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Git splits diff lines on \n, so the \r from CRLF/CR files stays glued to the line content in TextDiffLine.Content. After the editor trims the trailing newline, the last line is left with an isolated \r, which AvaloniaEdit renders as a boxed "CR" control character.

Strip it at parse time. RawContent keeps the raw bytes unchanged, so patch generation and the \r\n / \n line-ending markers are unaffected.

Introduced by commit 6a47cec

Original:

image

Git splits diff lines on '\n', so the '\r' from CRLF/CR files stays
glued to the line content in TextDiffLine.Content. After the editor
trims the trailing newline, the last line is left with an isolated
'\r', which AvaloniaEdit renders as a boxed "CR" control character.

Strip it at parse time. RawContent keeps the raw bytes unchanged, so
patch generation and the \r\n/\n line-ending markers are unaffected.
@love-linger

Copy link
Copy Markdown
Collaborator

What's the original git diff command output?

@gadfly3173

Copy link
Copy Markdown
Contributor Author
image
git -C "$REPO" --no-pager show $C | grep '^[-+]' | cat -A

@love-linger

Copy link
Copy Markdown
Collaborator

I mean the original git diff output to show the text diff

@gadfly3173

Copy link
Copy Markdown
Contributor Author

Do you mean this?

image

@love-linger

Copy link
Copy Markdown
Collaborator

See #2517

@gadfly3173

Copy link
Copy Markdown
Contributor Author

I think my PR only removes the end CR of the line, would not affect CR in the middle of the line.

@love-linger

Copy link
Copy Markdown
Collaborator
image

In your screenshot, the line-ending shows <CR>\r\n. It means the line ends with two CR (\r). TextDiffView will not show the CR block marker if there's only one CR at end of file.

@gadfly3173

Copy link
Copy Markdown
Contributor Author

It means the line ends with two CR (\r)

实际上都是\r\n,从前面的diff截图可以看到,每一行结尾都是 ^M$ 。CR标记固定显示在diff的结尾,而不是文件结尾。

@love-linger

Copy link
Copy Markdown
Collaborator

实际上都是\r\n,从前面的diff截图可以看到,每一行结尾都是 ^M$ 。CR标记固定显示在diff的结尾,而不是文件结尾。

我明白了。这个之前改 AvaloniaEdit 时改出的BUG。Diff 结果中由于最后一行没有 \n,所以line-ending的起始位置判断不对了。

love-linger added a commit that referenced this pull request Jul 15, 2026
@love-linger

Copy link
Copy Markdown
Collaborator

我提交了自己的修复方案

@gadfly3173 gadfly3173 closed this Jul 15, 2026
@gadfly3173 gadfly3173 deleted the fix/diff-trailing-cr branch July 16, 2026 01:25
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.

2 participants