Skip to content

[FIX] Calculate DOM for virtualization behaviour - #33

Merged
Avni2000 merged 6 commits into
mainfrom
fix-whitespace-after-matching
Feb 4, 2026
Merged

Avni2000 merged 6 commits into
mainfrom
fix-whitespace-after-matching

Conversation

@Avni2000

@Avni2000 Avni2000 commented Feb 4, 2026 •

Copy link
Copy Markdown
Owner
  • Replaces 200px estimates per cell

Summary by CodeRabbit

  • Bug Fixes

    • Virtual scrolling now measures actual row heights for more accurate rendering and smoother scrolling.
    • Fixed scroll-position glitches when rows are added or removed to prevent blank/offset areas.
    • Improved stability and performance during dynamic content changes and rapid scrolling.
  • Style

    • Adjusted column layout rules for consistent sizing and alignment across three columns.

- Replaces 200px estimates per cell
@coderabbitai

coderabbitai Bot commented Feb 4, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

ConflictResolver now measures actual row heights at runtime using ResizeObserver, replaces the estimated fallback with DEFAULT_ROW_HEIGHT, computes cumulative heights for O(1) lookups, and updates virtualization, scrolling, and rendering to use measured heights (including deletion-safe scroll adjustments).

Changes

Cohort / File(s) Summary
Virtualized ConflictResolver
src/web/client/ConflictResolver.tsx
Replaced estimated-row fallback with DEFAULT_ROW_HEIGHT. Added rowHeights state, rowRefs, ResizeObserver, cumulative heights, and helpers (getRowHeight, getTotalHeight, getRowIndexAtPosition). Rows wrapped in measuring divs with ref callbacks; virtualization/start-end indices now computed from measured heights with overscan. Handles deletions by adjusting scroll and clearing measurements as needed. Minor JSX restructuring and expanded comments.
Styles tweak
src/web/client/styles.ts
Replaced grid-template-columns: 1fr 1fr 1fr with grid-template-columns: repeat(3, minmax(0, 1fr)) in .column-labels and .cell-columns (layout unchanged aside from explicit minmax usage).

Sequence Diagram(s)

sequenceDiagram
    participant User as User (scroll)
    participant Scroll as ScrollContainer
    participant Resolver as ConflictResolver
    participant RO as ResizeObserver
    participant Renderer as DOM Renderer

    rect rgba(135,206,235,0.5)
    User->>Scroll: scrolls viewport
    Scroll->>Resolver: onScroll(position, viewportHeight)
    Resolver->>Resolver: getRowIndexAtPosition(position) / getTotalHeight()
    Resolver->>Renderer: render visible rows (startIndex..endIndex)
    end

    rect rgba(144,238,144,0.5)
    Renderer->>RO: observe row element refs
    RO-->>Resolver: onResize(rowIndex, newHeight)
    Resolver->>Resolver: update rowHeights, recompute cumulative heights
    Resolver->>Scroll: adjust scroll position if needed (on deletion)
    Resolver->>Renderer: re-render visible rows / update container minHeight
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I measured each row with gentle care,
ResizeObserver nibbles heights everywhere,
No more guessing — pixels fall in line,
I hop, scroll steady, every row looks fine,
Carrots for the smooth render time 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title '[FIX] Calculate DOM for virtualization behaviour' accurately describes the main change: replacing static height estimates with dynamic DOM measurement for virtualization, as evidenced by the ResizeObserver introduction and row height tracking implementation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-whitespace-after-matching

Comment @coderabbitai help to get the list of available commands and usage tips.

@Avni2000 Avni2000 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aims to close #28

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@src/web/client/ConflictResolver.tsx`:
- Around line 127-154: The ResizeObserver callback closes over the stale
rowHeights because the effect has an empty dependency array; replace direct
reads of rowHeights with a functional state update via setRowHeights(prev => {
... }) inside the observer so you always base changes on the latest Map, e.g.
create a new Map from prev, apply entry updates, and return the updated map only
when hasChanges is true (otherwise return prev); adjust the callback in the
useEffect that assigns resizeObserver.current and keep disconnect logic
unchanged.
- Around line 187-196: The current cleanup in setRowHeights preserves heights
for indices < currentLength which breaks when a middle row is deleted because
subsequent rows shift; update the logic in the component that calls
setRowHeights (the height-management for the virtual rows) to either clear the
entire rowHeights Map after a deletion to force re-measurement or implement
remapping by detecting deleted indexes and shifting all keys > deletedIndex down
by one; locate the call-sites and the setRowHeights usage in
ConflictResolver.tsx and replace the existing pruning logic with a full clear or
a proper key-shift routine so stored heights remain aligned with row indices.
🧹 Nitpick comments (2)
src/web/client/ConflictResolver.tsx (2)

471-517: Clarify virtualization approach: all rows render, only content is windowed.

The current implementation renders all row wrapper divs in normal flow and relies on MergeRow to skip expensive content when isVisible={false}. This is a valid "content windowing" approach that enables accurate height measurement.

However, for notebooks with hundreds of cells, the DOM node count may still impact performance. If this becomes an issue, consider true virtualization with absolutely-positioned rows and only rendering visible elements. For now, this approach is reasonable given the measurement requirements.


100-125: Consider caching cumulative heights for large notebooks.

getCumulativeHeight and getRowIndexAtPosition both iterate through all rows, resulting in O(n) complexity per call. During scrolling, these are called multiple times. For typical notebook sizes this is fine, but for very large notebooks (hundreds of cells), consider caching a prefix-sum array of cumulative heights, updated when rowHeights changes.

Comment thread src/web/client/ConflictResolver.tsx Resolved
Comment thread src/web/client/ConflictResolver.tsx Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/web/client/ConflictResolver.tsx`:
- Around line 119-142: The function getRowIndexAtPosition can return -1 when
rows.length === 0; add an early guard at the start of getRowIndexAtPosition to
handle an empty rows array (e.g., if rows.length === 0 return 0) and avoid
indexing cumulativeHeights out of bounds; also update the fallback that checks
cumulativeHeights[rows.length] to only run when rows.length > 0 (or otherwise
treat as out-of-bounds and return 0). Ensure you reference
getRowIndexAtPosition, cumulativeHeights, and rows.length when making these
changes.
🧹 Nitpick comments (2)
src/web/client/ConflictResolver.tsx (2)

173-190: Minor: rowRefs.current may accumulate stale entries after row deletions.

When rows are deleted and indices shift, the rowRefs.current Map retains entries for old indices. While this doesn't cause functional issues (the ResizeObserver unobserves elements when they unmount), it could lead to gradual memory accumulation in long editing sessions.

Consider clearing rowRefs.current alongside rowHeights when rows are deleted, though this is a minor concern.

♻️ Optional cleanup for rowRefs
             // Clean up height measurements for deleted rows
             setRowHeights(new Map());
+            // Also clear stale row refs
+            rowRefs.current.clear();
         }

499-557: Consider using stable keys instead of array indices for rows.

Using array indices as keys (key={i} on Line 508) can cause issues when rows are reordered via drag-and-drop:

  1. React may incorrectly reuse component instances, leading to stale internal state
  2. Height measurements in rowHeights may temporarily mismatch as indices shift

While the current implementation works because rowHeights is cleared on deletions and row data is passed as props, a more robust approach would use stable identifiers.

♻️ Suggested approach using stable row identifiers

If rows have unique identifiers (e.g., based on cell indices), use those as keys:

-                    {rows.map((row, i) => {
+                    {rows.map((row, i) => {
+                        // Generate stable key from cell indices
+                        const rowKey = `${row.baseCellIndex ?? 'x'}-${row.currentCellIndex ?? 'x'}-${row.incomingCellIndex ?? 'x'}`;
                         ...
-                        <React.Fragment key={i}>
+                        <React.Fragment key={rowKey}>

Note: This requires ensuring rowKey remains stable and unique across row operations.

Comment thread src/web/client/ConflictResolver.tsx Resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@Avni2000
Avni2000 merged commit e7b4225 into main Feb 4, 2026
@Avni2000 Avni2000 linked an issue Feb 4, 2026 that may be closed by this pull request
@Avni2000 Avni2000 linked an issue Feb 4, 2026 that may be closed by this pull request
@Avni2000
Avni2000 deleted the fix-whitespace-after-matching branch March 1, 2026 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FIX] Whitespace after matching

1 participant