diff --git a/README.md b/README.md index a970489..5a1ed55 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ ## 功能 - **仅差异行**:完全相同行直接不渲染,只显示真实变化的删除行红 `-` 与新增行绿 `+`,消除重复。 -- **行内字符高亮**:删/增行数相等的替换对,再做字符级 diff,用下划线在行内标出真正改动的字符,而不是整行糊在一起。 +- **行内字符高亮**:替换对再做字符级 diff,用下划线在行内标出真正改动的字符,而不是整行糊在一起。 +- **不等长替换配对**:删增行数不等时按行相似度做单调对齐,表头对表头、数据行对数据行,只有多出来的行单独显示,不再整块红绿糊在一起。 - **近线性算法**:先做公共前缀/后缀收缩线性,仅对中间小差异带运行 Myers diff,复杂度与差异量成正比而非平方,超大文件也流畅。 - **单行过长保护**:一行改动区超过 512 字符时不再逐字符对齐,整段标成改动;行级差异超过 600 行同样退化为整块删除加新增,宁可不精细也不让页面卡死。 - **默认收起**:卡片默认一行摘要,点开才显示 diff 体,不刷屏。 diff --git a/package.json b/package.json index 5a2000c..27651b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-edit-diff", - "version": "0.4.3", + "version": "0.4.4", "description": "「DSH·质量 diff」:接管 edit/write 及 extraTools 配置的其他工具卡片:行级去重与行内高亮,消除相同内容在删除与新增两区的重复;兼容 PTC 模式,标准可安装的 dsh 客户端插件", "keywords": [ "deepseek-harness", diff --git a/src/client.ts b/src/client.ts index 25e131a..2fb55b3 100644 --- a/src/client.ts +++ b/src/client.ts @@ -314,7 +314,68 @@ function contentLines(text: string): string[] { return (text.endsWith('\n') ? text.slice(0, -1) : text).split('\n') } -/** 从 ops 构建渲染行,仅含真实差异;替换对删/增行数相等做行内字符高亮。 */ +/** 不等长替换块的对齐上限,行数与字符总量都卡住,超限退回整块显示。 */ +const MAX_ALIGN_LINES = 200 +const MAX_ALIGN_CHARS = 200_000 + +/** 行相似度低于这个值就不配对,避免无关行被硬凑成一对。 */ +const MIN_PAIR_SIMILARITY = 0.5 + +/** 行相似度:公共前后缀合计占较长行的比例,线性于行长。 */ +function lineSimilarity(a: string, b: string): number { + const maxLen = Math.max(a.length, b.length) + if (maxLen === 0) return 1 + const minLen = Math.min(a.length, b.length) + let p = 0 + while (p < minLen && a[p] === b[p]) p++ + let s = 0 + while (s < minLen - p && a[a.length - 1 - s] === b[b.length - 1 - s]) s++ + return (p + s) / maxLen +} + +/** 单调对齐替换块两侧的行,返回 [删除行下标, 新增行下标] 序列;超过规模上限返回 null。 */ +function alignBlock(olds: readonly string[], adds: readonly string[]): Array<[number, number]> | null { + const n = olds.length + const m = adds.length + if (n === 0 || m === 0) return [] + if (n + m > MAX_ALIGN_LINES) return null + let chars = 0 + for (const line of olds) chars += line.length + for (const line of adds) chars += line.length + if (chars > MAX_ALIGN_CHARS) return null + + const dp: number[][] = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0)) + const from: number[][] = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0)) + for (let i = 1; i <= n; i++) { + for (let j = 1; j <= m; j++) { + const up = dp[i - 1]![j] as number + const left = dp[i]![j - 1] as number + let best = up + let move = 0 + if (left > best) { best = left; move = 1 } + const sim = lineSimilarity(olds[i - 1] as string, adds[j - 1] as string) + if (sim >= MIN_PAIR_SIMILARITY) { + const diag = (dp[i - 1]![j - 1] as number) + sim + if (diag > best) { best = diag; move = 2 } + } + dp[i]![j] = best + from[i]![j] = move + } + } + const pairs: Array<[number, number]> = [] + let i = n + let j = m + while (i > 0 && j > 0) { + const move = from[i]![j] as number + if (move === 2) { pairs.push([i - 1, j - 1]); i--; j-- } + else if (move === 0) i-- + else j-- + } + pairs.reverse() + return pairs +} + +/** 从 ops 构建渲染行,仅含真实差异;替换块按相似度配对,配对行做行内字符高亮。 */ function buildDiffLines(oldLines: readonly string[], newLines: readonly string[]): RenderLine[] { const ops = lineDiff(oldLines, newLines) const out: RenderLine[] = [] @@ -327,21 +388,27 @@ function buildDiffLines(oldLines: readonly string[], newLines: readonly string[] while (i < ops.length && ops[i]?.type === 'add') i++ const delOps = ops.slice(dStart, addStart) const addOps = ops.slice(addStart, i) - if (delOps.length === addOps.length && delOps.length > 0) { - for (let k = 0; k < delOps.length; k++) { - const del = delOps[k] - const add = addOps[k] - if (del === undefined || add === undefined) continue - const oldText = oldLines[del.a] ?? '' - const newText = newLines[add.b] ?? '' - const hl = lineHighlight(oldText, newText) - out.push({ kind: 'del', text: oldText, hl: hl.del }) - out.push({ kind: 'add', text: newText, hl: hl.add }) - } - } else { - for (const op of delOps) out.push({ kind: 'del', text: oldLines[op.a] ?? '', hl: [] }) - for (const op of addOps) out.push({ kind: 'add', text: newLines[op.b] ?? '', hl: [] }) + const delTexts = delOps.map(op => oldLines[op.a] ?? '') + const addTexts = addOps.map(op => newLines[op.b] ?? '') + const pairs = alignBlock(delTexts, addTexts) + if (pairs === null) { + for (const text of delTexts) out.push({ kind: 'del', text, hl: [] }) + for (const text of addTexts) out.push({ kind: 'add', text, hl: [] }) + continue + } + let di = 0 + let ai = 0 + for (const [pd, pa] of pairs) { + while (di < pd) { out.push({ kind: 'del', text: delTexts[di] as string, hl: [] }); di++ } + while (ai < pa) { out.push({ kind: 'add', text: addTexts[ai] as string, hl: [] }); ai++ } + const hl = lineHighlight(delTexts[pd] as string, addTexts[pa] as string) + out.push({ kind: 'del', text: delTexts[pd] as string, hl: hl.del }) + out.push({ kind: 'add', text: addTexts[pa] as string, hl: hl.add }) + di = pd + 1 + ai = pa + 1 } + while (di < delTexts.length) { out.push({ kind: 'del', text: delTexts[di] as string, hl: [] }); di++ } + while (ai < addTexts.length) { out.push({ kind: 'add', text: addTexts[ai] as string, hl: [] }); ai++ } } return out }