Skip to content

Fix word wrap misalignment at the end of the document (#948) - #957

Open
Xiao Di (xiaoditx) wants to merge 1 commit into
microsoft:mainfrom
xiaoditx:main
Open

Xiao Di (xiaoditx) wants to merge 1 commit into
microsoft:mainfrom
xiaoditx:main

Conversation

@xiaoditx

Copy link
Copy Markdown

Problem

Fixes #948. With word wrap enabled, the cursor and line-number margin drift on the last line of a document: an extra empty row appears below the text. Moving into a word on the last line can trigger it; repeated Left/Right adds more rows, and debug builds may hit:

assertion failed: cursor.visual_pos.y <= self.stats.visual_lines

Root cause

The word-wrap lookahead in measure_forward() did not stop at the end of the text, so it re-measured the last cluster until it falsely reported a wrap. It also moved words wider than a row as whole words, although hard wrap already handled them. Forward cursor movement could also measure from the cursor's current wrapped position instead of the line start, so the same logical position could map to different visual positions.

Changes

  • measurement.rs: stop lookahead at end of text; only wrap whole words that fit on a row.
    • buffer/mod.rs: re-anchor forward cursor moves to line start when word wrap is enabled; count the extra row render() uses when the cursor sits on the wrap column at end of document; update line stats before validating the cursor in edit_end().
    • Tests: width sweep 2..14, idempotent Left/Right, lookahead edge cases, extra-row bookkeeping.
    • No new dependencies.

Verification

  • cargo test --workspace all green.
    • cargo fmt --all -- --check no diff for changed files.
    • cargo clippy -p edit --all-targets no new warnings.

Notes

I filed this issue a few weeks ago; since it had not been picked up, I fixed it with AI assistance and manually reviewed the changes. My English is not very good, and I left many comments mostly unchanged, so some may be inaccurate or verbose. Please point out anything that should be corrected during review.

@xiaoditx

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

[bug]: Cursor misalignment occurs when displaying inline code in Markdown under word wrap

1 participant