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 / turns / turn-of / file-edit-of / file-edit-of.ts
53 lines · 1.4 KBRaw
1import { countOf } from '../../../count-of'
2import { isRecord } from '../../../is-record'
3import type Types from '../../types'
4import { hunksOf } from './hunks-of'
5
6/**
7 * One Edit or Write result as the file it changed, told as the built-in
8 * turn view tells an edit result; null for any other result.
9 *
10 * A `filePath` with a non-empty `structuredPatch`, or a `create` with its
11 * `content`, which becomes one all-`+` hunk.
12 *
13 * @param result what the transcript stored for the tool call
14 * @returns the file's edit, or null
15 */
16export function fileEditOf(result: unknown): Types.TurnFile | null {
17 if (!isRecord(result) || typeof result.filePath !== 'string') {
18 return null
19 }
20
21 const hunks = hunksOf(result.structuredPatch)
22 const isNewFile = result.type === 'create'
23
24 if (hunks.length > 0) {
25 const lines = hunks.flatMap(hunk => hunk.lines)
26
27 return {
28 path: result.filePath,
29 hunks,
30 added: countOf(lines, line => line.startsWith('+')),
31 removed: countOf(lines, line => line.startsWith('-')),
32 isNewFile,
33 isTruncated: false,
34 }
35 }
36
37 if (!isNewFile || typeof result.content !== 'string') {
38 return null
39 }
40
41 const created = result.content.split('\n')
42
43 return {
44 path: result.filePath,
45 hunks: [
46 { oldStart: 0, newStart: 1, lines: created.map(line => `+${line}`) },
47 ],
48 added: created.length,
49 removed: 0,
50 isNewFile: true,
51 isTruncated: false,
52 }
53}