Skip to content

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

TypeScript

mods/diff: hunks draw through the engine's Code element, split at hunk boundaries under its 10000-character source cap; the row renderer and word diff go

poteat30a685085957
diff --git a/mods/diff/hooks/index.ts b/mods/diff/hooks/index.ts
index 9094b627..42177844 100644
--- a/mods/diff/hooks/index.ts
+++ b/mods/diff/hooks/index.ts
@@ -21,6 +21,5 @@ export * from './todos'
export * from './tools'
export * from './turns'
export * from './views'
-export * from './word-diff'
export * as default from '.'
diff --git a/mods/diff/hooks/register.ts b/mods/diff/hooks/register.ts
index 54a98b77..0a7a6d7e 100644
--- a/mods/diff/hooks/register.ts
+++ b/mods/diff/hooks/register.ts
@@ -457,14 +457,14 @@ export function register(on: On) {
return next(e)
}
- const { Box, Text, Button, Select } = await $.ui.resolve(e)
+ const { Box, Text, Button, Select, Code } = await $.ui.resolve(e)
wasDrawnSinceProbe = true
columns = e.viewport?.columns ?? columns
model = { ...model, isFocused: e.props.isFocused }
return Views.paneView(
{
- ui: { Box, Text, Button, Select },
+ ui: { Box, Text, Button, Select, Code },
actions: actionsOf(host),
columns: e.props.bodyColumns,
},
diff --git a/mods/diff/hooks/views/detail/backgrounds/backgrounds.ts b/mods/diff/hooks/views/detail/backgrounds/backgrounds.ts
deleted file mode 100644
index f0ec3991..00000000
--- a/mods/diff/hooks/views/detail/backgrounds/backgrounds.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import type Types from '../types'
-
-/**
- * The theme key behind a run of each kind: the added and removed bands, and
- * none behind context, which draws on the terminal's own background.
- *
- * The theme's `background` key is the background-task accent, a cyan.
- */
-export const BACKGROUNDS: Readonly<Record<Types.LineKind, string | undefined>> =
- {
- added: 'diffAdded',
- removed: 'diffRemoved',
- context: undefined,
- }
diff --git a/mods/diff/hooks/views/detail/backgrounds/index.ts b/mods/diff/hooks/views/detail/backgrounds/index.ts
deleted file mode 100644
index cd6017e9..00000000
--- a/mods/diff/hooks/views/detail/backgrounds/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './backgrounds.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/code-blocks-of/code-blocks-of.ts b/mods/diff/hooks/views/detail/code-blocks-of/code-blocks-of.ts
new file mode 100644
index 00000000..9fc0cc18
--- /dev/null
+++ b/mods/diff/hooks/views/detail/code-blocks-of/code-blocks-of.ts
@@ -0,0 +1,76 @@
+import Git from '../../../git'
+import { HUNK_DIVIDER } from '../hunk-divider'
+import { HUNK_DIVIDER_NODES } from '../hunk-divider-nodes'
+import { hunkSourceOf } from '../hunk-source-of'
+import { leadingHunkOf } from '../leading-hunk-of'
+import { MAX_BODY_CHARS } from '../max-body-chars'
+import { MAX_BODY_NODES } from '../max-body-nodes'
+import { MAX_CODE_CHARS } from '../max-code-chars'
+import { subHunksOf } from '../sub-hunks-of'
+import type Types from '../types'
+import { drawnLineOf } from './drawn-line-of'
+
+/**
+ * A file's hunks as the sources of `Code` blocks, each at most
+ * MAX_CODE_CHARS, hunks sharing a block (the engine's ellipsis between).
+ *
+ * A new block at a hunk gets HUNK_DIVIDER; a hunk too long for one is split
+ * (subHunksOf) into blocks stacked with none. The body stops, truncated, at
+ * the whole lines MAX_BODY_CHARS and MAX_BODY_NODES hold, as the host counts.
+ *
+ * @param hunks the file's hunks
+ * @returns the blocks in order, and whether any line was left out or cut
+ */
+export function codeBlocksOf(hunks: readonly Git.Hunk[]): Types.CodeBody {
+ const blocks: Types.CodeBlock[] = []
+ let chars = 0
+ let nodes = 0
+ let isTruncated = false
+
+ for (const hunk of hunks) {
+ const lines = hunk.lines.filter(Git.isBodyLine).map(drawnLineOf)
+ const split = subHunksOf({ ...hunk, lines }, MAX_CODE_CHARS)
+ isTruncated ||= split.isTruncated
+
+ for (const [at, piece] of split.hunks.entries()) {
+ const last = blocks.at(-1)
+ const source = hunkSourceOf(piece)
+ const isHunkStart = at === 0
+ const isJoining =
+ isHunkStart &&
+ last !== undefined &&
+ last.source.length + 1 + source.length <= MAX_CODE_CHARS
+ const hasDivider = isHunkStart && !isJoining && last !== undefined
+ const overhead = isJoining ? 1 : hasDivider ? HUNK_DIVIDER.length : 0
+ const addedNodes = isJoining
+ ? 0
+ : 1 + (hasDivider ? HUNK_DIVIDER_NODES : 0)
+ const room = MAX_BODY_CHARS - chars - overhead
+ const fitted = source.length <= room ? piece : leadingHunkOf(piece, room)
+ const isOverNodes = nodes + addedNodes > MAX_BODY_NODES
+
+ if (isOverNodes || fitted.lines.length === 0) {
+ return { blocks, isTruncated: true }
+ }
+
+ const text = hunkSourceOf(fitted)
+
+ if (isJoining) {
+ blocks.pop()
+ }
+
+ blocks.push({
+ source: isJoining ? `${last.source}\n${text}` : text,
+ hasDivider: isJoining ? last.hasDivider : hasDivider,
+ })
+ chars += overhead + text.length
+ nodes += addedNodes
+
+ if (fitted.lines.length < piece.lines.length) {
+ return { blocks, isTruncated: true }
+ }
+ }
+ }
+
+ return { blocks, isTruncated }
+}
diff --git a/mods/diff/hooks/views/detail/code-blocks-of/drawn-line-of/drawn-line-of.ts b/mods/diff/hooks/views/detail/code-blocks-of/drawn-line-of/drawn-line-of.ts
new file mode 100644
index 00000000..b5807d53
--- /dev/null
+++ b/mods/diff/hooks/views/detail/code-blocks-of/drawn-line-of/drawn-line-of.ts
@@ -0,0 +1,15 @@
+import Layout from '../../../layout'
+
+/**
+ * A hunk line made safe for a `Code` source: its marker and its tabs kept,
+ * the engine laying tabs out as it does every diff it draws.
+ *
+ * Between the tabs sanitizeLine's rule holds: every control, format and
+ * invisible character dropped, so nothing can reorder or hide the line.
+ *
+ * @param line a body line, its `+`, `-` or space marker first
+ * @returns the drawable line
+ */
+export const drawnLineOf = (line: string) =>
+ line.slice(0, 1) +
+ line.slice(1).split('\t').map(Layout.sanitizeLine).join('\t')
diff --git a/mods/diff/hooks/views/detail/code-blocks-of/drawn-line-of/index.ts b/mods/diff/hooks/views/detail/code-blocks-of/drawn-line-of/index.ts
new file mode 100644
index 00000000..fad69179
--- /dev/null
+++ b/mods/diff/hooks/views/detail/code-blocks-of/drawn-line-of/index.ts
@@ -0,0 +1,3 @@
+export * from './drawn-line-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/code-blocks-of/index.ts b/mods/diff/hooks/views/detail/code-blocks-of/index.ts
new file mode 100644
index 00000000..f279db56
--- /dev/null
+++ b/mods/diff/hooks/views/detail/code-blocks-of/index.ts
@@ -0,0 +1,4 @@
+export * from './code-blocks-of.js'
+export * from './drawn-line-of'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/detail-view.tsx b/mods/diff/hooks/views/detail/detail-view.tsx
index 45e96373..8c61f5e7 100644
--- a/mods/diff/hooks/views/detail/detail-view.tsx
+++ b/mods/diff/hooks/views/detail/detail-view.tsx
@@ -5,22 +5,19 @@ import type { RenderElement } from 'claude-code'
import type { Kit } from '../kit'
import Layout from '../layout'
+import { codeBlocksOf } from './code-blocks-of'
import type { DetailModel } from './detail-model'
-import { gutterCellsOf } from './gutter-cells-of'
-import { hunkBody } from './hunk-body'
-import { linesOf } from './lines-of'
-import { MARKER_CELLS } from './marker-cells'
-import { maxRowsOf } from './max-rows-of'
+import { HUNK_DIVIDER } from './hunk-divider'
+import { MAX_CODE_CHARS } from './max-code-chars'
import { placeholderOf } from './placeholder-of'
-import { rowsOf } from './rows-of'
/**
- * The selected file under the list: its bold path and asides (renamed
- * from, untracked, truncated) cut to the width, the ask Button, the body.
+ * The selected file under the list: its bold path and asides, each name
+ * cut from its start to the width, the ask Button, the body.
*
- * The body is a placeholder or the hunks (cut to the rows the body's char
- * and node budgets hold, maxRowsOf), then the footer; the gutter fits the
- * largest number.
+ * The body is a placeholder, or the hunks as the engine's diff `Code` blocks
+ * (codeBlocksOf) named by the path, a dim divider where two meet at a hunk;
+ * then the footer when anything was cut.
*
* @param kit the elements and the width
* @param detail the selected file
@@ -32,18 +29,13 @@ export function detailView(
detail: DetailModel,
onToggleAsk: () => void,
): RenderElement {
- const { Box, Text, Button } = kit.ui
+ const { Box, Text, Button, Code } = kit.ui
const placeholder = placeholderOf(detail)
- const hunks = placeholder ? [] : (detail.body?.hunks ?? [])
- const lines = linesOf(hunks)
- const gutterCells = gutterCellsOf(lines)
- const contentCells = Math.max(1, kit.columns - gutterCells - MARKER_CELLS)
- const rows = placeholder ? [] : rowsOf(lines, contentCells)
- const cells = { gutterCells, contentCells }
- const maxRows = maxRowsOf(rows, cells)
- const isTruncated = detail.body?.isTruncated === true || rows.length > maxRows
- const nameOf = (path: string) =>
- Layout.truncateStart(Layout.sanitizeName(path), kit.columns)
+ const code = codeBlocksOf(placeholder ? [] : (detail.body?.hunks ?? []))
+ const isTruncated = detail.body?.isTruncated === true || code.isTruncated
+ const path = Layout.sanitizeName(detail.path).slice(-MAX_CODE_CHARS)
+ const nameOf = (name: string) =>
+ Layout.truncateStart(Layout.sanitizeName(name), kit.columns)
const renamedFrom =
detail.renamedFrom === null ? null : nameOf(detail.renamedFrom)
const asides = [
@@ -52,14 +44,6 @@ export function detailView(
isTruncated ? 'truncated' : null,
].filter(word => word !== null)
const aside = asides.length === 0 ? '' : ` (${asides.join(', ')})`
- const notes = (placeholder ?? []).map(line => (
- <Text dimColor italic wrap="wrap">
- {line}
- </Text>
- ))
- const body = placeholder
- ? notes
- : [hunkBody(kit.ui, rows.slice(0, maxRows), cells)]
const ask = placeholder
? []
: [
@@ -74,19 +58,32 @@ export function detailView(
</Text>,
]
: []
+ const notes = (placeholder ?? []).map(line => (
+ <Text dimColor italic wrap="wrap">
+ {line}
+ </Text>
+ ))
+ const hunks = code.blocks.flatMap(block => [
+ ...(block.hasDivider ? [<Text dimColor>{HUNK_DIVIDER}</Text>] : []),
+ <Code source={block.source} format="diff" path={path} />,
+ ])
- const header = (
- <Box flexDirection="row">
+ return (
+ <Box flexDirection="column">
{[
- <Text bold wrap="truncate-start">
- {nameOf(detail.path)}
- </Text>,
- <Text dimColor>{aside}</Text>,
- <Box flexGrow={1} />,
- ...ask,
+ <Box flexDirection="row">
+ {[
+ <Text bold wrap="truncate-start">
+ {nameOf(detail.path)}
+ </Text>,
+ <Text dimColor>{aside}</Text>,
+ <Box flexGrow={1} />,
+ ...ask,
+ ]}
+ </Box>,
+ ...(placeholder ? notes : hunks),
+ ...footer,
]}
</Box>
)
-
- return <Box flexDirection="column">{[header, ...body, ...footer]}</Box>
}
diff --git a/mods/diff/hooks/views/detail/gutter-cells-of/gutter-cells-of.ts b/mods/diff/hooks/views/detail/gutter-cells-of/gutter-cells-of.ts
deleted file mode 100644
index 098be16a..00000000
--- a/mods/diff/hooks/views/detail/gutter-cells-of/gutter-cells-of.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import type Types from '../types'
-
-/**
- * How wide the line-number gutter is for these lines: the longest number's
- * digits and one leading cell (StructuredDiff's digits + 1).
- *
- * @param lines the numbered lines
- * @returns the gutter's width in cells
- */
-export const gutterCellsOf = (lines: readonly Types.DiffLine[]) =>
- String(lines.reduce((most, line) => Math.max(most, line.lineNumber), 0))
- .length + 1
diff --git a/mods/diff/hooks/views/detail/gutter-cells-of/index.ts b/mods/diff/hooks/views/detail/gutter-cells-of/index.ts
deleted file mode 100644
index 4730ab40..00000000
--- a/mods/diff/hooks/views/detail/gutter-cells-of/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './gutter-cells-of.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/hunk-body/body-cells/body-cells.ts b/mods/diff/hooks/views/detail/hunk-body/body-cells/body-cells.ts
deleted file mode 100644
index 423b840a..00000000
--- a/mods/diff/hooks/views/detail/hunk-body/body-cells/body-cells.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-/**
- * The two widths a body is drawn in: the gutter's and the content's, the
- * marker cells between them.
- */
-export type BodyCells = {
- gutterCells: number
- contentCells: number
-}
diff --git a/mods/diff/hooks/views/detail/hunk-body/body-cells/index.ts b/mods/diff/hooks/views/detail/hunk-body/body-cells/index.ts
deleted file mode 100644
index 21daa6d4..00000000
--- a/mods/diff/hooks/views/detail/hunk-body/body-cells/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export type * from './body-cells.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/hunk-body/hunk-body.tsx b/mods/diff/hooks/views/detail/hunk-body/hunk-body.tsx
deleted file mode 100644
index ffe2f9a8..00000000
--- a/mods/diff/hooks/views/detail/hunk-body/hunk-body.tsx
+++ /dev/null
@@ -1,114 +0,0 @@
-/* @jsxRuntime classic */
-/* @jsx h */
-/* @jsxFrag Fragment */
-import type { RenderElement, RenderNode } from 'claude-code'
-
-import type Kit from '../../kit'
-import Layout from '../../layout'
-import { BACKGROUNDS } from '../backgrounds'
-import { MARKER_CELLS } from '../marker-cells'
-import { MAX_RUN_CHARS } from '../max-run-chars'
-import { runsOf } from '../runs-of'
-import type Types from '../types'
-import { WORD_BACKGROUNDS } from '../word-backgrounds'
-import type { BodyCells } from './body-cells'
-
-/**
- * A file's rows drawn as StructuredDiff's fallback draws them, in few
- * nodes: one dim gutter Text beside a column of one Text per run.
- *
- * Added and removed runs sit on their theme bands, changed words on the
- * word bands, every row is padded to the band's width less its text's
- * chargedWidth; a run's text and the gutter split at MAX_RUN_CHARS a string.
- *
- * @param ui the surface's elements
- * @param rows the laid-out rows (rowsOf), each exactly one terminal row
- * @param cells the gutter's and the content's widths
- * @returns the body element
- */
-export function hunkBody(
- ui: Kit.Ui,
- rows: readonly Types.Row[],
- cells: BodyCells,
-): RenderElement {
- const { Box, Text } = ui
- const bandCells = MARKER_CELLS + cells.contentCells
-
- function gutterChildren(): string[] {
- const children: string[] = []
- let pending = ''
-
- rows.forEach((row, at) => {
- const label = row.gutter.padStart(cells.gutterCells)
- const text = `${at === 0 ? '' : '\n'}${label}`
-
- if (pending.length + text.length > MAX_RUN_CHARS) {
- children.push(pending)
- pending = ''
- }
-
- pending += text
- })
-
- return pending === '' ? children : [...children, pending]
- }
-
- function runChildren(run: Types.Run): RenderNode[] {
- const children: RenderNode[] = []
- let pending = ''
-
- function flush() {
- if (pending !== '') {
- children.push(pending)
- pending = ''
- }
- }
-
- function plain(text: string) {
- if (pending.length + text.length > MAX_RUN_CHARS) {
- flush()
- }
-
- pending += text
- }
-
- function marked(text: string) {
- flush()
- children.push(
- <Text backgroundColor={WORD_BACKGROUNDS[run.kind]}>{text}</Text>,
- )
- }
-
- run.rows.forEach((row, at) => {
- const used = row.segments.reduce(
- (sum, segment) => sum + Layout.chargedWidth(segment.text),
- MARKER_CELLS,
- )
- plain(`${at === 0 ? '' : '\n'} ${row.marker} `)
-
- for (const segment of row.segments) {
- ;(segment.isChanged ? marked : plain)(segment.text)
- }
-
- plain(' '.repeat(Math.max(0, bandCells - used)))
- })
- flush()
-
- return children
- }
-
- return (
- <Box flexDirection="row">
- <Text dimColor wrap="truncate-end">
- {gutterChildren()}
- </Text>
- <Box flexDirection="column" flexShrink={0} width={bandCells}>
- {runsOf(rows).map(run => (
- <Text backgroundColor={BACKGROUNDS[run.kind]} wrap="truncate-end">
- {runChildren(run)}
- </Text>
- ))}
- </Box>
- </Box>
- )
-}
diff --git a/mods/diff/hooks/views/detail/hunk-body/index.ts b/mods/diff/hooks/views/detail/hunk-body/index.ts
deleted file mode 100644
index 937d5ed1..00000000
--- a/mods/diff/hooks/views/detail/hunk-body/index.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-export * from './body-cells'
-export * from './hunk-body.jsx'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/hunk-divider-nodes/hunk-divider-nodes.ts b/mods/diff/hooks/views/detail/hunk-divider-nodes/hunk-divider-nodes.ts
new file mode 100644
index 00000000..b116ae29
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-divider-nodes/hunk-divider-nodes.ts
@@ -0,0 +1,4 @@
+/**
+ * The nodes one HUNK_DIVIDER costs the tree: its Text and its string.
+ */
+export const HUNK_DIVIDER_NODES = 2
diff --git a/mods/diff/hooks/views/detail/hunk-divider-nodes/index.ts b/mods/diff/hooks/views/detail/hunk-divider-nodes/index.ts
new file mode 100644
index 00000000..e038d0df
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-divider-nodes/index.ts
@@ -0,0 +1,3 @@
+export * from './hunk-divider-nodes.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/hunk-divider/hunk-divider.ts b/mods/diff/hooks/views/detail/hunk-divider/hunk-divider.ts
new file mode 100644
index 00000000..3400a544
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-divider/hunk-divider.ts
@@ -0,0 +1,5 @@
+/**
+ * The dim text between two `Code` blocks that meet at a hunk boundary: the
+ * ellipsis the engine draws between the hunks of one `Code`.
+ */
+export const HUNK_DIVIDER = '...'
diff --git a/mods/diff/hooks/views/detail/hunk-divider/index.ts b/mods/diff/hooks/views/detail/hunk-divider/index.ts
new file mode 100644
index 00000000..69c6a933
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-divider/index.ts
@@ -0,0 +1,3 @@
+export * from './hunk-divider.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/hunk-header-of/hunk-header-of.ts b/mods/diff/hooks/views/detail/hunk-header-of/hunk-header-of.ts
new file mode 100644
index 00000000..9ab3e772
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-header-of/hunk-header-of.ts
@@ -0,0 +1,23 @@
+import { countOf } from '../../../count-of'
+import type Git from '../../../git'
+
+/**
+ * A hunk's `@@ -a,b +c,d @@` header, its counts read off its lines, so a
+ * cut or split hunk still parses: context counts on both sides.
+ *
+ * A side with no lines names the line before its start, as jsdiff's
+ * formatPatch writes a structured hunk and the engine's parser reads back.
+ *
+ * @param hunk the hunk, its lines body lines only
+ * @returns the header line
+ */
+export function hunkHeaderOf(hunk: Git.Hunk) {
+ const oldLines = countOf(hunk.lines, line => !line.startsWith('+'))
+ const newLines = countOf(hunk.lines, line => !line.startsWith('-'))
+ const isOldEmpty = oldLines === 0
+ const isNewEmpty = newLines === 0
+ const oldStart = isOldEmpty ? Math.max(0, hunk.oldStart - 1) : hunk.oldStart
+ const newStart = isNewEmpty ? Math.max(0, hunk.newStart - 1) : hunk.newStart
+
+ return `@@ -${oldStart},${oldLines} +${newStart},${newLines} @@`
+}
diff --git a/mods/diff/hooks/views/detail/hunk-header-of/index.ts b/mods/diff/hooks/views/detail/hunk-header-of/index.ts
new file mode 100644
index 00000000..5eb801a9
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-header-of/index.ts
@@ -0,0 +1,3 @@
+export * from './hunk-header-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/hunk-source-of/hunk-source-of.ts b/mods/diff/hooks/views/detail/hunk-source-of/hunk-source-of.ts
new file mode 100644
index 00000000..d837ce7b
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-source-of/hunk-source-of.ts
@@ -0,0 +1,11 @@
+import type Git from '../../../git'
+import { hunkHeaderOf } from '../hunk-header-of'
+
+/**
+ * A hunk as unified-diff text: its header (hunkHeaderOf), then its lines.
+ *
+ * @param hunk the hunk, its lines body lines only
+ * @returns the text, no trailing newline
+ */
+export const hunkSourceOf = (hunk: Git.Hunk) =>
+ [hunkHeaderOf(hunk), ...hunk.lines].join('\n')
diff --git a/mods/diff/hooks/views/detail/hunk-source-of/index.ts b/mods/diff/hooks/views/detail/hunk-source-of/index.ts
new file mode 100644
index 00000000..87be8572
--- /dev/null
+++ b/mods/diff/hooks/views/detail/hunk-source-of/index.ts
@@ -0,0 +1,3 @@
+export * from './hunk-source-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/index.ts b/mods/diff/hooks/views/detail/index.ts
index 9283fae6..0f3f5b48 100644
--- a/mods/diff/hooks/views/detail/index.ts
+++ b/mods/diff/hooks/views/detail/index.ts
@@ -1,18 +1,16 @@
-export * from './backgrounds'
+export * from './code-blocks-of'
export * from './detail-model'
export * from './detail-view.jsx'
-export * from './gutter-cells-of'
-export * from './hunk-body'
-export * from './lines-of'
-export * from './marker-cells'
+export * from './hunk-divider'
+export * from './hunk-divider-nodes'
+export * from './hunk-header-of'
+export * from './hunk-source-of'
+export * from './leading-hunk-of'
export * from './max-body-chars'
export * from './max-body-nodes'
-export * from './max-rows-of'
-export * from './max-run-chars'
+export * from './max-code-chars'
export * from './placeholder-of'
-export * from './rows-of'
-export * from './runs-of'
+export * from './sub-hunks-of'
export * from './types'
-export * from './word-backgrounds'
export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/leading-hunk-of/index.ts b/mods/diff/hooks/views/detail/leading-hunk-of/index.ts
new file mode 100644
index 00000000..efc09497
--- /dev/null
+++ b/mods/diff/hooks/views/detail/leading-hunk-of/index.ts
@@ -0,0 +1,3 @@
+export * from './leading-hunk-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/leading-hunk-of/leading-hunk-of.ts b/mods/diff/hooks/views/detail/leading-hunk-of/leading-hunk-of.ts
new file mode 100644
index 00000000..0672a071
--- /dev/null
+++ b/mods/diff/hooks/views/detail/leading-hunk-of/leading-hunk-of.ts
@@ -0,0 +1,31 @@
+import type Git from '../../../git'
+import { hunkHeaderOf } from '../hunk-header-of'
+
+/**
+ * The most of a hunk's first lines whose text (hunkSourceOf) fits the
+ * room, whole lines only: none when the first line alone does not fit.
+ *
+ * @param hunk the hunk, its lines body lines only
+ * @param maxChars the room, in UTF-16 units
+ * @returns the hunk cut to those lines, its starts unchanged
+ */
+export function leadingHunkOf(hunk: Git.Hunk, maxChars: number): Git.Hunk {
+ let bodyChars = 0
+ let taken = 0
+
+ for (const line of hunk.lines) {
+ const header = hunkHeaderOf({
+ ...hunk,
+ lines: hunk.lines.slice(0, taken + 1),
+ })
+ bodyChars += 1 + line.length
+
+ if (header.length + bodyChars > maxChars) {
+ break
+ }
+
+ taken += 1
+ }
+
+ return { ...hunk, lines: hunk.lines.slice(0, taken) }
+}
diff --git a/mods/diff/hooks/views/detail/lines-of/index.ts b/mods/diff/hooks/views/detail/lines-of/index.ts
deleted file mode 100644
index 8a09a14e..00000000
--- a/mods/diff/hooks/views/detail/lines-of/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './lines-of.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/lines-of/lines-of.ts b/mods/diff/hooks/views/detail/lines-of/lines-of.ts
deleted file mode 100644
index 445a6393..00000000
--- a/mods/diff/hooks/views/detail/lines-of/lines-of.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import type Git from '../../../git'
-import Layout from '../../layout'
-import type Types from '../types'
-
-/**
- * A file's hunks as numbered lines, hunk after hunk: context and added
- * lines count the new file's lines, removed ones the old file's.
- *
- * Each run of removed lines is paired index by index with the added run
- * right after it, for the word diff, as the engine's fallback diff pairs.
- *
- * @param hunks the parsed hunks
- * @returns the lines in drawing order
- */
-export function linesOf(hunks: readonly Git.Hunk[]): Types.DiffLine[] {
- const lines: Types.DiffLine[] = []
-
- for (const hunk of hunks) {
- let oldNumber = hunk.oldStart
- let newNumber = hunk.newStart
- let removedRun: Types.DiffLine[] = []
- let pairing = 0
-
- for (const raw of hunk.lines) {
- const text = Layout.sanitizeLine(raw.slice(1))
- const kind: Types.LineKind = raw.startsWith('+')
- ? 'added'
- : raw.startsWith('-')
- ? 'removed'
- : 'context'
-
- if (kind === 'removed') {
- const isNewRun = pairing > 0
-
- if (isNewRun) {
- removedRun = []
- pairing = 0
- }
-
- const line = { kind, lineNumber: oldNumber, text, pairedText: null }
- removedRun.push(line)
- lines.push(line)
- oldNumber += 1
- continue
- }
-
- if (kind === 'added') {
- const partner = removedRun[pairing]
-
- if (partner) {
- partner.pairedText = text
- pairing += 1
- }
-
- lines.push({
- kind,
- lineNumber: newNumber,
- text,
- pairedText: partner ? partner.text : null,
- })
- newNumber += 1
- continue
- }
-
- removedRun = []
- pairing = 0
- lines.push({ kind, lineNumber: newNumber, text, pairedText: null })
- oldNumber += 1
- newNumber += 1
- }
- }
-
- return lines
-}
diff --git a/mods/diff/hooks/views/detail/marker-cells/index.ts b/mods/diff/hooks/views/detail/marker-cells/index.ts
deleted file mode 100644
index 18692c83..00000000
--- a/mods/diff/hooks/views/detail/marker-cells/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './marker-cells.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/marker-cells/marker-cells.ts b/mods/diff/hooks/views/detail/marker-cells/marker-cells.ts
deleted file mode 100644
index bc096888..00000000
--- a/mods/diff/hooks/views/detail/marker-cells/marker-cells.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * The cells between the gutter and a row's content: a space, the `+`, `-`
- * or space marker, and a space.
- */
-export const MARKER_CELLS = 3
diff --git a/mods/diff/hooks/views/detail/max-body-chars/max-body-chars.ts b/mods/diff/hooks/views/detail/max-body-chars/max-body-chars.ts
index e2e294c8..77296c3b 100644
--- a/mods/diff/hooks/views/detail/max-body-chars/max-body-chars.ts
+++ b/mods/diff/hooks/views/detail/max-body-chars/max-body-chars.ts
@@ -1,5 +1,7 @@
/**
- * The most characters a file's drawn body holds in all, padding included:
- * under the host's whole-tree cap (100 000) with room for the pane's rest.
+ * The most characters a file's drawn body holds, its `Code` sources and
+ * dividers in all.
+ *
+ * Under the host's whole-tree cap (100 000), with room for the pane's rest.
*/
export const MAX_BODY_CHARS = 80_000
diff --git a/mods/diff/hooks/views/detail/max-body-nodes/max-body-nodes.ts b/mods/diff/hooks/views/detail/max-body-nodes/max-body-nodes.ts
index 10f58af5..8980b3e7 100644
--- a/mods/diff/hooks/views/detail/max-body-nodes/max-body-nodes.ts
+++ b/mods/diff/hooks/views/detail/max-body-nodes/max-body-nodes.ts
@@ -1,5 +1,7 @@
/**
- * The most nodes a file's drawn body holds, strings included: under the
- * host's whole-tree cap (2 000) with room for the list and the pane's rest.
+ * The most nodes a file's drawn body holds, a `Code` one and a divider two.
+ *
+ * Under the host's whole-tree cap (2 000), with room for the list and the
+ * pane's rest.
*/
export const MAX_BODY_NODES = 1_200
diff --git a/mods/diff/hooks/views/detail/max-code-chars/index.ts b/mods/diff/hooks/views/detail/max-code-chars/index.ts
new file mode 100644
index 00000000..a45f7d53
--- /dev/null
+++ b/mods/diff/hooks/views/detail/max-code-chars/index.ts
@@ -0,0 +1,3 @@
+export * from './max-code-chars.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/max-code-chars/max-code-chars.ts b/mods/diff/hooks/views/detail/max-code-chars/max-code-chars.ts
new file mode 100644
index 00000000..9c7d3a4c
--- /dev/null
+++ b/mods/diff/hooks/views/detail/max-code-chars/max-code-chars.ts
@@ -0,0 +1,5 @@
+/**
+ * The most characters one `Code` source (or path) holds: the host's cap on
+ * a leaf's text (10 000), which a hooks module cannot import.
+ */
+export const MAX_CODE_CHARS = 10_000
diff --git a/mods/diff/hooks/views/detail/max-rows-of/frame-nodes/frame-nodes.ts b/mods/diff/hooks/views/detail/max-rows-of/frame-nodes/frame-nodes.ts
deleted file mode 100644
index 4a54c58e..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/frame-nodes/frame-nodes.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * The nodes the body costs before any row: its row Box, the gutter Text
- * beside it, and the column Box that holds the runs.
- */
-export const FRAME_NODES = 3
diff --git a/mods/diff/hooks/views/detail/max-rows-of/frame-nodes/index.ts b/mods/diff/hooks/views/detail/max-rows-of/frame-nodes/index.ts
deleted file mode 100644
index d163b7f8..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/frame-nodes/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './frame-nodes.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/max-rows-of/highlight-nodes/highlight-nodes.ts b/mods/diff/hooks/views/detail/max-rows-of/highlight-nodes/highlight-nodes.ts
deleted file mode 100644
index cf55ca56..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/highlight-nodes/highlight-nodes.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * The nodes one word highlight costs inside a run: the highlighted Text,
- * its string, and the string the run resumes with.
- */
-export const HIGHLIGHT_NODES = 3
diff --git a/mods/diff/hooks/views/detail/max-rows-of/highlight-nodes/index.ts b/mods/diff/hooks/views/detail/max-rows-of/highlight-nodes/index.ts
deleted file mode 100644
index 9c8766a9..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/highlight-nodes/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './highlight-nodes.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/max-rows-of/index.ts b/mods/diff/hooks/views/detail/max-rows-of/index.ts
deleted file mode 100644
index 64112310..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/index.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export * from './frame-nodes'
-export * from './highlight-nodes'
-export * from './max-rows-of.js'
-export * from './run-nodes'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/max-rows-of/max-rows-of.ts b/mods/diff/hooks/views/detail/max-rows-of/max-rows-of.ts
deleted file mode 100644
index a7cd3210..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/max-rows-of.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import Layout from '../../layout'
-import type { BodyCells } from '../hunk-body/body-cells'
-import { MARKER_CELLS } from '../marker-cells'
-import { MAX_BODY_CHARS } from '../max-body-chars'
-import { MAX_BODY_NODES } from '../max-body-nodes'
-import { MAX_RUN_CHARS } from '../max-run-chars'
-import type Types from '../types'
-import { FRAME_NODES } from './frame-nodes'
-import { HIGHLIGHT_NODES } from './highlight-nodes'
-import { RUN_NODES } from './run-nodes'
-
-/**
- * How many of these rows the body may draw under MAX_BODY_CHARS and
- * MAX_BODY_NODES, counted as the host counts what hunkBody draws.
- *
- * A row costs its gutter, marker, segments and padding in UTF-16 units (an
- * astral letter costs two); a change of kind opens a run (a Text and its
- * string), a highlight is three nodes, the gutter and long runs split.
- *
- * @param rows the laid-out rows, in order
- * @param cells the gutter's and the content's widths
- * @returns the count of leading rows whose drawn tree fits both budgets
- */
-export function maxRowsOf(rows: readonly Types.Row[], cells: BodyCells) {
- let chars = 0
- let nodes = FRAME_NODES
- let kept = 0
- let kind: Types.LineKind | undefined
-
- for (const row of rows) {
- let units = 0
- let charged = 0
- let highlights = 0
-
- for (const segment of row.segments) {
- units += segment.text.length
- charged += Layout.chargedWidth(segment.text)
- highlights += segment.isChanged ? 1 : 0
- }
-
- chars +=
- cells.gutterCells +
- 2 +
- MARKER_CELLS +
- units +
- Math.max(0, cells.contentCells - charged)
- nodes += (row.kind === kind ? 0 : RUN_NODES) + highlights * HIGHLIGHT_NODES
- kind = row.kind
- const splits = Math.ceil((2 * chars) / MAX_RUN_CHARS)
-
- if (chars > MAX_BODY_CHARS || nodes + splits > MAX_BODY_NODES) {
- return kept
- }
-
- kept += 1
- }
-
- return kept
-}
diff --git a/mods/diff/hooks/views/detail/max-rows-of/run-nodes/index.ts b/mods/diff/hooks/views/detail/max-rows-of/run-nodes/index.ts
deleted file mode 100644
index 75348395..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/run-nodes/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './run-nodes.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/max-rows-of/run-nodes/run-nodes.ts b/mods/diff/hooks/views/detail/max-rows-of/run-nodes/run-nodes.ts
deleted file mode 100644
index 46f77a05..00000000
--- a/mods/diff/hooks/views/detail/max-rows-of/run-nodes/run-nodes.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-/**
- * The nodes a change of line kind opens: a Text and its string.
- */
-export const RUN_NODES = 2
diff --git a/mods/diff/hooks/views/detail/max-run-chars/index.ts b/mods/diff/hooks/views/detail/max-run-chars/index.ts
deleted file mode 100644
index f6fd9b0b..00000000
--- a/mods/diff/hooks/views/detail/max-run-chars/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './max-run-chars.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/max-run-chars/max-run-chars.ts b/mods/diff/hooks/views/detail/max-run-chars/max-run-chars.ts
deleted file mode 100644
index a77d43e1..00000000
--- a/mods/diff/hooks/views/detail/max-run-chars/max-run-chars.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * The most characters one Text string of a run holds: under the host's
- * per-string cap (10 000), which a hooks module cannot import.
- */
-export const MAX_RUN_CHARS = 8_000
diff --git a/mods/diff/hooks/views/detail/rows-of/index.ts b/mods/diff/hooks/views/detail/rows-of/index.ts
deleted file mode 100644
index 04f748c7..00000000
--- a/mods/diff/hooks/views/detail/rows-of/index.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export * from './max-highlights'
-export * from './rows-of.js'
-export * from './segments-of'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/rows-of/max-highlights/index.ts b/mods/diff/hooks/views/detail/rows-of/max-highlights/index.ts
deleted file mode 100644
index 6e25c387..00000000
--- a/mods/diff/hooks/views/detail/rows-of/max-highlights/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './max-highlights.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/rows-of/max-highlights/max-highlights.ts b/mods/diff/hooks/views/detail/rows-of/max-highlights/max-highlights.ts
deleted file mode 100644
index accf1ccc..00000000
--- a/mods/diff/hooks/views/detail/rows-of/max-highlights/max-highlights.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * How many word-diff highlights one body may draw; past it lines draw plain,
- * keeping the tree under its node cap (each highlight is two nodes).
- */
-export const MAX_HIGHLIGHTS = 300
diff --git a/mods/diff/hooks/views/detail/rows-of/rows-of.ts b/mods/diff/hooks/views/detail/rows-of/rows-of.ts
deleted file mode 100644
index 36e44dfe..00000000
--- a/mods/diff/hooks/views/detail/rows-of/rows-of.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import { countOf } from '../../../count-of'
-import Layout from '../../layout'
-import type Types from '../types'
-import { MAX_HIGHLIGHTS } from './max-highlights'
-import { segmentsOf } from './segments-of'
-
-/**
- * Numbered lines laid into terminal rows: a long line wraps onto
- * continuation rows with an empty gutter.
- *
- * Word highlights stop once MAX_HIGHLIGHTS are spent, keeping the tree
- * under its node cap.
- *
- * @param lines the numbered lines
- * @param cells the content's room per row after gutter and marker, one at
- * least
- * @returns the rows in drawing order
- */
-export function rowsOf(
- lines: readonly Types.DiffLine[],
- cells: number,
-): Types.Row[] {
- const rows: Types.Row[] = []
- let highlights = 0
-
- for (const line of lines) {
- const marked = segmentsOf(line)
- const wanted = countOf(marked, segment => segment.isChanged)
- const isAffordable = highlights + wanted <= MAX_HIGHLIGHTS
- highlights += isAffordable ? wanted : 0
- const segments = isAffordable
- ? marked
- : [{ text: line.text, isChanged: false }]
- const marker =
- line.kind === 'added' ? '+' : line.kind === 'removed' ? '-' : ' '
-
- Layout.wrapSegments(segments, cells).forEach((wrapped, at) => {
- rows.push({
- kind: line.kind,
- gutter: at === 0 ? String(line.lineNumber) : '',
- marker: at === 0 ? marker : ' ',
- segments: wrapped,
- })
- })
- }
-
- return rows
-}
diff --git a/mods/diff/hooks/views/detail/rows-of/segments-of/index.ts b/mods/diff/hooks/views/detail/rows-of/segments-of/index.ts
deleted file mode 100644
index 1bbbdb86..00000000
--- a/mods/diff/hooks/views/detail/rows-of/segments-of/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './segments-of.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/rows-of/segments-of/segments-of.ts b/mods/diff/hooks/views/detail/rows-of/segments-of/segments-of.ts
deleted file mode 100644
index d38c2829..00000000
--- a/mods/diff/hooks/views/detail/rows-of/segments-of/segments-of.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { wordDiff } from '../../../../word-diff'
-import type Layout from '../../../layout'
-import type Types from '../../types'
-
-/**
- * A line's content as segments: one plain segment, or, for a paired line
- * whose word diff holds, its own side's parts with the changed ones marked.
- *
- * @param line the numbered line
- * @returns the segments in order
- */
-export function segmentsOf(line: Types.DiffLine): Layout.Segment[] {
- const plain = [{ text: line.text, isChanged: false }]
-
- if (line.pairedText === null || line.kind === 'context') {
- return plain
- }
-
- const isRemoved = line.kind === 'removed'
- const parts = isRemoved
- ? wordDiff(line.text, line.pairedText)
- : wordDiff(line.pairedText, line.text)
- const ownSide = isRemoved ? 'removed' : 'added'
-
- if (!parts) {
- return plain
- }
-
- return parts
- .filter(part => part.kind === 'same' || part.kind === ownSide)
- .map(part => ({ text: part.text, isChanged: part.kind === ownSide }))
-}
diff --git a/mods/diff/hooks/views/detail/runs-of/index.ts b/mods/diff/hooks/views/detail/runs-of/index.ts
deleted file mode 100644
index baee7840..00000000
--- a/mods/diff/hooks/views/detail/runs-of/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './runs-of.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/runs-of/runs-of.ts b/mods/diff/hooks/views/detail/runs-of/runs-of.ts
deleted file mode 100644
index c7539814..00000000
--- a/mods/diff/hooks/views/detail/runs-of/runs-of.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import type Types from '../types'
-
-/**
- * Rows grouped into runs of one kind, in order.
- *
- * @param rows the laid-out rows
- * @returns the runs
- */
-export function runsOf(rows: readonly Types.Row[]): Types.Run[] {
- const runs: Types.Run[] = []
- let start = 0
-
- rows.forEach((row, at) => {
- const isRunEnd = rows[at + 1]?.kind !== row.kind
-
- if (isRunEnd) {
- runs.push({ kind: row.kind, rows: rows.slice(start, at + 1) })
- start = at + 1
- }
- })
-
- return runs
-}
diff --git a/mods/diff/hooks/views/detail/sub-hunks-of/first-line-cut/first-line-cut.ts b/mods/diff/hooks/views/detail/sub-hunks-of/first-line-cut/first-line-cut.ts
new file mode 100644
index 00000000..ae5eaca3
--- /dev/null
+++ b/mods/diff/hooks/views/detail/sub-hunks-of/first-line-cut/first-line-cut.ts
@@ -0,0 +1,23 @@
+import type Git from '../../../../git'
+import { hunkHeaderOf } from '../../hunk-header-of'
+
+/**
+ * A hunk's first line cut so that it alone, under its header, fits the
+ * room: its marker kept, no surrogate pair split.
+ *
+ * @param hunk the hunk, its first line too long for a sub-hunk of its own
+ * @param maxChars the room the one-line sub-hunk's text fits
+ * @returns the cut line
+ */
+export function firstLineCut(hunk: Git.Hunk, maxChars: number) {
+ const [first = ''] = hunk.lines
+ const kept = first.slice(
+ 0,
+ Math.max(
+ 1,
+ maxChars - 1 - hunkHeaderOf({ ...hunk, lines: [first] }).length,
+ ),
+ )
+
+ return /[\uD800-\uDBFF]$/u.test(kept) ? kept.slice(0, -1) : kept
+}
diff --git a/mods/diff/hooks/views/detail/sub-hunks-of/first-line-cut/index.ts b/mods/diff/hooks/views/detail/sub-hunks-of/first-line-cut/index.ts
new file mode 100644
index 00000000..36981a12
--- /dev/null
+++ b/mods/diff/hooks/views/detail/sub-hunks-of/first-line-cut/index.ts
@@ -0,0 +1,3 @@
+export * from './first-line-cut.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/sub-hunks-of/index.ts b/mods/diff/hooks/views/detail/sub-hunks-of/index.ts
new file mode 100644
index 00000000..cf1c98de
--- /dev/null
+++ b/mods/diff/hooks/views/detail/sub-hunks-of/index.ts
@@ -0,0 +1,4 @@
+export * from './first-line-cut'
+export * from './sub-hunks-of.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/sub-hunks-of/sub-hunks-of.ts b/mods/diff/hooks/views/detail/sub-hunks-of/sub-hunks-of.ts
new file mode 100644
index 00000000..af1a51d4
--- /dev/null
+++ b/mods/diff/hooks/views/detail/sub-hunks-of/sub-hunks-of.ts
@@ -0,0 +1,43 @@
+import { countOf } from '../../../count-of'
+import type Git from '../../../git'
+import { leadingHunkOf } from '../leading-hunk-of'
+import { firstLineCut } from './first-line-cut'
+
+/**
+ * A hunk as consecutive sub-hunks whose texts (hunkSourceOf) each fit the
+ * room, each starting where the last left off, so the counts add up.
+ *
+ * A line too long for a sub-hunk of its own is cut to fit, not dropped, and
+ * `isTruncated` says so; a hunk that fits comes back whole.
+ *
+ * @param hunk the hunk, its lines body lines only
+ * @param maxChars the room each sub-hunk's text fits, in UTF-16 units
+ * @returns the sub-hunks in order, and whether a line was cut
+ */
+export function subHunksOf(
+ hunk: Git.Hunk,
+ maxChars: number,
+): Pick<Git.FileHunks, 'hunks' | 'isTruncated'> {
+ const hunks: Git.Hunk[] = []
+ let rest = hunk
+ let isTruncated = false
+
+ while (rest.lines.length > 0) {
+ const leading = leadingHunkOf(rest, maxChars)
+ const isLineCut = leading.lines.length === 0
+ const piece = isLineCut
+ ? { ...rest, lines: [firstLineCut(rest, maxChars)] }
+ : leading
+ const taken = rest.lines.slice(0, piece.lines.length)
+
+ hunks.push(piece)
+ isTruncated ||= isLineCut
+ rest = {
+ oldStart: rest.oldStart + countOf(taken, line => !line.startsWith('+')),
+ newStart: rest.newStart + countOf(taken, line => !line.startsWith('-')),
+ lines: rest.lines.slice(piece.lines.length),
+ }
+ }
+
+ return { hunks, isTruncated }
+}
diff --git a/mods/diff/hooks/views/detail/types/code-block/code-block.ts b/mods/diff/hooks/views/detail/types/code-block/code-block.ts
new file mode 100644
index 00000000..953360d7
--- /dev/null
+++ b/mods/diff/hooks/views/detail/types/code-block/code-block.ts
@@ -0,0 +1,8 @@
+/**
+ * One `Code` element of a file's body: its unified-diff source, and whether
+ * a divider stands above it (it opens a hunk after another block).
+ */
+export type CodeBlock = {
+ source: string
+ hasDivider: boolean
+}
diff --git a/mods/diff/hooks/views/detail/types/code-block/index.ts b/mods/diff/hooks/views/detail/types/code-block/index.ts
new file mode 100644
index 00000000..dd3f6adf
--- /dev/null
+++ b/mods/diff/hooks/views/detail/types/code-block/index.ts
@@ -0,0 +1,3 @@
+export type * from './code-block.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/types/code-body/code-body.ts b/mods/diff/hooks/views/detail/types/code-body/code-body.ts
new file mode 100644
index 00000000..7706a3a8
--- /dev/null
+++ b/mods/diff/hooks/views/detail/types/code-body/code-body.ts
@@ -0,0 +1,10 @@
+import type { CodeBlock } from '../code-block'
+
+/**
+ * A file's hunks as the body draws them: the `Code` blocks in order, and
+ * whether the body's budget or a source's cap left any of a line out.
+ */
+export type CodeBody = {
+ blocks: readonly CodeBlock[]
+ isTruncated: boolean
+}
diff --git a/mods/diff/hooks/views/detail/types/code-body/index.ts b/mods/diff/hooks/views/detail/types/code-body/index.ts
new file mode 100644
index 00000000..1951707e
--- /dev/null
+++ b/mods/diff/hooks/views/detail/types/code-body/index.ts
@@ -0,0 +1,3 @@
+export type * from './code-body.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/types/diff-line/diff-line.ts b/mods/diff/hooks/views/detail/types/diff-line/diff-line.ts
deleted file mode 100644
index 7a692663..00000000
--- a/mods/diff/hooks/views/detail/types/diff-line/diff-line.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import type { LineKind } from '../line-kind'
-
-/**
- * One hunk line ready to lay out: its side, its gutter's line number, its
- * sanitized text, and the paired line's text for a word diff, if any.
- *
- * The number is the new file's for context and added lines, the old
- * file's for removed ones; a removed line pairs with its added counterpart.
- */
-export type DiffLine = {
- kind: LineKind
- lineNumber: number
- text: string
- pairedText: string | null
-}
diff --git a/mods/diff/hooks/views/detail/types/diff-line/index.ts b/mods/diff/hooks/views/detail/types/diff-line/index.ts
deleted file mode 100644
index 74685334..00000000
--- a/mods/diff/hooks/views/detail/types/diff-line/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export type * from './diff-line.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/types/index.ts b/mods/diff/hooks/views/detail/types/index.ts
index 0bf55f05..d36f9e46 100644
--- a/mods/diff/hooks/views/detail/types/index.ts
+++ b/mods/diff/hooks/views/detail/types/index.ts
@@ -1,6 +1,4 @@
-export * from './diff-line'
-export * from './line-kind'
-export * from './row'
-export type * from './run.js'
+export * from './code-block'
+export * from './code-body'
export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/types/line-kind/index.ts b/mods/diff/hooks/views/detail/types/line-kind/index.ts
deleted file mode 100644
index 9b1925af..00000000
--- a/mods/diff/hooks/views/detail/types/line-kind/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export type * from './line-kind.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/types/line-kind/line-kind.ts b/mods/diff/hooks/views/detail/types/line-kind/line-kind.ts
deleted file mode 100644
index cc378105..00000000
--- a/mods/diff/hooks/views/detail/types/line-kind/line-kind.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-/**
- * Which side of the diff a line is on, read from its marker.
- */
-export type LineKind = 'context' | 'added' | 'removed'
diff --git a/mods/diff/hooks/views/detail/types/row/index.ts b/mods/diff/hooks/views/detail/types/row/index.ts
deleted file mode 100644
index 41a5adfa..00000000
--- a/mods/diff/hooks/views/detail/types/row/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export type * from './row.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/types/row/row.ts b/mods/diff/hooks/views/detail/types/row/row.ts
deleted file mode 100644
index 4893b9ca..00000000
--- a/mods/diff/hooks/views/detail/types/row/row.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import type Layout from '../../../layout'
-import type { LineKind } from '../line-kind'
-
-/**
- * One terminal row of a file's body: the gutter's text (the line number,
- * or nothing on a continuation), the marker, and the content's segments.
- */
-export type Row = {
- kind: LineKind
- gutter: string
- marker: string
- segments: readonly Layout.Segment[]
-}
diff --git a/mods/diff/hooks/views/detail/types/run.ts b/mods/diff/hooks/views/detail/types/run.ts
deleted file mode 100644
index 65faf3b4..00000000
--- a/mods/diff/hooks/views/detail/types/run.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import type { LineKind } from './line-kind'
-import type { Row } from './row'
-
-/**
- * Consecutive rows of one kind, drawn as one Text so a long file stays far
- * under the tree's node cap.
- */
-export type Run = {
- kind: LineKind
- rows: readonly Row[]
-}
diff --git a/mods/diff/hooks/views/detail/word-backgrounds/index.ts b/mods/diff/hooks/views/detail/word-backgrounds/index.ts
deleted file mode 100644
index 4c966806..00000000
--- a/mods/diff/hooks/views/detail/word-backgrounds/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './word-backgrounds.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/detail/word-backgrounds/word-backgrounds.ts b/mods/diff/hooks/views/detail/word-backgrounds/word-backgrounds.ts
deleted file mode 100644
index 65570c65..00000000
--- a/mods/diff/hooks/views/detail/word-backgrounds/word-backgrounds.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import type Types from '../types'
-
-/**
- * The theme key behind a changed word of each kind; a context line never
- * carries one, so it has none.
- */
-export const WORD_BACKGROUNDS: Readonly<
- Record<Types.LineKind, string | undefined>
-> = {
- added: 'diffAddedWord',
- removed: 'diffRemovedWord',
- context: undefined,
-}
diff --git a/mods/diff/hooks/views/kit/ui/ui.ts b/mods/diff/hooks/views/kit/ui/ui.ts
index b1fe9342..21e6dfe3 100644
--- a/mods/diff/hooks/views/kit/ui/ui.ts
+++ b/mods/diff/hooks/views/kit/ui/ui.ts
@@ -4,10 +4,10 @@ import type { ElementTable } from 'claude-code'
* The element constructors the pane draws with, destructured from the
* table `$.ui.resolve(e)` hands the render hook.
*
- * The terminal and the desktop carry all four; the mobile app has no
+ * The terminal and the desktop carry all five; the mobile app has no
* Select, so the pane never draws there.
*/
export type Ui = Pick<
ElementTable<'terminal' | 'desktop'>,
- 'Box' | 'Text' | 'Button' | 'Select'
+ 'Box' | 'Text' | 'Button' | 'Select' | 'Code'
>
diff --git a/mods/diff/hooks/views/layout/index.ts b/mods/diff/hooks/views/layout/index.ts
index 20264812..7ac964be 100644
--- a/mods/diff/hooks/views/layout/index.ts
+++ b/mods/diff/hooks/views/layout/index.ts
@@ -3,10 +3,7 @@ export * from './charged-width'
export * from './plural'
export * from './sanitize-line'
export * from './sanitize-name'
-export * from './segment'
-export * from './slice-cells'
export * from './tab-width'
export * from './truncate-start'
-export * from './wrap-segments'
export * as default from '.'
diff --git a/mods/diff/hooks/views/layout/segment/index.ts b/mods/diff/hooks/views/layout/segment/index.ts
deleted file mode 100644
index 7702d779..00000000
--- a/mods/diff/hooks/views/layout/segment/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export type * from './segment.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/layout/segment/segment.ts b/mods/diff/hooks/views/layout/segment/segment.ts
deleted file mode 100644
index 1799cbe0..00000000
--- a/mods/diff/hooks/views/layout/segment/segment.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-/**
- * One stretch of a drawn diff row: its text and whether the word diff
- * marks it as changed.
- */
-export type Segment = {
- text: string
- isChanged: boolean
-}
diff --git a/mods/diff/hooks/views/layout/slice-cells/cells-split/cells-split.ts b/mods/diff/hooks/views/layout/slice-cells/cells-split/cells-split.ts
deleted file mode 100644
index 7055d7ee..00000000
--- a/mods/diff/hooks/views/layout/slice-cells/cells-split/cells-split.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-/**
- * A line cut at a cell budget: the head that fits, the budget it spent
- * (a code point that draws in no cell still spends one), and the tail.
- */
-export type CellsSplit = {
- head: string
- spent: number
- tail: string
-}
diff --git a/mods/diff/hooks/views/layout/slice-cells/cells-split/index.ts b/mods/diff/hooks/views/layout/slice-cells/cells-split/index.ts
deleted file mode 100644
index b4ada917..00000000
--- a/mods/diff/hooks/views/layout/slice-cells/cells-split/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export type * from './cells-split.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/layout/slice-cells/index.ts b/mods/diff/hooks/views/layout/slice-cells/index.ts
deleted file mode 100644
index 110bebcb..00000000
--- a/mods/diff/hooks/views/layout/slice-cells/index.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-export * from './cells-split'
-export * from './slice-cells.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/layout/slice-cells/slice-cells.ts b/mods/diff/hooks/views/layout/slice-cells/slice-cells.ts
deleted file mode 100644
index 859d01fc..00000000
--- a/mods/diff/hooks/views/layout/slice-cells/slice-cells.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { chargedWidth } from '../charged-width'
-import type { CellsSplit } from './cells-split'
-
-/**
- * The longest head of a line that fits a cell budget, cut between code
- * points, and what is left after it.
- *
- * The budget is spent by chargedWidth, at least one cell a code point, so
- * a head never holds more code points than cells: a line of ten thousand
- * combining marks still wraps.
- *
- * @param text a sanitized line
- * @param cells the room, at least one cell
- * @returns the head that fits, the budget it spent, and the remaining tail
- */
-export function sliceCells(text: string, cells: number): CellsSplit {
- let spent = 0
- let taken = 0
- const characters = [...text]
-
- for (const character of characters) {
- const next = spent + chargedWidth(character)
-
- if (next > cells && taken > 0) {
- break
- }
-
- spent = next
- taken += 1
- }
-
- return {
- head: characters.slice(0, taken).join(''),
- spent,
- tail: characters.slice(taken).join(''),
- }
-}
diff --git a/mods/diff/hooks/views/layout/wrap-segments/index.ts b/mods/diff/hooks/views/layout/wrap-segments/index.ts
deleted file mode 100644
index 15c13b85..00000000
--- a/mods/diff/hooks/views/layout/wrap-segments/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './wrap-segments.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/views/layout/wrap-segments/wrap-segments.ts b/mods/diff/hooks/views/layout/wrap-segments/wrap-segments.ts
deleted file mode 100644
index 2d60129f..00000000
--- a/mods/diff/hooks/views/layout/wrap-segments/wrap-segments.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-import type { Segment } from '../segment'
-import { sliceCells } from '../slice-cells'
-
-/**
- * A line's segments laid into rows of at most a cell budget, a segment
- * split where a row fills, so each row draws as exactly one terminal row.
- *
- * A row holds no more code points than cells (sliceCells' spending), and
- * a continuation can sit under an empty gutter.
- *
- * @param segments the line's segments in order
- * @param cells the room per row, at least one cell
- * @returns the rows; one empty row for an empty line
- */
-export function wrapSegments(
- segments: readonly Segment[],
- cells: number,
-): Segment[][] {
- const rows: Segment[][] = []
- let row: Segment[] = []
- let used = 0
-
- for (const segment of segments) {
- let rest = segment.text
-
- while (rest !== '') {
- const split = sliceCells(rest, Math.max(1, cells - used))
- row.push({ text: split.head, isChanged: segment.isChanged })
- used += split.spent
- rest = split.tail
-
- if (rest !== '' || used >= cells) {
- rows.push(row)
- row = []
- used = 0
- }
- }
- }
-
- const isRowPending = row.length > 0 || rows.length === 0
-
- return isRowPending ? [...rows, row] : rows
-}
diff --git a/mods/diff/hooks/word-diff/change-threshold/change-threshold.ts b/mods/diff/hooks/word-diff/change-threshold/change-threshold.ts
deleted file mode 100644
index 02a48ae7..00000000
--- a/mods/diff/hooks/word-diff/change-threshold/change-threshold.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * Past this share of changed characters a line pair reads as rewritten, not
- * edited, and gets no word highlights (the engine's fallback diff rule).
- */
-export const CHANGE_THRESHOLD = 0.4
diff --git a/mods/diff/hooks/word-diff/change-threshold/index.ts b/mods/diff/hooks/word-diff/change-threshold/index.ts
deleted file mode 100644
index 82132d14..00000000
--- a/mods/diff/hooks/word-diff/change-threshold/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './change-threshold.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/word-diff/diff-parts/diff-parts.ts b/mods/diff/hooks/word-diff/diff-parts/diff-parts.ts
deleted file mode 100644
index 72a07e0b..00000000
--- a/mods/diff/hooks/word-diff/diff-parts/diff-parts.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { MAX_LCS_CELLS } from '../max-lcs-cells'
-import type { WordPart } from '../word-part'
-
-/**
- * The parts of a longest-common-subsequence diff of two token lists, in
- * order, adjacent parts of one kind merged.
- *
- * Null when the table would pass MAX_LCS_CELLS.
- *
- * @param before the removed line's tokens
- * @param after the added line's tokens
- * @returns the parts, or null for lines too long to compare
- */
-export function diffParts(
- before: readonly string[],
- after: readonly string[],
-): WordPart[] | null {
- const columns = after.length + 1
-
- if ((before.length + 1) * columns > MAX_LCS_CELLS) {
- return null
- }
-
- const lengths = new Uint32Array((before.length + 1) * columns)
- const at = (i: number, j: number) => lengths[i * columns + j] ?? 0
-
- for (let i = before.length - 1; i >= 0; i--) {
- for (let j = after.length - 1; j >= 0; j--) {
- const isMatch = before[i] === after[j]
- lengths[i * columns + j] = isMatch
- ? at(i + 1, j + 1) + 1
- : Math.max(at(i + 1, j), at(i, j + 1))
- }
- }
-
- const parts: WordPart[] = []
-
- function emit(text: string, kind: WordPart['kind']) {
- const last = parts.at(-1)
-
- if (last?.kind === kind) {
- last.text += text
-
- return
- }
-
- parts.push({ text, kind })
- }
-
- let i = 0
- let j = 0
-
- while (i < before.length || j < after.length) {
- const old = before[i]
- const fresh = after[j]
- const isSame = old !== undefined && old === fresh
- const isRemoved =
- !isSame &&
- old !== undefined &&
- (fresh === undefined || at(i + 1, j) >= at(i, j + 1))
-
- if (isSame) {
- emit(old, 'same')
- i += 1
- j += 1
- } else if (isRemoved) {
- emit(old, 'removed')
- i += 1
- } else if (fresh !== undefined) {
- emit(fresh, 'added')
- j += 1
- }
- }
-
- return parts
-}
diff --git a/mods/diff/hooks/word-diff/diff-parts/index.ts b/mods/diff/hooks/word-diff/diff-parts/index.ts
deleted file mode 100644
index 8be9cfce..00000000
--- a/mods/diff/hooks/word-diff/diff-parts/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './diff-parts.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/word-diff/index.ts b/mods/diff/hooks/word-diff/index.ts
deleted file mode 100644
index b6e603f7..00000000
--- a/mods/diff/hooks/word-diff/index.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-export * from './change-threshold'
-export * from './diff-parts'
-export * from './max-lcs-cells'
-export * from './tokens-of'
-export * from './word-diff.js'
-export * from './word-part'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/word-diff/max-lcs-cells/index.ts b/mods/diff/hooks/word-diff/max-lcs-cells/index.ts
deleted file mode 100644
index 68ceabec..00000000
--- a/mods/diff/hooks/word-diff/max-lcs-cells/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './max-lcs-cells.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/word-diff/max-lcs-cells/max-lcs-cells.ts b/mods/diff/hooks/word-diff/max-lcs-cells/max-lcs-cells.ts
deleted file mode 100644
index 6dca6759..00000000
--- a/mods/diff/hooks/word-diff/max-lcs-cells/max-lcs-cells.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/**
- * The largest token-count product a word diff will fill a table for; two
- * longer lines are shown without word highlights.
- */
-export const MAX_LCS_CELLS = 250_000
diff --git a/mods/diff/hooks/word-diff/tokens-of/index.ts b/mods/diff/hooks/word-diff/tokens-of/index.ts
deleted file mode 100644
index e1b0b5d7..00000000
--- a/mods/diff/hooks/word-diff/tokens-of/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './tokens-of.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/word-diff/tokens-of/tokens-of.ts b/mods/diff/hooks/word-diff/tokens-of/tokens-of.ts
deleted file mode 100644
index 528807f2..00000000
--- a/mods/diff/hooks/word-diff/tokens-of/tokens-of.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-/**
- * A line as the tokens a word diff compares: runs of word characters, runs
- * of whitespace (kept, as diffWordsWithSpace does), other characters alone.
- *
- * @param text one line without its diff marker
- * @returns the tokens, which join back to `text`
- */
-export const tokensOf = (text: string): string[] =>
- text.match(/\w+|\s+|[^\w\s]/gu) ?? []
diff --git a/mods/diff/hooks/word-diff/word-diff.ts b/mods/diff/hooks/word-diff/word-diff.ts
deleted file mode 100644
index ea7b2ca2..00000000
--- a/mods/diff/hooks/word-diff/word-diff.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { CHANGE_THRESHOLD } from './change-threshold'
-import { diffParts } from './diff-parts'
-import { tokensOf } from './tokens-of'
-import type { WordPart } from './word-part'
-
-/**
- * The word-level diff of a removed line and the added line paired with it,
- * or null when the pair reads as a rewrite or is too long to compare.
- *
- * A rewrite changed more than CHANGE_THRESHOLD of its characters; the
- * caller then draws both lines plain (the engine's fallback diff rule).
- *
- * @param removed the removed line's text, marker dropped
- * @param added the added line's text, marker dropped
- * @returns the parts in order, or null
- */
-export function wordDiff(removed: string, added: string): WordPart[] | null {
- const parts = diffParts(tokensOf(removed), tokensOf(added))
-
- if (!parts) {
- return null
- }
-
- const total = removed.length + added.length
- const changed = parts
- .filter(part => part.kind !== 'same')
- .reduce((sum, part) => sum + part.text.length, 0)
-
- return total > 0 && changed / total > CHANGE_THRESHOLD ? null : parts
-}
diff --git a/mods/diff/hooks/word-diff/word-part/index.ts b/mods/diff/hooks/word-diff/word-part/index.ts
deleted file mode 100644
index e7a36dbd..00000000
--- a/mods/diff/hooks/word-diff/word-part/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export type * from './word-part.js'
-
-export * as default from '.'
diff --git a/mods/diff/hooks/word-diff/word-part/word-part.ts b/mods/diff/hooks/word-diff/word-part/word-part.ts
deleted file mode 100644
index b7417ae8..00000000
--- a/mods/diff/hooks/word-diff/word-part/word-part.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-/**
- * One run of a word-level diff between a removed line and the added line
- * that replaced it: text both share, or text on one side only.
- */
-export type WordPart = {
- text: string
- kind: 'same' | 'added' | 'removed'
-}