MCPcopy Create free account
hub / github.com/IIIIQIIII/claude-code / wordDiffStrings

Function wordDiffStrings

src/native-ts/color-diff/index.ts:606–638  ·  view source on GitHub ↗
(oldStr: string, newStr: string)

Source from the content-addressed store, hash-verified

604}
605
606function wordDiffStrings(oldStr: string, newStr: string): [Range[], Range[]] {
607 const oldTokens = tokenize(oldStr)
608 const newTokens = tokenize(newStr)
609 const ops = diffArrays(oldTokens, newTokens)
610
611 const totalLen = oldStr.length + newStr.length
612 let changedLen = 0
613 const oldRanges: Range[] = []
614 const newRanges: Range[] = []
615 let oldOff = 0
616 let newOff = 0
617
618 for (const op of ops) {
619 const len = op.value.reduce((s, t) => s + t.length, 0)
620 if (op.removed) {
621 changedLen += len
622 oldRanges.push({ start: oldOff, end: oldOff + len })
623 oldOff += len
624 } else if (op.added) {
625 changedLen += len
626 newRanges.push({ start: newOff, end: newOff + len })
627 newOff += len
628 } else {
629 oldOff += len
630 newOff += len
631 }
632 }
633
634 if (totalLen > 0 && changedLen / totalLen > CHANGE_THRESHOLD) {
635 return [[], []]
636 }
637 return [oldRanges, newRanges]
638}
639
640// ---------------------------------------------------------------------------
641// Highlight (per-line transform pipeline)

Callers 1

renderMethod · 0.85

Calls 1

tokenizeFunction · 0.70

Tested by

no test coverage detected