Skip to content

Fix vim method/comment navigation with expanded diff hunks#47976

Merged
dinocosta merged 3 commits intozed-industries:mainfrom
lex00:fix/vim-method-nav-expanded-hunks-46612
Feb 2, 2026
Merged

Fix vim method/comment navigation with expanded diff hunks#47976
dinocosta merged 3 commits intozed-industries:mainfrom
lex00:fix/vim-method-nav-expanded-hunks-46612

Conversation

@lex00
Copy link
Contributor

@lex00 lex00 commented Jan 29, 2026

Summary

  • Fixed method_motion and comment_motion coordinate space mismatch
  • When diff hunks are expanded, raw buffer offsets ≠ MultiBuffer offsets
  • Now follows the pattern already used by section_motion

Fixes #46612

Test plan

  • Added test_method_motion_with_expanded_diff_hunks test
  • cargo test -p vim --lib passes (454 tests)
  • ./script/clippy -p vim passes

Release Notes:

  • Fixed vim method and comment navigation (] m, [ m, ] shift-m, [ shift-m, ] /, [ /) incorrectly positioning cursor when diff hunks are expanded

🤖 Generated with Claude Code

The `method_motion` and `comment_motion` functions incorrectly treated
raw buffer offsets as MultiBuffer offsets. When diff hunks are expanded,
the MultiBuffer contains extra content (deleted lines from the git base),
making these coordinate spaces different.

The fix follows the same pattern already used by `section_motion`:
- Use `map.buffer_snapshot().text_object_ranges()` which returns
  `Range<MultiBufferOffset>` with proper coordinate transformation
- Keep offset as `MultiBufferOffset` throughout (don't extract `.0`)
- Use `MultiBufferOffset(0)..offset` for the search range

This ensures vim motions `]m`, `[m`, `]M`, `[M` (method navigation) and
`]*`, `[*` (comment navigation) work correctly regardless of whether
diff hunks are expanded or collapsed.

Fixes zed-industries#46612

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Jan 29, 2026
- Add test coverage for `] /` and `[ /` motions when diff hunks are
  expanded.
- Refactored both `method_motion` and `comment_motion` to avoid multiple
  calls to `DisplaySnapshot.buffer_snapshot`.
Copy link
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 @lex00 ! 🙂

I've also pushed a small commit with a small refactoring and also adding tests for the comment motion, to ensure that we catch any regressions there.

@dinocosta dinocosta enabled auto-merge (squash) February 2, 2026 12:23
@dinocosta dinocosta merged commit a4fe57d into zed-industries:main Feb 2, 2026
27 checks passed
adb-sh pushed a commit to adb-sh/zed that referenced this pull request Feb 5, 2026
…tries#47976)

Previously, `]m`/`[m` (method) and `]/`/`[/` (comment) motions would
navigate to incorrect positions when diff hunks were expanded. This was
caused by extracting raw `usize` values from `MultiBufferOffset` and
operating directly on the underlying buffer, which doesn't account for
expanded diff hunk content.

The fix properly uses `MultiBufferOffset` throughout and queries
`text_object_ranges` on the `MultiBufferSnapshot` instead of the
underlying buffer, ensuring correct coordinate mapping when diff content
is displayed inline.

Fixes zed-industries#46612

Release Notes:

- Fixed vim method and comment navigation (`] m`, `[ m`, `] shift-m`, `[
shift-m`, `] /`, `[ /`) incorrectly positioning cursor when diff hunks
are expanded

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: dino <dinojoaocosta@gmail.com>
@lex00 lex00 deleted the fix/vim-method-nav-expanded-hunks-46612 branch March 1, 2026 20:21
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.

Vim: Next method commands broken with expanded diff hunks

2 participants