fix(forge): rebuild long old sides without argument spread and keep lens focus

Rebuilding the old side spread each run of unchanged lines into push,
which throws a RangeError once a file has around 200,000 unchanged
lines, so such a file failed to draw as soon as it was read ahead. Lines
are now appended one at a time.

A lens segment gained or lost its Tooltip wrapper when its count showed
or hid, which remounted the focused button and dropped the focus. Every
segment now keeps a Tooltip that is disabled without hover text.

gapOf has no caller since gaps answer which lines they still hide, so it
and its test are removed.
This commit is contained in:
LegGasai committed 2026-11-15 12:24:48 +08:00
1 parent 0f421817af
commit 08b9e73017
6 files changed
+29 -30

No files matched your search

@@ -84,7 +84,7 @@ export function SegmentedChoice<Value extends string>({ segments, value, choose,
<div ref={group} role="radiogroup" aria-label={label} className={css.segmented} data-sizing={sizing}>
{segments.map((segment) => {
const button = (
<button key={segment.value} type="button" role="radio" className={css.segment} aria-checked={value === segment.value}
<button type="button" role="radio" className={css.segment} aria-checked={value === segment.value}
disabled={segment.disabledTip !== null} tabIndex={value === segment.value ? 0 : -1} data-segment={segment.value}
onKeyDown={move} onClick={() => { choose(segment.value) }}>
<span className={css.segmentLabel}>{segment.label}</span>
@@ -92,7 +92,9 @@ export function SegmentedChoice<Value extends string>({ segments, value, choose,
</button>
)
const tip = segment.disabledTip ?? (counted || segment.count === null ? null : `${segment.label} ${String(segment.count)}`)
return tip === null ? button : <Tooltip key={segment.value} label={tip} side="bottom">{button}</Tooltip>
// Every segment keeps its Tooltip, disabled without hover text, so showing or hiding the counts keeps the
// button mounted and the focus on it.
return <Tooltip key={segment.value} label={tip ?? segment.label} side="bottom" disabled={tip === null}>{button}</Tooltip>
})}
</div>
)
@@ -37,9 +37,13 @@ function drawn(text: string): string {
*/
export function oldSideLines(lines: readonly string[], hunks: readonly ForgeDiffHunk[]): string[] {
const old: string[] = []
// Lines are appended one at a time: spreading a long run of unchanged lines into push would exceed the argument limit.
const copy = (from: number, to?: number): void => {
for (const line of lines.slice(from - 1, to)) old.push(line)
}
let at = 1
for (const hunk of hunks) {
old.push(...lines.slice(at - 1, hunk.newStart - 1))
copy(at, hunk.newStart - 1)
at = Math.max(at, hunk.newStart)
for (const line of hunk.lines) {
const sign = line.charAt(0)
@@ -47,7 +51,7 @@ export function oldSideLines(lines: readonly string[], hunks: readonly ForgeDiff
if (sign === ' ' || sign === '+') at++
}
}
old.push(...lines.slice(at - 1))
copy(at)
return old
}
@@ -79,18 +79,6 @@ function spotOf(file: ForgeDiffFile, side: 'L' | 'R', line: number): LineSpot {
return { kind: 'gap', index: file.hunks.length }
}
/**
* Find the collapsed unchanged lines that hold a line: the index of the hunk they precede, or the hunk count after the last hunk.
* @param file - the diff file.
* @param side - side the line numbers.
* @param line - the line.
* @returns the gap index, or undefined when a hunk shows the line.
*/
export function gapOf(file: ForgeDiffFile, side: 'L' | 'R', line: number): number | undefined {
const spot = spotOf(file, side, line)
return spot.kind === 'gap' ? spot.index : undefined
}
/**
* Order a file's threads as its section shows them: threads on the whole file, then threads on lines by the row of their
* last line, then outdated threads, then threads the chosen commits do not show. Threads on unchanged lines sort
@@ -39,6 +39,12 @@ describe('oldSideLines', () => {
expect(oldSideLines(['a', 'b'], [deletion])).toEqual(['a', 'gone', 'b'])
expect(oldSideLines(['a', 'b', 'c'], [{ oldStart: 3, oldLines: 0, newStart: 3, newLines: 1, lines: ['+c'] }])).toEqual(['a', 'b'])
})
it('rebuilds a file whose unchanged lines outnumber the arguments one call takes', () => {
const lines = Array.from({ length: 200_000 }, (_, at) => `l${String(at + 1)}`)
const old = oldSideLines(lines, [{ oldStart: 150_000, oldLines: 1, newStart: 150_000, newLines: 1, lines: ['-was', '+l150000'] }])
expect([old.length, old[149_998], old[149_999], old[150_000], old.at(-1)]).toEqual([200_000, 'l149999', 'was', 'l150001', 'l200000'])
})
})
describe('sideRun', () => {
@@ -112,6 +112,18 @@ describe('LensControl width', () => {
expect(counts()).toEqual(Array(4).fill(css.count))
})
it('keeps the focused segment mounted while its count shows or hides', () => {
const { view, counts, resize } = fitted({ cell: 300, counted: 280, labels: 200 })
const focused = view.getByRole('radio', { name: 'Interface1' })
focused.focus()
resize(290)
expect(counts()).toEqual(Array(4).fill(css.hiddenCount))
expect(document.activeElement).toBe(focused)
resize(300)
expect(counts()).toEqual(Array(4).fill(css.count))
expect(document.activeElement).toBe(focused)
})
it('measures again when fonts load, and stops once unmounted', () => {
const { view, need, counts, widths } = fitted({ cell: 300, counted: 280, labels: 200 })
widths.counted = 290
@@ -2,7 +2,7 @@
import { describe, expect, it } from 'vitest'
import type { ForgeReviewThread } from '@deepseek-ai/dsh-experimental-forge/types'
import {
anchorRange, formatThreadQuote, gapOf, inFileOrder, hunkExcerpt, passesFilter, placeThread, threadQuoteSummary, THREAD_FILTERS,
anchorRange, formatThreadQuote, inFileOrder, hunkExcerpt, passesFilter, placeThread, threadQuoteSummary, THREAD_FILTERS,
} from '../src/client/review-threads.ts'
import { diffFile, reviewComment, reviewThread } from './thread-fixtures.client.ts'
@@ -42,19 +42,6 @@ it('names a side and its lines', () => {
expect(anchorRange('L', { startLine: 3, endLine: 5 })).toBe('L3–5')
})
it('finds the collapsed unchanged lines that hold a line on either side', () => {
const file = diffFile('a.ts', [
{ oldStart: 5, oldLines: 3, newStart: 5, newLines: 3, lines: [' a', '-b', '+b', ' c'] },
{ oldStart: 20, oldLines: 2, newStart: 22, newLines: 2, lines: ['-x', '+x', ' y'] },
])
expect(gapOf(file, 'R', 2)).toBe(0)
expect(gapOf(file, 'R', 6)).toBeUndefined()
expect(gapOf(file, 'R', 10)).toBe(1)
expect(gapOf(file, 'L', 21)).toBeUndefined()
expect(gapOf(file, 'R', 21)).toBe(1)
expect(gapOf(file, 'R', 40)).toBe(2)
})
it('orders a file\'s threads as its section shows them', () => {
const file = diffFile('a.ts', [
{ oldStart: 5, oldLines: 3, newStart: 5, newLines: 3, lines: [' a', '-b', '+b', ' c'] },