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 / hunk-header-of / hunk-header-of.ts
23 lines · 945 BRaw
1import { countOf } from '../../../count-of'
2import type Git from '../../../git'
3
4/**
5 * A hunk's `@@ -a,b +c,d @@` header, its counts read off its lines, so a
6 * cut or split hunk still parses: context counts on both sides.
7 *
8 * A side with no lines names the line before its start, as jsdiff's
9 * formatPatch writes a structured hunk and the engine's parser reads back.
10 *
11 * @param hunk the hunk, its lines body lines only
12 * @returns the header line
13 */
14export function hunkHeaderOf(hunk: Git.Hunk) {
15 const oldLines = countOf(hunk.lines, line => !line.startsWith('+'))
16 const newLines = countOf(hunk.lines, line => !line.startsWith('-'))
17 const isOldEmpty = oldLines === 0
18 const isNewEmpty = newLines === 0
19 const oldStart = isOldEmpty ? Math.max(0, hunk.oldStart - 1) : hunk.oldStart
20 const newStart = isNewEmpty ? Math.max(0, hunk.newStart - 1) : hunk.newStart
21
22 return `@@ -${oldStart},${oldLines} +${newStart},${newLines} @@`
23}