Skip to content

vim: Fix subword motion near end of line#45908

Merged
dinocosta merged 4 commits intozed-industries:mainfrom
lionel-:bugfix/subword-motion
Jan 14, 2026
Merged

vim: Fix subword motion near end of line#45908
dinocosta merged 4 commits intozed-industries:mainfrom
lionel-:bugfix/subword-motion

Conversation

@lionel-
Copy link
Contributor

@lionel- lionel- commented Dec 31, 2025

Closes #17780

The main issue that is fixed here: the boundary detection logic currently excludes newlines due to !at_newline:

let found = !left.is_whitespace() && !at_newline && (is_word_end || is_subword_end);

But removing && !at_newline reveals a secondary issue with the backtracking implemented in the subword motion:

let mut new_point = map.clip_point(new_point, Bias::Left);
if need_backtrack {
    *new_point.column_mut() -= 1;
}

If new_point represents the newline, an invalid position, clipping the point with a left bias causes a one-character backtrack to match a valid position. Then in the next statement we backtrack again. Because of this the cursor no longer moves once it reaches a newline.

Here the fix is to use the same approach as in the regular word motion: find an exclusive boundary instead of an inclusive one in the first place, then we don't need backtracking. The need_next_char logic, also copied from the regular motion implementation, is needed to ensure that motions that don't allow crossing line breaks still make some progress at newlines.

This whole fix is self-contained in the first commit. The second commit is a refactor that makes both motions use the same implementation (with an is_boundary predicate), since they are now so close. This way it'll be easier to keep them in sync.

Release Notes:

  • Fixed subword motion incorrectly jumping to the next line when near the end of a line

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Dec 31, 2025
@dinocosta
Copy link
Member

Holding off on reviewing this until #45341 (review) is resolved, as that might impact these changes 🔍

@dinocosta dinocosta changed the title Fix vim subword motion near end of line vim: Fix subword motion near end of line Jan 14, 2026
@dinocosta dinocosta enabled auto-merge (squash) January 14, 2026 10:48
@dinocosta
Copy link
Member

@lionel- Thank you so much for fixing this and for adding tests for this specific scenario 🙌

I've merged main and updated a very small part of the code in light of the changes introduced by #45341 , shipping this now!

@dinocosta dinocosta merged commit b5242ab into zed-industries:main Jan 14, 2026
25 checks passed
rtfeldman pushed a commit that referenced this pull request Jan 14, 2026
Fixes subword motion incorrectly jumping to next line when near end of
line. Updates boundary detection to use exclusive boundaries with
need_next_char parameter, matching regular word motion behavior.
Refactors word and subword motion to share boundary detection logic.

Closes #17780

Release Notes:

- Fixed subword motion incorrectly jumping to the next line when near
the end of a line

---------

Co-authored-by: dino <dinojoaocosta@gmail.com>
@lionel- lionel- deleted the bugfix/subword-motion branch February 7, 2026 09:56
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subword motion ignores line breaks, jumps to next line unexpectedly

2 participants