Skip to content

editor: Fix auto indent adding trailing whitespace on repeated enter#52628

Merged
dinocosta merged 2 commits intozed-industries:mainfrom
GoncaloAleixo4:fix-auto-indent-trailing-whitespace
Apr 20, 2026
Merged

editor: Fix auto indent adding trailing whitespace on repeated enter#52628
dinocosta merged 2 commits intozed-industries:mainfrom
GoncaloAleixo4:fix-auto-indent-trailing-whitespace

Conversation

@GoncaloAleixo4
Copy link
Copy Markdown
Contributor

@GoncaloAleixo4 GoncaloAleixo4 commented Mar 28, 2026

Fixes #34316

Problem

Pressing Enter repeatedly on an auto-indented blank line leaves trailing
whitespace on every line left behind.

Reproduction steps:

  1. Open a Python file with if __name__ == "__main__": followed by an indented line
  2. Place the cursor at the end of the if line
  3. Press Enter — a new indented line is created correctly
  4. Press Enter again — the previous blank line now has trailing whitespace ( )

The root cause is in newline() in editor.rs. The edit that inserts
the new line starts at the cursor position. When the cursor is at the
end of a whitespace-only line (e.g. |), the edit inserts \n
after the existing whitespace, leaving the original behind as
trailing 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 fix
  • test_newline_preserves_whitespace_on_non_blank_line — verifies that pressing Enter mid-line is unaffected by the change
  • test_newline_clears_trailing_whitespace_with_tabs — verifies the fix works with tab-based indentation

Self-Review Checklist:

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content is consistent with the UI/UX checklist
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Closes #34316

Release Notes:

  • Fixed auto indent leaving trailing whitespace on blank indented lines when creating new lines.⁠

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
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Mar 28, 2026
@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label Mar 28, 2026
@zed-codeowner-coordinator zed-codeowner-coordinator Bot requested review from a team, cole-miller and dinocosta and removed request for a team March 28, 2026 16:06
@GoncaloAleixo4
Copy link
Copy Markdown
Contributor Author

Hi, just a gentle ping on this PR.

Please let me know if any changes are needed. Thank you.

@dinocosta
Copy link
Copy Markdown
Member

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 🙂

@GoncaloAleixo4
Copy link
Copy Markdown
Contributor Author

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.
Copy link
Copy Markdown
Member

@dinocosta dinocosta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ✅

@dinocosta dinocosta merged commit e5086f3 into zed-industries:main Apr 20, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto indent adds trailing whitespace with repeated enter

4 participants