fix: undo/redo produces wrong results when text contains multiple newlines#852
Open
Giggitycountless wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: undo/redo produces wrong results when text contains multiple newlines#852Giggitycountless wants to merge 1 commit intomicrosoft:mainfrom
Giggitycountless wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…lines The undo_redo reinsert loop used `lines_fwd(added, beg, 0, 1)` to find newlines, but lines_fwd is designed to seek to a specific line number. On the second iteration, line >= line_stop causes lines_fwd to return immediately without advancing, resulting in only the first line being reinserted (or an infinite loop in debug builds). Replaced with `memchr2(b'\r', b'\n', ...)` which correctly iterates through all newlines regardless of position. Also properly handles \r\n as a single newline unit to avoid double-inserting line endings in CRLF buffers. Fixes microsoft#834
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Fixes the undo/redo reinsert loop in
TextBuffer::undo_redo()that produces incorrect results when the edited text contains multiple newlines.Root cause
The reinsert loop used
simd::lines_fwd(added, beg, 0, 1)to find newlines in the recorded text. However,lines_fwdis designed to seek to a specific line number — on the second iteration,line >= line_stopcauses it to return immediately without advancing the offset. This means only the first line of a multi-line edit is reinserted during undo/redo.Fix
Replaced
lines_fwdwithsimd::memchr2(b'\r', b'\n', ...)which correctly finds the next newline byte regardless of iteration state. Also properly handles\r\nas a single newline unit to avoid double-inserting line endings in CRLF buffers.Tests added
8 regression tests covering:
Fixes #834