From f1eca2f14db1ad3b56ae3d0a5f458af5ebae32ee Mon Sep 17 00:00:00 2001 From: Haileyesus Date: Mon, 23 Feb 2026 09:48:15 +0300 Subject: [PATCH] fix(code-editor): clamp diff nav index after chunk recompute `editorToolbarPanel` keeps `currentIndex` as local state while diff chunks are recomputed from `getChunks(view.state)` on each `updatePanel()` call. When the diff shrank (for example after resolving edits), `currentIndex` could remain greater than `chunkCount - 1`. That caused two user-facing issues: - The counter could render impossible values like `4/2 changes`. - Prev/next handlers could read `chunks[currentIndex]` as `undefined`, so navigation would fail to scroll to a valid chunk. Repro in UI: 1. Open a file with multiple diff chunks. 2. Navigate to a high chunk index using Next. 3. Edit content so the number of chunks decreases. 4. Observe stale index in the counter and broken nav behavior. Fix: - After recomputing chunks, clamp the index immediately: `currentIndex = Math.max(0, Math.min(currentIndex, Math.max(0, chunkCount - 1)))` - Keep all downstream uses (counter rendering and prev/next logic) based on the clamped index. Result: - Counter always stays within valid bounds. - Navigation never references an out-of-range chunk. - Zero-chunk behavior remains intact (counter and disabled buttons). File: - src/components/code-editor/utils/editorToolbarPanel.ts --- src/components/code-editor/utils/editorToolbarPanel.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/components/code-editor/utils/editorToolbarPanel.ts b/src/components/code-editor/utils/editorToolbarPanel.ts index b584592..963406d 100644 --- a/src/components/code-editor/utils/editorToolbarPanel.ts +++ b/src/components/code-editor/utils/editorToolbarPanel.ts @@ -65,6 +65,8 @@ export const createEditorToolbarPanelExtension = ({ const chunksData = hasDiff ? getChunks(view.state) : null; const chunks = chunksData?.chunks || []; const chunkCount = chunks.length; + const maxChunkIndex = Math.max(0, chunkCount - 1); + currentIndex = Math.max(0, Math.min(currentIndex, maxChunkIndex)); let toolbarHtml = '
'; toolbarHtml += '
';