Skip to content

[DiffKeymap] Spacebar resets j/k navigation state to first change #345

Description

@pedropaulovc

Description

When using j/k keyboard navigation to move between changes in the diff viewer, pressing the spacebar resets the navigation state back to the first change. The expected behavior is that spacebar should scroll the page (standard browser behavior) without affecting the j/k navigation position.

Steps to Reproduce

  1. Open a PR with multiple changes
  2. Set view mode to: inline view, full file mode, hidden comments
  3. Use 'j' key to navigate to the 2nd or 3rd change
  4. Press spacebar
  5. Observe: navigation state resets to first change (bug)
  6. Expected: page should scroll down, j/k position should remain unchanged

Expected Behavior

Pressing spacebar should:

  • Scroll the page down by approximately one viewport height (standard browser behavior)
  • Preserve the current j/k navigation index so that subsequent j/k presses continue from the same position

Actual Behavior

Pressing spacebar:

  • Resets the currentHunkIndex state to -1 or 0, causing navigation to jump back to the first change
  • Next use of 'j' key navigates from the first change instead of continuing from the previous position

Root Cause Analysis

The diff-keymap.ts extension defines key bindings for j, k, PageUp, PageDown, Home, and End keys, but does not define a key binding for spacebar. The spacebar may be triggering a default CodeMirror behavior or being handled elsewhere, which could be resetting the diffNavigationState.currentHunkIndex via state mutations or StateEffect.

Affected file: src/features/diff/components/codemirror/extensions/diff-keymap.ts

The diffNavigationState StateField manages currentHunkIndex for navigation:

  • Lines 40-55: State definition
  • Lines 85-102: 'j' key binding (increments currentHunkIndex)
  • Lines 104-121: 'k' key binding (decrements currentHunkIndex)
  • Lines 123-170: Page scroll and navigation key bindings

Missing: Spacebar key binding that allows default scroll behavior without affecting state

Additional Context

  • This happens with the setup: inline view, full file mode, hidden comments
  • The issue specifically occurs when spacebar is pressed after j/k navigation
  • The spacebar should behave as default browser scroll behavior (or fall through to CodeMirror's default)
  • The j/k navigation index should be preserved when spacebar is pressed

Test Scenario

A reproducible test case would:

  1. Create a diff with 5+ changes
  2. Navigate to change 3 using 'j' key twice
  3. Press spacebar
  4. Verify that currentHunkIndex remains at 2 (third change, 0-indexed)
  5. Press 'j' again and verify it navigates to change 4 (not back to change 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions