Skip to content

Feature: Virtualizer.scrollToLine() API for programmatic scroll-to-line #452

@davegaeddert

Description

@davegaeddert

Prerequisites

Describe the feature

The Virtualizer class already tracks scrollTop, scrollHeight, and has getOffsetInScrollContainer(element) — it knows its own layout. But there's no API for scrolling to a specific line number programmatically.

Current workaround: Consumers must implement a two-phase scroll:

  1. Compute an approximate scroll position using (lineNumber / totalLines) * scrollHeight
  2. Scroll instantly to that position (triggering the virtualizer to render that area)
  3. Poll/wait for the target [data-line="N"] element to appear in the shadow DOM
  4. Call scrollIntoView() on the found element

This is fragile because the proportion-based approximation doesn't account for injected content (annotations, hunk separators, file headers) that adds uneven height. With many annotations above the target line, the approximation can be off by hundreds of pixels — sometimes enough that the virtualizer doesn't render the target area at all.

Proposed API

// On the Virtualizer class:
scrollToLine(lineNumber: number, options?: { behavior?: ScrollBehavior; block?: ScrollLogicalPosition }): void

Since the Virtualizer manages the scroll container and knows the actual rendered heights of all content, it can compute the exact scroll offset for any line — no approximation needed.

Alternatives considered

  • Proportion-based scroll + polling (current approach): works ~80% of the time, fails when annotation panels add significant height before the target
  • Using getOffsetInScrollContainer(element): requires the element to already exist in the DOM, which it doesn't for virtualized off-screen lines

What version of @pierre/diffs are you using?

1.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions