Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim all trailing whitespace on insert_newline #12177

Merged
merged 2 commits into from
Dec 5, 2024

Conversation

the-mikedavis
Copy link
Member

This is like #4854 but works on any trailing whitespace. The fast-lane for an entire line being whitespace (#4854) is kept as-is. The idea is to work well with #10996: you should be able to hit <ret> twice when starting on a comment line and the middle commented line should not have trailing whitespace.

For example before:

//·Comment⏎
//·⏎
//·|⏎

after:

//·Comment⏎
//⏎
//·|⏎

This happens to fix the first case mentioned in #12165

@the-mikedavis the-mikedavis added C-enhancement Category: Improvements A-command Area: Commands labels Dec 2, 2024
@archseer archseer merged commit fd3e889 into master Dec 5, 2024
6 checks passed
@archseer archseer deleted the ret-trim-trailing-whitespace branch December 5, 2024 11:53
the-mikedavis added a commit that referenced this pull request Jan 15, 2025
#12177 changed `insert_newline`'s behavior to trim any trailing
whitespace on a line which came before a cursor. `insert_newline` would
previously never delete text. Even the whitespace stripping behavior in
#4854 worked by inserting text - a line ending at the beginning of the
line. `global_offs`, a variable that tracks the number of characters
inserted between iterations over the existing selection ranges, was not
updated to also account for text deleted by the trimming behavior,
causing cursors to be offset by the amount of trailing space deleted
and causing panics in some cases.

To fix this we need to subtract the number of trimmed whitespace
characters from `global_offs`. `global_offs` must become an `isize`
(was a `usize`) because it may become negative in cases where a lot of
trailing whitespace is trimmed. Integration tests have been added for
each of these cases.

Fixes #12461
Fixes #12495
Fixes #12539
rmburg pushed a commit to rmburg/helix that referenced this pull request Jan 20, 2025
helix-editor#12177 changed `insert_newline`'s behavior to trim any trailing
whitespace on a line which came before a cursor. `insert_newline` would
previously never delete text. Even the whitespace stripping behavior in
helix-editor#4854 worked by inserting text - a line ending at the beginning of the
line. `global_offs`, a variable that tracks the number of characters
inserted between iterations over the existing selection ranges, was not
updated to also account for text deleted by the trimming behavior,
causing cursors to be offset by the amount of trailing space deleted
and causing panics in some cases.

To fix this we need to subtract the number of trimmed whitespace
characters from `global_offs`. `global_offs` must become an `isize`
(was a `usize`) because it may become negative in cases where a lot of
trailing whitespace is trimmed. Integration tests have been added for
each of these cases.

Fixes helix-editor#12461
Fixes helix-editor#12495
Fixes helix-editor#12539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-command Area: Commands C-enhancement Category: Improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants