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