spacehub

surshar / claude-code

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.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / views / detail / sub-hunks-of / sub-hunks-of.ts
39 lines · 1.2 KBRaw
1import Git from '../../../git'
2import { leadingHunkOf } from '../leading-hunk-of'
3import { firstLineCut } from './first-line-cut'
4
5/**
6 * A hunk as consecutive sub-hunks whose texts (hunkSourceOf) each fit the
7 * room, each starting where the last left off, so the counts add up.
8 *
9 * A line too long for a sub-hunk of its own is cut to fit, not dropped, and
10 * `isTruncated` says so; a hunk that fits comes back whole.
11 *
12 * @param hunk the hunk, its lines body lines only
13 * @param maxChars the room each sub-hunk's text fits, in UTF-16 units
14 * @returns the sub-hunks in order, and whether a line was cut
15 */
16export function subHunksOf(
17 hunk: Git.Hunk,
18 maxChars: number,
19): Pick<Git.FileHunks, 'hunks' | 'isTruncated'> {
20 const hunks: Git.Hunk[] = []
21
22 let rest = hunk
23 let isTruncated = false
24
25 while (rest.lines.length > 0) {
26 const leading = leadingHunkOf(rest, maxChars)
27 const isLineCut = leading.lines.length === 0
28
29 const piece = isLineCut
30 ? { ...rest, lines: [firstLineCut(rest, maxChars)] }
31 : leading
32
33 hunks.push(piece)
34 isTruncated ||= isLineCut
35 rest = Git.hunkAfter(rest, piece.lines.length)
36 }
37
38 return { hunks, isTruncated }
39}