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 / leading-hunk-of / leading-hunk-of.ts
32 lines · 827 BRaw
1import type Git from '../../../git'
2import { hunkHeaderOf } from '../hunk-header-of'
3
4/**
5 * The most of a hunk's first lines whose text (hunkSourceOf) fits the
6 * room, whole lines only: none when the first line alone does not fit.
7 *
8 * @param hunk the hunk, its lines body lines only
9 * @param maxChars the room, in UTF-16 units
10 * @returns the hunk cut to those lines, its starts unchanged
11 */
12export function leadingHunkOf(hunk: Git.Hunk, maxChars: number): Git.Hunk {
13 let bodyChars = 0
14 let taken = 0
15
16 for (const line of hunk.lines) {
17 const header = hunkHeaderOf({
18 ...hunk,
19 lines: hunk.lines.slice(0, taken + 1),
20 })
21
22 bodyChars += 1 + line.length
23
24 if (header.length + bodyChars > maxChars) {
25 break
26 }
27
28 taken += 1
29 }
30
31 return { ...hunk, lines: hunk.lines.slice(0, taken) }
32}