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 / rows-of / segments-of / segments-of.ts
32 lines · 970 BRaw
1import { wordDiff } from '../../../../word-diff'
2import type Layout from '../../../layout'
3import type Types from '../../types'
4
5/**
6 * A line's content as segments: one plain segment, or, for a paired line
7 * whose word diff holds, its own side's parts with the changed ones marked.
8 *
9 * @param line the numbered line
10 * @returns the segments in order
11 */
12export function segmentsOf(line: Types.DiffLine): Layout.Segment[] {
13 const plain = [{ text: line.text, isChanged: false }]
14
15 if (line.pairedText === null || line.kind === 'context') {
16 return plain
17 }
18
19 const isRemoved = line.kind === 'removed'
20 const parts = isRemoved
21 ? wordDiff(line.text, line.pairedText)
22 : wordDiff(line.pairedText, line.text)
23 const ownSide = isRemoved ? 'removed' : 'added'
24
25 if (!parts) {
26 return plain
27 }
28
29 return parts
30 .filter(part => part.kind === 'same' || part.kind === ownSide)
31 .map(part => ({ text: part.text, isChanged: part.kind === ownSide }))
32}