editor: Fix auto indent adding trailing whitespace on repeated enter#52628
Conversation
When pressing Enter on a line that only contains auto-indent whitespace, that whitespace was preserved as trailing whitespace when a new line was created below it. The fix extends the edit range to the beginning of the line whenever the cursor is at the end of a whitespace-only line, so the whitespace is replaced rather than left behind. Fixes zed-industries#34316 Release Notes: - Fixed auto indent leaving trailing whitespace on blank lines when pressing Enter repeatedly
|
Hi, just a gentle ping on this PR. Please let me know if any changes are needed. Thank you. |
Hey @GoncaloAleixo4 ! Sorry for the late reply, haven't yet had time to reach this Pull Request as I try to review them from oldest to newest and still have quite a few pending ones, as I've been focused on getting a project over the line, sorry 🙂 |
Hi @dinocosta, no worries at all. Thanks for the update. I'll wait for your review, and I'm happy to make any changes if needed. |
* Update the list of cases where, on newline, the edit is extended to the start of the line in order to consider the scenario where, even though the line is not blank, there's only whitespace before the cursor. * Merge all trailing whitespace on newline tests to a single, larger test, to avoid having to setup multiple times, as we can simply update the reset the buffer's text.
dinocosta
left a comment
There was a problem hiding this comment.
Thank you @GoncaloAleixo4 ! 🙂
I've pushed a commit merging the tests together and adding support for one extra scenario where, even though the line isn't blank, if there's only whitespace before the cursor position, we also want to extend the edit. This will match NeoVim's behavior too ✅
Fixes #34316
Problem
Pressing Enter repeatedly on an auto-indented blank line leaves trailing
whitespace on every line left behind.
Reproduction steps:
if __name__ == "__main__":followed by an indented lineifline)The root cause is in
newline()ineditor.rs. The edit that insertsthe new line starts at the cursor position. When the cursor is at the
end of a whitespace-only line (e.g.
|), the edit inserts\nafter the existing whitespace, leaving the original
behind astrailing whitespace.
Fix
editor.rs— when the cursor is at the end of a whitespace-only line,the edit range is extended to the beginning of that line so the
whitespace is replaced rather than kept.
Test Plan
3 unit tests added to
editor_tests.rs:test_newline_clears_trailing_whitespace_on_blank_line— reproduces the reported bug and verifies the fixtest_newline_preserves_whitespace_on_non_blank_line— verifies that pressing Enter mid-line is unaffected by the changetest_newline_clears_trailing_whitespace_with_tabs— verifies the fix works with tab-based indentationSelf-Review Checklist:
Closes #34316
Release Notes: