dsh-vscode-mode 0.29.0 → 0.29.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -4
- package/lib/index.js +402 -182
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/pending/hunks.ts +6 -150
- package/src/pending/lineDiff.ts +413 -0
- package/src/shared/review.ts +5 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-vscode-mode",
|
|
3
|
-
"version": "0.29.
|
|
3
|
+
"version": "0.29.1",
|
|
4
4
|
"description": "DSH 上的类 VSCode 编码体验:Monaco 编辑器(文件页签/QuickOpen/状态栏,可驻 DSH 0.1.5+ 官方右侧 Sidebar 与对话同屏)+ 命令栏(Ctrl+Shift+P)与可扩展指令系统 + VS Code 兼容代码片段(.code-snippets 全局/项目,IntelliSense 展开)+ Agent 编辑差异审查(整文件差异/采纳/拒绝/归档/回滚)+ SVN 集成(侧栏变更面板/日志弹窗/并排差异)+ 语言服务器(LSP)智能(跳转定义/引用/hover/大纲 + VSIX 扩展市场安装),状态持久化到工作区旁车,界面状态(页签/光标/侧栏/编辑 Tab/导航历史)镜像到 DSH home 跨重启恢复",
|
|
5
5
|
"icon": "assets/icon.svg",
|
|
6
6
|
"keywords": [
|
package/src/pending/hunks.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 待审差异构造:把两侧全文变成官方视图复用的 hunk,并提供块级 Keep/Revert 的文本合成。
|
|
3
|
+
* 行差异交给 lineDiff 引擎(唯一行锚点切分 + 小窗口 LCS),只有真整段替换才标 coarse。
|
|
3
4
|
* @author ddj 2026年10月29号
|
|
4
5
|
*/
|
|
5
|
-
import type { OfficialDiff
|
|
6
|
+
import type { OfficialDiff } from '../shared/review.js'
|
|
6
7
|
import type { ReviewBlock } from '../shared/reviewRegions.js'
|
|
7
8
|
import { logicalLines } from '../shared/lineEndings.js'
|
|
8
|
-
|
|
9
|
-
/** 每个 hunk 的上下文行数。 */
|
|
10
|
-
const CONTEXT = 3
|
|
11
|
-
/** LCS 单元格上限:超过后退化为整段替换,避免大文件卡死。 */
|
|
12
|
-
const CELL_LIMIT = 1_000_000
|
|
13
|
-
|
|
14
|
-
/** 一行差异操作。 */
|
|
15
|
-
type LineOp = { kind: ' ' | '-' | '+'; text: string }
|
|
9
|
+
import { lineHunks } from './lineDiff.js'
|
|
16
10
|
|
|
17
11
|
/**
|
|
18
12
|
* @author ddj 2026年10月29号
|
|
@@ -21,11 +15,11 @@ type LineOp = { kind: ' ' | '-' | '+'; text: string }
|
|
|
21
15
|
* @param display 展示名
|
|
22
16
|
* @param oldText 基线全文
|
|
23
17
|
* @param newText 当前全文
|
|
24
|
-
* @returns
|
|
18
|
+
* @returns 文本差异;coarse 表示比较中确有区域退化为整段替换
|
|
25
19
|
*/
|
|
26
20
|
export function textDiff(path: string, display: string, oldText: string, newText: string): OfficialDiff {
|
|
27
|
-
const hunks =
|
|
28
|
-
return { kind: 'text', path, display, before: true, after: true, hunks, coarse:
|
|
21
|
+
const { hunks, degraded } = lineHunks(logicalLines(oldText), logicalLines(newText))
|
|
22
|
+
return { kind: 'text', path, display, before: true, after: true, hunks, coarse: degraded }
|
|
29
23
|
}
|
|
30
24
|
|
|
31
25
|
/**
|
|
@@ -101,141 +95,3 @@ function locateSpan(lines: string[], pattern: string[]): number | null {
|
|
|
101
95
|
}
|
|
102
96
|
return at >= 0 ? at : null
|
|
103
97
|
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* @author ddj 2026年10月29号
|
|
107
|
-
* @description: 生成带上下文的标准 hunk 列表。
|
|
108
|
-
* @param oldLines 基线行
|
|
109
|
-
* @param newLines 当前行
|
|
110
|
-
* @returns hunk 列表
|
|
111
|
-
*/
|
|
112
|
-
function buildHunks(oldLines: string[], newLines: string[]): ReviewHunk[] {
|
|
113
|
-
const ops = diffLines(oldLines, newLines)
|
|
114
|
-
const hunks: ReviewHunk[] = []
|
|
115
|
-
let index = 0
|
|
116
|
-
let oldLine = 0
|
|
117
|
-
let newLine = 0
|
|
118
|
-
while (index < ops.length) {
|
|
119
|
-
if (ops[index].kind === ' ') {
|
|
120
|
-
oldLine++
|
|
121
|
-
newLine++
|
|
122
|
-
index++
|
|
123
|
-
continue
|
|
124
|
-
}
|
|
125
|
-
const head = ops.slice(Math.max(0, index - CONTEXT), index)
|
|
126
|
-
const collected: LineOp[] = []
|
|
127
|
-
let probe = index
|
|
128
|
-
while (probe < ops.length) {
|
|
129
|
-
const op = ops[probe]
|
|
130
|
-
if (op.kind !== ' ') { collected.push(op); probe++; continue }
|
|
131
|
-
const rest = ops.slice(probe, probe + CONTEXT * 2 + 1)
|
|
132
|
-
if (rest.some((item) => item.kind !== ' ')) { collected.push(op); probe++; continue }
|
|
133
|
-
break
|
|
134
|
-
}
|
|
135
|
-
const tail = ops.slice(probe, probe + CONTEXT)
|
|
136
|
-
const oldStartLine = oldLine - head.length + 1
|
|
137
|
-
const newStartLine = newLine - head.length + 1
|
|
138
|
-
hunks.push(makeHunk([...head, ...collected, ...tail], oldStartLine, newStartLine))
|
|
139
|
-
for (const op of collected) {
|
|
140
|
-
if (op.kind !== '+') oldLine++
|
|
141
|
-
if (op.kind !== '-') newLine++
|
|
142
|
-
}
|
|
143
|
-
oldLine += head.length + tail.length
|
|
144
|
-
newLine += head.length + tail.length
|
|
145
|
-
index = probe + tail.length
|
|
146
|
-
}
|
|
147
|
-
return hunks
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* @author ddj 2026年10月29号
|
|
152
|
-
* @description: 由一段操作装配 hunk。
|
|
153
|
-
* @param ops 该 hunk 的操作
|
|
154
|
-
* @param oldStart 基线起始行
|
|
155
|
-
* @param newStart 当前起始行
|
|
156
|
-
* @returns hunk
|
|
157
|
-
*/
|
|
158
|
-
function makeHunk(ops: LineOp[], oldStart: number, newStart: number): ReviewHunk {
|
|
159
|
-
const oldCount = ops.filter((op) => op.kind !== '+').length
|
|
160
|
-
const newCount = ops.filter((op) => op.kind !== '-').length
|
|
161
|
-
return { oldStart, oldLines: oldCount, newStart, newLines: newCount, lines: ops.map((op) => op.kind + op.text) }
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* @author ddj 2026年10月29号
|
|
166
|
-
* @description: 行级差异:先剪公共前后缀,剩余部分在限额内做 LCS,超限按整段替换。
|
|
167
|
-
* @param oldLines 基线行
|
|
168
|
-
* @param newLines 当前行
|
|
169
|
-
* @returns 顺序完整的操作列表
|
|
170
|
-
*/
|
|
171
|
-
function diffLines(oldLines: string[], newLines: string[]): LineOp[] {
|
|
172
|
-
let head = 0
|
|
173
|
-
while (head < oldLines.length && head < newLines.length && oldLines[head] === newLines[head]) head++
|
|
174
|
-
let tail = 0
|
|
175
|
-
while (tail < oldLines.length - head && tail < newLines.length - head
|
|
176
|
-
&& oldLines[oldLines.length - 1 - tail] === newLines[newLines.length - 1 - tail]) tail++
|
|
177
|
-
const left = oldLines.slice(head, oldLines.length - tail)
|
|
178
|
-
const right = newLines.slice(head, newLines.length - tail)
|
|
179
|
-
const middle = left.length * right.length > CELL_LIMIT ? coarseOps(left, right) : lcsOps(left, right)
|
|
180
|
-
return [
|
|
181
|
-
...oldLines.slice(0, head).map((text) => ({ kind: ' ' as const, text })),
|
|
182
|
-
...middle,
|
|
183
|
-
...oldLines.slice(oldLines.length - tail).map((text) => ({ kind: ' ' as const, text })),
|
|
184
|
-
]
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* @author ddj 2026年10月29号
|
|
189
|
-
* @description: 超限时的退化口径:整段删除再整段新增,绝不谎报逐行对应。
|
|
190
|
-
* @param left 基线剩余行
|
|
191
|
-
* @param right 当前剩余行
|
|
192
|
-
* @returns 操作列表
|
|
193
|
-
*/
|
|
194
|
-
function coarseOps(left: string[], right: string[]): LineOp[] {
|
|
195
|
-
return [
|
|
196
|
-
...left.map((text) => ({ kind: '-' as const, text })),
|
|
197
|
-
...right.map((text) => ({ kind: '+' as const, text })),
|
|
198
|
-
]
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* @author ddj 2026年10月29号
|
|
203
|
-
* @description: 限额内的 LCS 行差异。
|
|
204
|
-
* @param left 基线剩余行
|
|
205
|
-
* @param right 当前剩余行
|
|
206
|
-
* @returns 操作列表
|
|
207
|
-
*/
|
|
208
|
-
function lcsOps(left: string[], right: string[]): LineOp[] {
|
|
209
|
-
const rows = left.length + 1
|
|
210
|
-
const cols = right.length + 1
|
|
211
|
-
const table = new Uint32Array(rows * cols)
|
|
212
|
-
for (let i = left.length - 1; i >= 0; i--) {
|
|
213
|
-
for (let j = right.length - 1; j >= 0; j--) {
|
|
214
|
-
table[i * cols + j] = left[i] === right[j]
|
|
215
|
-
? table[(i + 1) * cols + j + 1] + 1
|
|
216
|
-
: Math.max(table[(i + 1) * cols + j], table[i * cols + j + 1])
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
const ops: LineOp[] = []
|
|
220
|
-
let i = 0
|
|
221
|
-
let j = 0
|
|
222
|
-
while (i < left.length && j < right.length) {
|
|
223
|
-
if (left[i] === right[j]) { ops.push({ kind: ' ', text: left[i] }); i++; j++; continue }
|
|
224
|
-
if (table[(i + 1) * cols + j] >= table[i * cols + j + 1]) { ops.push({ kind: '-', text: left[i] }); i++ }
|
|
225
|
-
else { ops.push({ kind: '+', text: right[j] }); j++ }
|
|
226
|
-
}
|
|
227
|
-
while (i < left.length) ops.push({ kind: '-', text: left[i++] })
|
|
228
|
-
while (j < right.length) ops.push({ kind: '+', text: right[j++] })
|
|
229
|
-
return ops
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* @author ddj 2026年10月29号
|
|
234
|
-
* @description: 判断这次差异是否触发了大文件退化口径。
|
|
235
|
-
* @param oldText 基线全文
|
|
236
|
-
* @param newText 当前全文
|
|
237
|
-
* @returns 是否粗粒度
|
|
238
|
-
*/
|
|
239
|
-
function isCoarse(oldText: string, newText: string): boolean {
|
|
240
|
-
return logicalLines(oldText).length * logicalLines(newText).length > CELL_LIMIT
|
|
241
|
-
}
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 行级差异引擎:先裁公共前后缀,再按唯一行锚点切分,最后在小窗口内做 LCS。
|
|
3
|
+
* 千行文件上的少量/分散改动不再因单元格上限退化成整段替换;只有确实找不到
|
|
4
|
+
* 可匹配行的区域才按整段替换处理并标记 degraded。
|
|
5
|
+
* @author ddj 2026年11月11号
|
|
6
|
+
*/
|
|
7
|
+
import type { ReviewHunk } from '../shared/review.js'
|
|
8
|
+
|
|
9
|
+
// #region 类型与常量
|
|
10
|
+
/** 一行差异操作。 */
|
|
11
|
+
export type LineOp = { kind: ' ' | '-' | '+'; text: string }
|
|
12
|
+
|
|
13
|
+
/** 一次行差异:hunk 列表与是否发生整段替换退化。 */
|
|
14
|
+
export interface LineDiff { hunks: ReviewHunk[]; degraded: boolean }
|
|
15
|
+
|
|
16
|
+
/** 两侧都唯一出现一次的相同行配对,用于把窗口切成更小的子窗口。 */
|
|
17
|
+
interface Anchor { oldIndex: number; newIndex: number }
|
|
18
|
+
|
|
19
|
+
/** 一次差异的累计预算与退化标记,按引用在递归间共享。 */
|
|
20
|
+
interface WorkState { cells: number; degraded: boolean }
|
|
21
|
+
|
|
22
|
+
/** 单窗口 LCS 单元格上限:超过先找锚点;4 MB 表实测约 7 ms。 */
|
|
23
|
+
const LCS_CELLS = 1_000_000
|
|
24
|
+
|
|
25
|
+
/** 一次差异的 LCS 总预算:防止病态输入拖慢 Host,约 80 ms 量级封顶。 */
|
|
26
|
+
const WORK_CELLS = 12_000_000
|
|
27
|
+
|
|
28
|
+
/** 锚点递归深度上限:超过即把该窗口按整段替换处理。 */
|
|
29
|
+
const MAX_DEPTH = 64
|
|
30
|
+
|
|
31
|
+
/** 每个 hunk 的上下文行数。 */
|
|
32
|
+
const CONTEXT = 3
|
|
33
|
+
// #endregion
|
|
34
|
+
|
|
35
|
+
// #region 差异引擎
|
|
36
|
+
/**
|
|
37
|
+
* @public
|
|
38
|
+
* @author ddj 2026年11月11号
|
|
39
|
+
* @description: 计算两侧逻辑行的差异并装配 hunk;相同输入不产出 hunk。
|
|
40
|
+
* @param oldLines 基线行
|
|
41
|
+
* @param newLines 当前行
|
|
42
|
+
* @returns hunk 列表与退化标记
|
|
43
|
+
*/
|
|
44
|
+
export function lineHunks(oldLines: string[], newLines: string[]): LineDiff {
|
|
45
|
+
if (sameLines(oldLines, newLines)) return { hunks: [], degraded: false }
|
|
46
|
+
|
|
47
|
+
const work: WorkState = { cells: 0, degraded: false }
|
|
48
|
+
const ops: LineOp[] = []
|
|
49
|
+
diffWindows(oldLines, 0, oldLines.length, newLines, 0, newLines.length, 0, work, ops)
|
|
50
|
+
return { hunks: buildHunks(ops), degraded: work.degraded }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @private
|
|
55
|
+
* @author ddj 2026年11月11号
|
|
56
|
+
* @description: 判断两组行是否逐行相同,避免为完全相同的文件生成整页上下文。
|
|
57
|
+
* @param oldLines 基线行
|
|
58
|
+
* @param newLines 当前行
|
|
59
|
+
* @returns 是否逐行相同
|
|
60
|
+
*/
|
|
61
|
+
function sameLines(oldLines: string[], newLines: string[]): boolean {
|
|
62
|
+
if (oldLines.length !== newLines.length) return false
|
|
63
|
+
return oldLines.every((text, index) => text === newLines[index])
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @private
|
|
68
|
+
* @author ddj 2026年11月11号
|
|
69
|
+
* @description: 先裁当前窗口的公共前后缀并发成上下文行,再让中段进入核心比较。
|
|
70
|
+
* @param oldLines 基线行
|
|
71
|
+
* @param oldStart 基线窗口起点
|
|
72
|
+
* @param oldCount 基线窗口行数
|
|
73
|
+
* @param newLines 当前行
|
|
74
|
+
* @param newStart 当前窗口起点
|
|
75
|
+
* @param newCount 当前窗口行数
|
|
76
|
+
* @param depth 递归深度
|
|
77
|
+
* @param work 共享预算
|
|
78
|
+
* @param ops 输出操作
|
|
79
|
+
* @returns 无
|
|
80
|
+
*/
|
|
81
|
+
function diffWindows(
|
|
82
|
+
oldLines: string[], oldStart: number, oldCount: number,
|
|
83
|
+
newLines: string[], newStart: number, newCount: number,
|
|
84
|
+
depth: number, work: WorkState, ops: LineOp[],
|
|
85
|
+
): void {
|
|
86
|
+
let prefix = 0
|
|
87
|
+
while (prefix < oldCount && prefix < newCount && oldLines[oldStart + prefix] === newLines[newStart + prefix]) prefix++
|
|
88
|
+
let suffix = 0
|
|
89
|
+
while (suffix < oldCount - prefix && suffix < newCount - prefix
|
|
90
|
+
&& oldLines[oldStart + oldCount - 1 - suffix] === newLines[newStart + newCount - 1 - suffix]) suffix++
|
|
91
|
+
|
|
92
|
+
for (let index = 0; index < prefix; index++) ops.push({ kind: ' ', text: oldLines[oldStart + index] })
|
|
93
|
+
diffCore(oldLines, oldStart + prefix, oldCount - prefix - suffix,
|
|
94
|
+
newLines, newStart + prefix, newCount - prefix - suffix, depth, work, ops)
|
|
95
|
+
for (let index = 0; index < suffix; index++) ops.push({ kind: ' ', text: oldLines[oldStart + oldCount - suffix + index] })
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @private
|
|
100
|
+
* @author ddj 2026年11月11号
|
|
101
|
+
* @description: 核心比较:小窗口做最优 LCS,大窗口按唯一行锚点递归切分,无锚点才整段替换。
|
|
102
|
+
* @param oldLines 基线行
|
|
103
|
+
* @param oldStart 基线窗口起点
|
|
104
|
+
* @param oldCount 基线窗口行数
|
|
105
|
+
* @param newLines 当前行
|
|
106
|
+
* @param newStart 当前窗口起点
|
|
107
|
+
* @param newCount 当前窗口行数
|
|
108
|
+
* @param depth 递归深度
|
|
109
|
+
* @param work 共享预算
|
|
110
|
+
* @param ops 输出操作
|
|
111
|
+
* @returns 无
|
|
112
|
+
*/
|
|
113
|
+
function diffCore(
|
|
114
|
+
oldLines: string[], oldStart: number, oldCount: number,
|
|
115
|
+
newLines: string[], newStart: number, newCount: number,
|
|
116
|
+
depth: number, work: WorkState, ops: LineOp[],
|
|
117
|
+
): void {
|
|
118
|
+
if (oldCount === 0) {
|
|
119
|
+
for (let index = 0; index < newCount; index++) ops.push({ kind: '+', text: newLines[newStart + index] })
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
if (newCount === 0) {
|
|
123
|
+
for (let index = 0; index < oldCount; index++) ops.push({ kind: '-', text: oldLines[oldStart + index] })
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const cells = oldCount * newCount
|
|
128
|
+
if (cells <= LCS_CELLS && work.cells + cells <= WORK_CELLS) {
|
|
129
|
+
runLcs(oldLines, oldStart, oldCount, newLines, newStart, newCount, work, ops)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
if (depth >= MAX_DEPTH || work.cells >= WORK_CELLS) {
|
|
133
|
+
appendReplace(oldLines, oldStart, oldCount, newLines, newStart, newCount, ops)
|
|
134
|
+
work.degraded = true
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const anchors = pickAnchors(oldLines, oldStart, oldCount, newLines, newStart, newCount)
|
|
139
|
+
if (anchors.length === 0) {
|
|
140
|
+
appendReplace(oldLines, oldStart, oldCount, newLines, newStart, newCount, ops)
|
|
141
|
+
work.degraded = true
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let oldOffset = 0
|
|
146
|
+
let newOffset = 0
|
|
147
|
+
for (const anchor of anchors) {
|
|
148
|
+
diffWindows(oldLines, oldStart + oldOffset, anchor.oldIndex - oldOffset,
|
|
149
|
+
newLines, newStart + newOffset, anchor.newIndex - newOffset, depth + 1, work, ops)
|
|
150
|
+
ops.push({ kind: ' ', text: oldLines[oldStart + anchor.oldIndex] })
|
|
151
|
+
oldOffset = anchor.oldIndex + 1
|
|
152
|
+
newOffset = anchor.newIndex + 1
|
|
153
|
+
}
|
|
154
|
+
diffWindows(oldLines, oldStart + oldOffset, oldCount - oldOffset,
|
|
155
|
+
newLines, newStart + newOffset, newCount - newOffset, depth + 1, work, ops)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @private
|
|
160
|
+
* @author ddj 2026年11月11号
|
|
161
|
+
* @description: 在窗口内做最优 LCS;平局优先删行,保持与历史实现一致的 hunk 形状。
|
|
162
|
+
* @param oldLines 基线行
|
|
163
|
+
* @param oldStart 基线窗口起点
|
|
164
|
+
* @param oldCount 基线窗口行数
|
|
165
|
+
* @param newLines 当前行
|
|
166
|
+
* @param newStart 当前窗口起点
|
|
167
|
+
* @param newCount 当前窗口行数
|
|
168
|
+
* @param work 共享预算
|
|
169
|
+
* @param ops 输出操作
|
|
170
|
+
* @returns 无
|
|
171
|
+
*/
|
|
172
|
+
function runLcs(
|
|
173
|
+
oldLines: string[], oldStart: number, oldCount: number,
|
|
174
|
+
newLines: string[], newStart: number, newCount: number,
|
|
175
|
+
work: WorkState, ops: LineOp[],
|
|
176
|
+
): void {
|
|
177
|
+
work.cells += oldCount * newCount
|
|
178
|
+
const cols = newCount + 1
|
|
179
|
+
const table = new Uint32Array((oldCount + 1) * cols)
|
|
180
|
+
for (let oldIndex = oldCount - 1; oldIndex >= 0; oldIndex--) {
|
|
181
|
+
const row = oldIndex * cols
|
|
182
|
+
const nextRow = row + cols
|
|
183
|
+
for (let newIndex = newCount - 1; newIndex >= 0; newIndex--) {
|
|
184
|
+
table[row + newIndex] = oldLines[oldStart + oldIndex] === newLines[newStart + newIndex]
|
|
185
|
+
? table[nextRow + newIndex + 1] + 1
|
|
186
|
+
: Math.max(table[nextRow + newIndex], table[row + newIndex + 1])
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let oldIndex = 0
|
|
191
|
+
let newIndex = 0
|
|
192
|
+
while (oldIndex < oldCount && newIndex < newCount) {
|
|
193
|
+
if (oldLines[oldStart + oldIndex] === newLines[newStart + newIndex]) {
|
|
194
|
+
ops.push({ kind: ' ', text: oldLines[oldStart + oldIndex] })
|
|
195
|
+
oldIndex++
|
|
196
|
+
newIndex++
|
|
197
|
+
continue
|
|
198
|
+
}
|
|
199
|
+
if (table[(oldIndex + 1) * cols + newIndex] >= table[oldIndex * cols + newIndex + 1]) {
|
|
200
|
+
ops.push({ kind: '-', text: oldLines[oldStart + oldIndex] })
|
|
201
|
+
oldIndex++
|
|
202
|
+
} else {
|
|
203
|
+
ops.push({ kind: '+', text: newLines[newStart + newIndex] })
|
|
204
|
+
newIndex++
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
while (oldIndex < oldCount) ops.push({ kind: '-', text: oldLines[oldStart + oldIndex++] })
|
|
208
|
+
while (newIndex < newCount) ops.push({ kind: '+', text: newLines[newStart + newIndex++] })
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @private
|
|
213
|
+
* @author ddj 2026年11月11号
|
|
214
|
+
* @description: 退化口径:整段删除再整段新增,绝不谎报逐行对应。
|
|
215
|
+
* @param oldLines 基线行
|
|
216
|
+
* @param oldStart 基线窗口起点
|
|
217
|
+
* @param oldCount 基线窗口行数
|
|
218
|
+
* @param newLines 当前行
|
|
219
|
+
* @param newStart 当前窗口起点
|
|
220
|
+
* @param newCount 当前窗口行数
|
|
221
|
+
* @param ops 输出操作
|
|
222
|
+
* @returns 无
|
|
223
|
+
*/
|
|
224
|
+
function appendReplace(
|
|
225
|
+
oldLines: string[], oldStart: number, oldCount: number,
|
|
226
|
+
newLines: string[], newStart: number, newCount: number, ops: LineOp[],
|
|
227
|
+
): void {
|
|
228
|
+
for (let index = 0; index < oldCount; index++) ops.push({ kind: '-', text: oldLines[oldStart + index] })
|
|
229
|
+
for (let index = 0; index < newCount; index++) ops.push({ kind: '+', text: newLines[newStart + index] })
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @private
|
|
234
|
+
* @author ddj 2026年11月11号
|
|
235
|
+
* @description: 取两侧都恰好出现一次的相同行做锚点,并按出现顺序取最长递增链,避免交叉配对。
|
|
236
|
+
* @param oldLines 基线行
|
|
237
|
+
* @param oldStart 基线窗口起点
|
|
238
|
+
* @param oldCount 基线窗口行数
|
|
239
|
+
* @param newLines 当前行
|
|
240
|
+
* @param newStart 当前窗口起点
|
|
241
|
+
* @param newCount 当前窗口行数
|
|
242
|
+
* @returns 顺序递增的锚点列表
|
|
243
|
+
*/
|
|
244
|
+
function pickAnchors(
|
|
245
|
+
oldLines: string[], oldStart: number, oldCount: number,
|
|
246
|
+
newLines: string[], newStart: number, newCount: number,
|
|
247
|
+
): Anchor[] {
|
|
248
|
+
const oldCounts = countRange(oldLines, oldStart, oldCount)
|
|
249
|
+
const newCounts = countRange(newLines, newStart, newCount)
|
|
250
|
+
const newIndexes = new Map<string, number>()
|
|
251
|
+
for (let index = 0; index < newCount; index++) {
|
|
252
|
+
const text = newLines[newStart + index]
|
|
253
|
+
if (newCounts.get(text) === 1) newIndexes.set(text, index)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const pairs: Anchor[] = []
|
|
257
|
+
for (let index = 0; index < oldCount; index++) {
|
|
258
|
+
const text = oldLines[oldStart + index]
|
|
259
|
+
if (oldCounts.get(text) !== 1) continue
|
|
260
|
+
const newIndex = newIndexes.get(text)
|
|
261
|
+
if (newIndex === undefined) continue
|
|
262
|
+
pairs.push({ oldIndex: index, newIndex })
|
|
263
|
+
}
|
|
264
|
+
return longestChain(pairs)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @private
|
|
269
|
+
* @author ddj 2026年11月11号
|
|
270
|
+
* @description: 统计窗口内每一行文本的出现次数。
|
|
271
|
+
* @param lines 行集合
|
|
272
|
+
* @param start 窗口起点
|
|
273
|
+
* @param count 窗口行数
|
|
274
|
+
* @returns 行文本到出现次数的映射
|
|
275
|
+
*/
|
|
276
|
+
function countRange(lines: string[], start: number, count: number): Map<string, number> {
|
|
277
|
+
const counts = new Map<string, number>()
|
|
278
|
+
for (let index = 0; index < count; index++) {
|
|
279
|
+
const text = lines[start + index]
|
|
280
|
+
counts.set(text, (counts.get(text) ?? 0) + 1)
|
|
281
|
+
}
|
|
282
|
+
return counts
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @private
|
|
287
|
+
* @author ddj 2026年11月11号
|
|
288
|
+
* @description: 按右侧下标做最长递增子序列(耐心排序 O(n log n)),保持锚点不交叉。
|
|
289
|
+
* @param pairs 已按左侧下标升序的候选锚点
|
|
290
|
+
* @returns 顺序递增的锚点子序列
|
|
291
|
+
*/
|
|
292
|
+
function longestChain(pairs: Anchor[]): Anchor[] {
|
|
293
|
+
const tails: number[] = []
|
|
294
|
+
const previous = new Array<number>(pairs.length).fill(-1)
|
|
295
|
+
for (let index = 0; index < pairs.length; index++) {
|
|
296
|
+
const value = pairs[index].newIndex
|
|
297
|
+
let left = 0
|
|
298
|
+
let right = tails.length
|
|
299
|
+
while (left < right) {
|
|
300
|
+
const middle = (left + right) >>> 1
|
|
301
|
+
if (pairs[tails[middle]].newIndex < value) left = middle + 1
|
|
302
|
+
else right = middle
|
|
303
|
+
}
|
|
304
|
+
if (left > 0) previous[index] = tails[left - 1]
|
|
305
|
+
tails[left] = index
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const ordered: Anchor[] = []
|
|
309
|
+
let cursor = tails.length ? tails[tails.length - 1] : -1
|
|
310
|
+
while (cursor >= 0) {
|
|
311
|
+
ordered.push(pairs[cursor])
|
|
312
|
+
cursor = previous[cursor]
|
|
313
|
+
}
|
|
314
|
+
return ordered.reverse()
|
|
315
|
+
}
|
|
316
|
+
// #endregion
|
|
317
|
+
|
|
318
|
+
// #region hunk 装配
|
|
319
|
+
/**
|
|
320
|
+
* @private
|
|
321
|
+
* @author ddj 2026年11月11号
|
|
322
|
+
* @description: 把操作流装配成带上下文的 hunk;变化间隔不超过 2×上下文时合并同一 hunk。
|
|
323
|
+
* 行号按已消费行数推进:head 已在主循环逐行计入,块结束只补 tail。
|
|
324
|
+
* @param ops 完整操作流
|
|
325
|
+
* @returns hunk 列表
|
|
326
|
+
*/
|
|
327
|
+
function buildHunks(ops: LineOp[]): ReviewHunk[] {
|
|
328
|
+
const hunks: ReviewHunk[] = []
|
|
329
|
+
let index = 0
|
|
330
|
+
let oldLine = 0
|
|
331
|
+
let newLine = 0
|
|
332
|
+
while (index < ops.length) {
|
|
333
|
+
if (ops[index].kind === ' ') {
|
|
334
|
+
oldLine++
|
|
335
|
+
newLine++
|
|
336
|
+
index++
|
|
337
|
+
continue
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const head = ops.slice(Math.max(0, index - CONTEXT), index)
|
|
341
|
+
const { changes, stop } = collectChanges(ops, index)
|
|
342
|
+
const tail = ops.slice(stop, stop + CONTEXT)
|
|
343
|
+
hunks.push(makeHunk([...head, ...changes, ...tail], oldLine - head.length + 1, newLine - head.length + 1))
|
|
344
|
+
|
|
345
|
+
for (const op of changes) {
|
|
346
|
+
if (op.kind !== '+') oldLine++
|
|
347
|
+
if (op.kind !== '-') newLine++
|
|
348
|
+
}
|
|
349
|
+
oldLine += tail.length
|
|
350
|
+
newLine += tail.length
|
|
351
|
+
index = stop + tail.length
|
|
352
|
+
}
|
|
353
|
+
return hunks
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @private
|
|
358
|
+
* @author ddj 2026年11月11号
|
|
359
|
+
* @description: 收集从首个变化开始的连续操作,直到遇到足够长的纯上下文段才停止。
|
|
360
|
+
* @param ops 完整操作流
|
|
361
|
+
* @param start 首个变化的位置
|
|
362
|
+
* @returns 变化段操作与停止位置
|
|
363
|
+
*/
|
|
364
|
+
function collectChanges(ops: LineOp[], start: number): { changes: LineOp[]; stop: number } {
|
|
365
|
+
const changes: LineOp[] = []
|
|
366
|
+
let probe = start
|
|
367
|
+
while (probe < ops.length) {
|
|
368
|
+
const op = ops[probe]
|
|
369
|
+
if (op.kind !== ' ') {
|
|
370
|
+
changes.push(op)
|
|
371
|
+
probe++
|
|
372
|
+
continue
|
|
373
|
+
}
|
|
374
|
+
if (!hasChange(ops, probe)) break
|
|
375
|
+
changes.push(op)
|
|
376
|
+
probe++
|
|
377
|
+
}
|
|
378
|
+
return { changes, stop: probe }
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @private
|
|
383
|
+
* @author ddj 2026年11月11号
|
|
384
|
+
* @description: 判断上下文窗口内是否还有变化,用于决定是否把两处改动并进同一 hunk。
|
|
385
|
+
* @param ops 完整操作流
|
|
386
|
+
* @param at 待检查上下文段起点
|
|
387
|
+
* @returns 窗口内是否含变化
|
|
388
|
+
*/
|
|
389
|
+
function hasChange(ops: LineOp[], at: number): boolean {
|
|
390
|
+
return ops.slice(at, at + CONTEXT * 2 + 1).some((op) => op.kind !== ' ')
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* @private
|
|
395
|
+
* @author ddj 2026年11月11号
|
|
396
|
+
* @description: 由一段操作装配 hunk,并统计两侧行数。
|
|
397
|
+
* @param ops 该 hunk 的操作
|
|
398
|
+
* @param oldStart 基线起始行(1 基)
|
|
399
|
+
* @param newStart 当前起始行(1 基)
|
|
400
|
+
* @returns hunk
|
|
401
|
+
*/
|
|
402
|
+
function makeHunk(ops: LineOp[], oldStart: number, newStart: number): ReviewHunk {
|
|
403
|
+
let oldLines = 0
|
|
404
|
+
let newLines = 0
|
|
405
|
+
const lines: string[] = []
|
|
406
|
+
for (const op of ops) {
|
|
407
|
+
if (op.kind !== '+') oldLines++
|
|
408
|
+
if (op.kind !== '-') newLines++
|
|
409
|
+
lines.push(op.kind + op.text)
|
|
410
|
+
}
|
|
411
|
+
return { oldStart, oldLines, newStart, newLines, lines }
|
|
412
|
+
}
|
|
413
|
+
// #endregion
|
package/src/shared/review.ts
CHANGED
|
@@ -14,6 +14,11 @@ export interface ReviewHunk {
|
|
|
14
14
|
newLines: number
|
|
15
15
|
lines: string[]
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* @author ddj 2026年11月11号
|
|
19
|
+
* @description: 文本差异;coarse 表示比较中有区域退化为整段替换——官方来源是宿主比较超时,
|
|
20
|
+
* 待审来源是行差异引擎在无唯一行锚点时退化。为 true 时不作块级 Undo 依据。
|
|
21
|
+
*/
|
|
17
22
|
export type OfficialDiff = { kind: 'text'; path: string; display: string; before: boolean; after: boolean; hunks: ReviewHunk[]; coarse: boolean }
|
|
18
23
|
| { kind: 'binary' | 'oversized'; path: string; display: string }
|
|
19
24
|
export interface OfficialSummary {
|