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 / word-diff / word-diff.ts
30 lines · 1.0 KBRaw
1import { CHANGE_THRESHOLD } from './change-threshold'
2import { diffParts } from './diff-parts'
3import { tokensOf } from './tokens-of'
4import type { WordPart } from './word-part'
5
6/**
7 * The word-level diff of a removed line and the added line paired with it,
8 * or null when the pair reads as a rewrite or is too long to compare.
9 *
10 * A rewrite changed more than CHANGE_THRESHOLD of its characters; the
11 * caller then draws both lines plain (the engine's fallback diff rule).
12 *
13 * @param removed the removed line's text, marker dropped
14 * @param added the added line's text, marker dropped
15 * @returns the parts in order, or null
16 */
17export function wordDiff(removed: string, added: string): WordPart[] | null {
18 const parts = diffParts(tokensOf(removed), tokensOf(added))
19
20 if (!parts) {
21 return null
22 }
23
24 const total = removed.length + added.length
25 const changed = parts
26 .filter(part => part.kind !== 'same')
27 .reduce((sum, part) => sum + part.text.length, 0)
28
29 return total > 0 && changed / total > CHANGE_THRESHOLD ? null : parts
30}