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 / lines-of / lines-of.ts
74 lines · 1.8 KBRaw
1import type Git from '../../../git'
2import Layout from '../../layout'
3import type Types from '../types'
4
5/**
6 * A file's hunks as numbered lines, hunk after hunk: context and added
7 * lines count the new file's lines, removed ones the old file's.
8 *
9 * Each run of removed lines is paired index by index with the added run
10 * right after it, for the word diff, as the engine's fallback diff pairs.
11 *
12 * @param hunks the parsed hunks
13 * @returns the lines in drawing order
14 */
15export function linesOf(hunks: readonly Git.Hunk[]): Types.DiffLine[] {
16 const lines: Types.DiffLine[] = []
17
18 for (const hunk of hunks) {
19 let oldNumber = hunk.oldStart
20 let newNumber = hunk.newStart
21 let removedRun: Types.DiffLine[] = []
22 let pairing = 0
23
24 for (const raw of hunk.lines) {
25 const text = Layout.sanitizeLine(raw.slice(1))
26 const kind: Types.LineKind = raw.startsWith('+')
27 ? 'added'
28 : raw.startsWith('-')
29 ? 'removed'
30 : 'context'
31
32 if (kind === 'removed') {
33 const isNewRun = pairing > 0
34
35 if (isNewRun) {
36 removedRun = []
37 pairing = 0
38 }
39
40 const line = { kind, lineNumber: oldNumber, text, pairedText: null }
41 removedRun.push(line)
42 lines.push(line)
43 oldNumber += 1
44 continue
45 }
46
47 if (kind === 'added') {
48 const partner = removedRun[pairing]
49
50 if (partner) {
51 partner.pairedText = text
52 pairing += 1
53 }
54
55 lines.push({
56 kind,
57 lineNumber: newNumber,
58 text,
59 pairedText: partner ? partner.text : null,
60 })
61 newNumber += 1
62 continue
63 }
64
65 removedRun = []
66 pairing = 0
67 lines.push({ kind, lineNumber: newNumber, text, pairedText: null })
68 oldNumber += 1
69 newNumber += 1
70 }
71 }
72
73 return lines
74}