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 / tests / fixtures / code-blocks-of / hunks-of.ts
36 lines · 971 BRaw
1import type { ParsedHunk } from './parsed-hunk.js'
2
3/**
4 * The hunks a unified-diff `Code` source holds, read as a surface reads
5 * them.
6 *
7 * Each `@@ -a,b +c,d @@` header opens one and the lines under it are its
8 * body; a source that opens with no header holds none.
9 *
10 * @param source the `Code` element's source
11 * @returns the hunks in order
12 */
13export function hunksOf(source: string): ParsedHunk[] {
14 const hunks: ParsedHunk[] = []
15
16 for (const line of source.split('\n')) {
17 const header = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line)
18 const last = hunks.at(-1)
19
20 if (header) {
21 hunks.push({
22 oldStart: Number(header[1]),
23 oldLines: Number(header[2] ?? 1),
24 newStart: Number(header[3]),
25 newLines: Number(header[4] ?? 1),
26 lines: [],
27 })
28 } else if (last) {
29 hunks[hunks.length - 1] = { ...last, lines: [...last.lines, line] }
30 } else {
31 return []
32 }
33 }
34
35 return hunks
36}