dsh-vscode-mode 0.29.0 → 0.29.2
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 +27 -4
- package/lib/client.js +79 -38
- package/lib/client.js.map +1 -1
- package/lib/index.js +402 -182
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client/styles/mcp.css +55 -14
- package/src/client/ui/AiSettings.ts +7 -5
- package/src/client/ui/IntegrationSettings.ts +8 -6
- package/src/client/ui/LspSettings.ts +2 -2
- package/src/client/ui/McpSettings.ts +26 -27
- package/src/client/ui/PerfSettings.ts +1 -1
- package/src/client/ui/SettingsUi.ts +44 -0
- package/src/client-primitives.d.ts +5 -0
- package/src/pending/hunks.ts +6 -150
- package/src/pending/lineDiff.ts +413 -0
- package/src/shared/review.ts +5 -0
package/lib/index.js
CHANGED
|
@@ -7099,7 +7099,7 @@ function rememberColdCwd(sessionId, cwd) {
|
|
|
7099
7099
|
/** dsh-remote-ssh 工作区标记文件名。 */
|
|
7100
7100
|
const MARKER = ".remote-ssh.json";
|
|
7101
7101
|
/** 向上探测的最大层数(含 cwd 自身;与上游 findMirrorRoot 的 8 层有界一致)。 */
|
|
7102
|
-
const MAX_DEPTH = 8;
|
|
7102
|
+
const MAX_DEPTH$1 = 8;
|
|
7103
7103
|
/** 探测结果 TTL(edrv.list 为 5s 轮询,避免每轮都做文件 IO)。 */
|
|
7104
7104
|
const TTL_MS = 6e4;
|
|
7105
7105
|
/** cwd → { at, ws } 探测缓存(含阴性结果)。 */
|
|
@@ -7137,7 +7137,7 @@ function markerWsOf(root, raw) {
|
|
|
7137
7137
|
*/
|
|
7138
7138
|
function walkRemoteWs(cwd) {
|
|
7139
7139
|
let dir = cwd;
|
|
7140
|
-
for (let i = 0; i < MAX_DEPTH; i++) {
|
|
7140
|
+
for (let i = 0; i < MAX_DEPTH$1; i++) {
|
|
7141
7141
|
try {
|
|
7142
7142
|
const ws = markerWsOf(dir, readFileSync(join(dir, MARKER), "utf8"));
|
|
7143
7143
|
if (ws) return ws;
|
|
@@ -11073,11 +11073,405 @@ function decodeXml(value) {
|
|
|
11073
11073
|
return value.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"").replace(/'/g, "'").replace(/&/g, "&");
|
|
11074
11074
|
}
|
|
11075
11075
|
//#endregion
|
|
11076
|
-
//#region src/pending/
|
|
11076
|
+
//#region src/pending/lineDiff.ts
|
|
11077
|
+
/** 单窗口 LCS 单元格上限:超过先找锚点;4 MB 表实测约 7 ms。 */
|
|
11078
|
+
const LCS_CELLS = 1e6;
|
|
11079
|
+
/** 一次差异的 LCS 总预算:防止病态输入拖慢 Host,约 80 ms 量级封顶。 */
|
|
11080
|
+
const WORK_CELLS = 12e6;
|
|
11081
|
+
/** 锚点递归深度上限:超过即把该窗口按整段替换处理。 */
|
|
11082
|
+
const MAX_DEPTH = 64;
|
|
11077
11083
|
/** 每个 hunk 的上下文行数。 */
|
|
11078
11084
|
const CONTEXT = 3;
|
|
11079
|
-
/**
|
|
11080
|
-
|
|
11085
|
+
/**
|
|
11086
|
+
* @public
|
|
11087
|
+
* @author ddj 2026年11月11号
|
|
11088
|
+
* @description: 计算两侧逻辑行的差异并装配 hunk;相同输入不产出 hunk。
|
|
11089
|
+
* @param oldLines 基线行
|
|
11090
|
+
* @param newLines 当前行
|
|
11091
|
+
* @returns hunk 列表与退化标记
|
|
11092
|
+
*/
|
|
11093
|
+
function lineHunks(oldLines, newLines) {
|
|
11094
|
+
if (sameLines(oldLines, newLines)) return {
|
|
11095
|
+
hunks: [],
|
|
11096
|
+
degraded: false
|
|
11097
|
+
};
|
|
11098
|
+
const work = {
|
|
11099
|
+
cells: 0,
|
|
11100
|
+
degraded: false
|
|
11101
|
+
};
|
|
11102
|
+
const ops = [];
|
|
11103
|
+
diffWindows(oldLines, 0, oldLines.length, newLines, 0, newLines.length, 0, work, ops);
|
|
11104
|
+
return {
|
|
11105
|
+
hunks: buildHunks(ops),
|
|
11106
|
+
degraded: work.degraded
|
|
11107
|
+
};
|
|
11108
|
+
}
|
|
11109
|
+
/**
|
|
11110
|
+
* @private
|
|
11111
|
+
* @author ddj 2026年11月11号
|
|
11112
|
+
* @description: 判断两组行是否逐行相同,避免为完全相同的文件生成整页上下文。
|
|
11113
|
+
* @param oldLines 基线行
|
|
11114
|
+
* @param newLines 当前行
|
|
11115
|
+
* @returns 是否逐行相同
|
|
11116
|
+
*/
|
|
11117
|
+
function sameLines(oldLines, newLines) {
|
|
11118
|
+
if (oldLines.length !== newLines.length) return false;
|
|
11119
|
+
return oldLines.every((text, index) => text === newLines[index]);
|
|
11120
|
+
}
|
|
11121
|
+
/**
|
|
11122
|
+
* @private
|
|
11123
|
+
* @author ddj 2026年11月11号
|
|
11124
|
+
* @description: 先裁当前窗口的公共前后缀并发成上下文行,再让中段进入核心比较。
|
|
11125
|
+
* @param oldLines 基线行
|
|
11126
|
+
* @param oldStart 基线窗口起点
|
|
11127
|
+
* @param oldCount 基线窗口行数
|
|
11128
|
+
* @param newLines 当前行
|
|
11129
|
+
* @param newStart 当前窗口起点
|
|
11130
|
+
* @param newCount 当前窗口行数
|
|
11131
|
+
* @param depth 递归深度
|
|
11132
|
+
* @param work 共享预算
|
|
11133
|
+
* @param ops 输出操作
|
|
11134
|
+
* @returns 无
|
|
11135
|
+
*/
|
|
11136
|
+
function diffWindows(oldLines, oldStart, oldCount, newLines, newStart, newCount, depth, work, ops) {
|
|
11137
|
+
let prefix = 0;
|
|
11138
|
+
while (prefix < oldCount && prefix < newCount && oldLines[oldStart + prefix] === newLines[newStart + prefix]) prefix++;
|
|
11139
|
+
let suffix = 0;
|
|
11140
|
+
while (suffix < oldCount - prefix && suffix < newCount - prefix && oldLines[oldStart + oldCount - 1 - suffix] === newLines[newStart + newCount - 1 - suffix]) suffix++;
|
|
11141
|
+
for (let index = 0; index < prefix; index++) ops.push({
|
|
11142
|
+
kind: " ",
|
|
11143
|
+
text: oldLines[oldStart + index]
|
|
11144
|
+
});
|
|
11145
|
+
diffCore(oldLines, oldStart + prefix, oldCount - prefix - suffix, newLines, newStart + prefix, newCount - prefix - suffix, depth, work, ops);
|
|
11146
|
+
for (let index = 0; index < suffix; index++) ops.push({
|
|
11147
|
+
kind: " ",
|
|
11148
|
+
text: oldLines[oldStart + oldCount - suffix + index]
|
|
11149
|
+
});
|
|
11150
|
+
}
|
|
11151
|
+
/**
|
|
11152
|
+
* @private
|
|
11153
|
+
* @author ddj 2026年11月11号
|
|
11154
|
+
* @description: 核心比较:小窗口做最优 LCS,大窗口按唯一行锚点递归切分,无锚点才整段替换。
|
|
11155
|
+
* @param oldLines 基线行
|
|
11156
|
+
* @param oldStart 基线窗口起点
|
|
11157
|
+
* @param oldCount 基线窗口行数
|
|
11158
|
+
* @param newLines 当前行
|
|
11159
|
+
* @param newStart 当前窗口起点
|
|
11160
|
+
* @param newCount 当前窗口行数
|
|
11161
|
+
* @param depth 递归深度
|
|
11162
|
+
* @param work 共享预算
|
|
11163
|
+
* @param ops 输出操作
|
|
11164
|
+
* @returns 无
|
|
11165
|
+
*/
|
|
11166
|
+
function diffCore(oldLines, oldStart, oldCount, newLines, newStart, newCount, depth, work, ops) {
|
|
11167
|
+
if (oldCount === 0) {
|
|
11168
|
+
for (let index = 0; index < newCount; index++) ops.push({
|
|
11169
|
+
kind: "+",
|
|
11170
|
+
text: newLines[newStart + index]
|
|
11171
|
+
});
|
|
11172
|
+
return;
|
|
11173
|
+
}
|
|
11174
|
+
if (newCount === 0) {
|
|
11175
|
+
for (let index = 0; index < oldCount; index++) ops.push({
|
|
11176
|
+
kind: "-",
|
|
11177
|
+
text: oldLines[oldStart + index]
|
|
11178
|
+
});
|
|
11179
|
+
return;
|
|
11180
|
+
}
|
|
11181
|
+
const cells = oldCount * newCount;
|
|
11182
|
+
if (cells <= LCS_CELLS && work.cells + cells <= WORK_CELLS) {
|
|
11183
|
+
runLcs(oldLines, oldStart, oldCount, newLines, newStart, newCount, work, ops);
|
|
11184
|
+
return;
|
|
11185
|
+
}
|
|
11186
|
+
if (depth >= MAX_DEPTH || work.cells >= WORK_CELLS) {
|
|
11187
|
+
appendReplace(oldLines, oldStart, oldCount, newLines, newStart, newCount, ops);
|
|
11188
|
+
work.degraded = true;
|
|
11189
|
+
return;
|
|
11190
|
+
}
|
|
11191
|
+
const anchors = pickAnchors(oldLines, oldStart, oldCount, newLines, newStart, newCount);
|
|
11192
|
+
if (anchors.length === 0) {
|
|
11193
|
+
appendReplace(oldLines, oldStart, oldCount, newLines, newStart, newCount, ops);
|
|
11194
|
+
work.degraded = true;
|
|
11195
|
+
return;
|
|
11196
|
+
}
|
|
11197
|
+
let oldOffset = 0;
|
|
11198
|
+
let newOffset = 0;
|
|
11199
|
+
for (const anchor of anchors) {
|
|
11200
|
+
diffWindows(oldLines, oldStart + oldOffset, anchor.oldIndex - oldOffset, newLines, newStart + newOffset, anchor.newIndex - newOffset, depth + 1, work, ops);
|
|
11201
|
+
ops.push({
|
|
11202
|
+
kind: " ",
|
|
11203
|
+
text: oldLines[oldStart + anchor.oldIndex]
|
|
11204
|
+
});
|
|
11205
|
+
oldOffset = anchor.oldIndex + 1;
|
|
11206
|
+
newOffset = anchor.newIndex + 1;
|
|
11207
|
+
}
|
|
11208
|
+
diffWindows(oldLines, oldStart + oldOffset, oldCount - oldOffset, newLines, newStart + newOffset, newCount - newOffset, depth + 1, work, ops);
|
|
11209
|
+
}
|
|
11210
|
+
/**
|
|
11211
|
+
* @private
|
|
11212
|
+
* @author ddj 2026年11月11号
|
|
11213
|
+
* @description: 在窗口内做最优 LCS;平局优先删行,保持与历史实现一致的 hunk 形状。
|
|
11214
|
+
* @param oldLines 基线行
|
|
11215
|
+
* @param oldStart 基线窗口起点
|
|
11216
|
+
* @param oldCount 基线窗口行数
|
|
11217
|
+
* @param newLines 当前行
|
|
11218
|
+
* @param newStart 当前窗口起点
|
|
11219
|
+
* @param newCount 当前窗口行数
|
|
11220
|
+
* @param work 共享预算
|
|
11221
|
+
* @param ops 输出操作
|
|
11222
|
+
* @returns 无
|
|
11223
|
+
*/
|
|
11224
|
+
function runLcs(oldLines, oldStart, oldCount, newLines, newStart, newCount, work, ops) {
|
|
11225
|
+
work.cells += oldCount * newCount;
|
|
11226
|
+
const cols = newCount + 1;
|
|
11227
|
+
const table = new Uint32Array((oldCount + 1) * cols);
|
|
11228
|
+
for (let oldIndex = oldCount - 1; oldIndex >= 0; oldIndex--) {
|
|
11229
|
+
const row = oldIndex * cols;
|
|
11230
|
+
const nextRow = row + cols;
|
|
11231
|
+
for (let newIndex = newCount - 1; newIndex >= 0; newIndex--) table[row + newIndex] = oldLines[oldStart + oldIndex] === newLines[newStart + newIndex] ? table[nextRow + newIndex + 1] + 1 : Math.max(table[nextRow + newIndex], table[row + newIndex + 1]);
|
|
11232
|
+
}
|
|
11233
|
+
let oldIndex = 0;
|
|
11234
|
+
let newIndex = 0;
|
|
11235
|
+
while (oldIndex < oldCount && newIndex < newCount) {
|
|
11236
|
+
if (oldLines[oldStart + oldIndex] === newLines[newStart + newIndex]) {
|
|
11237
|
+
ops.push({
|
|
11238
|
+
kind: " ",
|
|
11239
|
+
text: oldLines[oldStart + oldIndex]
|
|
11240
|
+
});
|
|
11241
|
+
oldIndex++;
|
|
11242
|
+
newIndex++;
|
|
11243
|
+
continue;
|
|
11244
|
+
}
|
|
11245
|
+
if (table[(oldIndex + 1) * cols + newIndex] >= table[oldIndex * cols + newIndex + 1]) {
|
|
11246
|
+
ops.push({
|
|
11247
|
+
kind: "-",
|
|
11248
|
+
text: oldLines[oldStart + oldIndex]
|
|
11249
|
+
});
|
|
11250
|
+
oldIndex++;
|
|
11251
|
+
} else {
|
|
11252
|
+
ops.push({
|
|
11253
|
+
kind: "+",
|
|
11254
|
+
text: newLines[newStart + newIndex]
|
|
11255
|
+
});
|
|
11256
|
+
newIndex++;
|
|
11257
|
+
}
|
|
11258
|
+
}
|
|
11259
|
+
while (oldIndex < oldCount) ops.push({
|
|
11260
|
+
kind: "-",
|
|
11261
|
+
text: oldLines[oldStart + oldIndex++]
|
|
11262
|
+
});
|
|
11263
|
+
while (newIndex < newCount) ops.push({
|
|
11264
|
+
kind: "+",
|
|
11265
|
+
text: newLines[newStart + newIndex++]
|
|
11266
|
+
});
|
|
11267
|
+
}
|
|
11268
|
+
/**
|
|
11269
|
+
* @private
|
|
11270
|
+
* @author ddj 2026年11月11号
|
|
11271
|
+
* @description: 退化口径:整段删除再整段新增,绝不谎报逐行对应。
|
|
11272
|
+
* @param oldLines 基线行
|
|
11273
|
+
* @param oldStart 基线窗口起点
|
|
11274
|
+
* @param oldCount 基线窗口行数
|
|
11275
|
+
* @param newLines 当前行
|
|
11276
|
+
* @param newStart 当前窗口起点
|
|
11277
|
+
* @param newCount 当前窗口行数
|
|
11278
|
+
* @param ops 输出操作
|
|
11279
|
+
* @returns 无
|
|
11280
|
+
*/
|
|
11281
|
+
function appendReplace(oldLines, oldStart, oldCount, newLines, newStart, newCount, ops) {
|
|
11282
|
+
for (let index = 0; index < oldCount; index++) ops.push({
|
|
11283
|
+
kind: "-",
|
|
11284
|
+
text: oldLines[oldStart + index]
|
|
11285
|
+
});
|
|
11286
|
+
for (let index = 0; index < newCount; index++) ops.push({
|
|
11287
|
+
kind: "+",
|
|
11288
|
+
text: newLines[newStart + index]
|
|
11289
|
+
});
|
|
11290
|
+
}
|
|
11291
|
+
/**
|
|
11292
|
+
* @private
|
|
11293
|
+
* @author ddj 2026年11月11号
|
|
11294
|
+
* @description: 取两侧都恰好出现一次的相同行做锚点,并按出现顺序取最长递增链,避免交叉配对。
|
|
11295
|
+
* @param oldLines 基线行
|
|
11296
|
+
* @param oldStart 基线窗口起点
|
|
11297
|
+
* @param oldCount 基线窗口行数
|
|
11298
|
+
* @param newLines 当前行
|
|
11299
|
+
* @param newStart 当前窗口起点
|
|
11300
|
+
* @param newCount 当前窗口行数
|
|
11301
|
+
* @returns 顺序递增的锚点列表
|
|
11302
|
+
*/
|
|
11303
|
+
function pickAnchors(oldLines, oldStart, oldCount, newLines, newStart, newCount) {
|
|
11304
|
+
const oldCounts = countRange(oldLines, oldStart, oldCount);
|
|
11305
|
+
const newCounts = countRange(newLines, newStart, newCount);
|
|
11306
|
+
const newIndexes = /* @__PURE__ */ new Map();
|
|
11307
|
+
for (let index = 0; index < newCount; index++) {
|
|
11308
|
+
const text = newLines[newStart + index];
|
|
11309
|
+
if (newCounts.get(text) === 1) newIndexes.set(text, index);
|
|
11310
|
+
}
|
|
11311
|
+
const pairs = [];
|
|
11312
|
+
for (let index = 0; index < oldCount; index++) {
|
|
11313
|
+
const text = oldLines[oldStart + index];
|
|
11314
|
+
if (oldCounts.get(text) !== 1) continue;
|
|
11315
|
+
const newIndex = newIndexes.get(text);
|
|
11316
|
+
if (newIndex === void 0) continue;
|
|
11317
|
+
pairs.push({
|
|
11318
|
+
oldIndex: index,
|
|
11319
|
+
newIndex
|
|
11320
|
+
});
|
|
11321
|
+
}
|
|
11322
|
+
return longestChain(pairs);
|
|
11323
|
+
}
|
|
11324
|
+
/**
|
|
11325
|
+
* @private
|
|
11326
|
+
* @author ddj 2026年11月11号
|
|
11327
|
+
* @description: 统计窗口内每一行文本的出现次数。
|
|
11328
|
+
* @param lines 行集合
|
|
11329
|
+
* @param start 窗口起点
|
|
11330
|
+
* @param count 窗口行数
|
|
11331
|
+
* @returns 行文本到出现次数的映射
|
|
11332
|
+
*/
|
|
11333
|
+
function countRange(lines, start, count) {
|
|
11334
|
+
const counts = /* @__PURE__ */ new Map();
|
|
11335
|
+
for (let index = 0; index < count; index++) {
|
|
11336
|
+
const text = lines[start + index];
|
|
11337
|
+
counts.set(text, (counts.get(text) ?? 0) + 1);
|
|
11338
|
+
}
|
|
11339
|
+
return counts;
|
|
11340
|
+
}
|
|
11341
|
+
/**
|
|
11342
|
+
* @private
|
|
11343
|
+
* @author ddj 2026年11月11号
|
|
11344
|
+
* @description: 按右侧下标做最长递增子序列(耐心排序 O(n log n)),保持锚点不交叉。
|
|
11345
|
+
* @param pairs 已按左侧下标升序的候选锚点
|
|
11346
|
+
* @returns 顺序递增的锚点子序列
|
|
11347
|
+
*/
|
|
11348
|
+
function longestChain(pairs) {
|
|
11349
|
+
const tails = [];
|
|
11350
|
+
const previous = new Array(pairs.length).fill(-1);
|
|
11351
|
+
for (let index = 0; index < pairs.length; index++) {
|
|
11352
|
+
const value = pairs[index].newIndex;
|
|
11353
|
+
let left = 0;
|
|
11354
|
+
let right = tails.length;
|
|
11355
|
+
while (left < right) {
|
|
11356
|
+
const middle = left + right >>> 1;
|
|
11357
|
+
if (pairs[tails[middle]].newIndex < value) left = middle + 1;
|
|
11358
|
+
else right = middle;
|
|
11359
|
+
}
|
|
11360
|
+
if (left > 0) previous[index] = tails[left - 1];
|
|
11361
|
+
tails[left] = index;
|
|
11362
|
+
}
|
|
11363
|
+
const ordered = [];
|
|
11364
|
+
let cursor = tails.length ? tails[tails.length - 1] : -1;
|
|
11365
|
+
while (cursor >= 0) {
|
|
11366
|
+
ordered.push(pairs[cursor]);
|
|
11367
|
+
cursor = previous[cursor];
|
|
11368
|
+
}
|
|
11369
|
+
return ordered.reverse();
|
|
11370
|
+
}
|
|
11371
|
+
/**
|
|
11372
|
+
* @private
|
|
11373
|
+
* @author ddj 2026年11月11号
|
|
11374
|
+
* @description: 把操作流装配成带上下文的 hunk;变化间隔不超过 2×上下文时合并同一 hunk。
|
|
11375
|
+
* 行号按已消费行数推进:head 已在主循环逐行计入,块结束只补 tail。
|
|
11376
|
+
* @param ops 完整操作流
|
|
11377
|
+
* @returns hunk 列表
|
|
11378
|
+
*/
|
|
11379
|
+
function buildHunks(ops) {
|
|
11380
|
+
const hunks = [];
|
|
11381
|
+
let index = 0;
|
|
11382
|
+
let oldLine = 0;
|
|
11383
|
+
let newLine = 0;
|
|
11384
|
+
while (index < ops.length) {
|
|
11385
|
+
if (ops[index].kind === " ") {
|
|
11386
|
+
oldLine++;
|
|
11387
|
+
newLine++;
|
|
11388
|
+
index++;
|
|
11389
|
+
continue;
|
|
11390
|
+
}
|
|
11391
|
+
const head = ops.slice(Math.max(0, index - CONTEXT), index);
|
|
11392
|
+
const { changes, stop } = collectChanges(ops, index);
|
|
11393
|
+
const tail = ops.slice(stop, stop + CONTEXT);
|
|
11394
|
+
hunks.push(makeHunk([
|
|
11395
|
+
...head,
|
|
11396
|
+
...changes,
|
|
11397
|
+
...tail
|
|
11398
|
+
], oldLine - head.length + 1, newLine - head.length + 1));
|
|
11399
|
+
for (const op of changes) {
|
|
11400
|
+
if (op.kind !== "+") oldLine++;
|
|
11401
|
+
if (op.kind !== "-") newLine++;
|
|
11402
|
+
}
|
|
11403
|
+
oldLine += tail.length;
|
|
11404
|
+
newLine += tail.length;
|
|
11405
|
+
index = stop + tail.length;
|
|
11406
|
+
}
|
|
11407
|
+
return hunks;
|
|
11408
|
+
}
|
|
11409
|
+
/**
|
|
11410
|
+
* @private
|
|
11411
|
+
* @author ddj 2026年11月11号
|
|
11412
|
+
* @description: 收集从首个变化开始的连续操作,直到遇到足够长的纯上下文段才停止。
|
|
11413
|
+
* @param ops 完整操作流
|
|
11414
|
+
* @param start 首个变化的位置
|
|
11415
|
+
* @returns 变化段操作与停止位置
|
|
11416
|
+
*/
|
|
11417
|
+
function collectChanges(ops, start) {
|
|
11418
|
+
const changes = [];
|
|
11419
|
+
let probe = start;
|
|
11420
|
+
while (probe < ops.length) {
|
|
11421
|
+
const op = ops[probe];
|
|
11422
|
+
if (op.kind !== " ") {
|
|
11423
|
+
changes.push(op);
|
|
11424
|
+
probe++;
|
|
11425
|
+
continue;
|
|
11426
|
+
}
|
|
11427
|
+
if (!hasChange(ops, probe)) break;
|
|
11428
|
+
changes.push(op);
|
|
11429
|
+
probe++;
|
|
11430
|
+
}
|
|
11431
|
+
return {
|
|
11432
|
+
changes,
|
|
11433
|
+
stop: probe
|
|
11434
|
+
};
|
|
11435
|
+
}
|
|
11436
|
+
/**
|
|
11437
|
+
* @private
|
|
11438
|
+
* @author ddj 2026年11月11号
|
|
11439
|
+
* @description: 判断上下文窗口内是否还有变化,用于决定是否把两处改动并进同一 hunk。
|
|
11440
|
+
* @param ops 完整操作流
|
|
11441
|
+
* @param at 待检查上下文段起点
|
|
11442
|
+
* @returns 窗口内是否含变化
|
|
11443
|
+
*/
|
|
11444
|
+
function hasChange(ops, at) {
|
|
11445
|
+
return ops.slice(at, at + 6 + 1).some((op) => op.kind !== " ");
|
|
11446
|
+
}
|
|
11447
|
+
/**
|
|
11448
|
+
* @private
|
|
11449
|
+
* @author ddj 2026年11月11号
|
|
11450
|
+
* @description: 由一段操作装配 hunk,并统计两侧行数。
|
|
11451
|
+
* @param ops 该 hunk 的操作
|
|
11452
|
+
* @param oldStart 基线起始行(1 基)
|
|
11453
|
+
* @param newStart 当前起始行(1 基)
|
|
11454
|
+
* @returns hunk
|
|
11455
|
+
*/
|
|
11456
|
+
function makeHunk(ops, oldStart, newStart) {
|
|
11457
|
+
let oldLines = 0;
|
|
11458
|
+
let newLines = 0;
|
|
11459
|
+
const lines = [];
|
|
11460
|
+
for (const op of ops) {
|
|
11461
|
+
if (op.kind !== "+") oldLines++;
|
|
11462
|
+
if (op.kind !== "-") newLines++;
|
|
11463
|
+
lines.push(op.kind + op.text);
|
|
11464
|
+
}
|
|
11465
|
+
return {
|
|
11466
|
+
oldStart,
|
|
11467
|
+
oldLines,
|
|
11468
|
+
newStart,
|
|
11469
|
+
newLines,
|
|
11470
|
+
lines
|
|
11471
|
+
};
|
|
11472
|
+
}
|
|
11473
|
+
//#endregion
|
|
11474
|
+
//#region src/pending/hunks.ts
|
|
11081
11475
|
/**
|
|
11082
11476
|
* @author ddj 2026年10月29号
|
|
11083
11477
|
* @description: 构造可直接交给官方渲染器的差异;两侧完全相同时返回空 hunk 列表。
|
|
@@ -11085,10 +11479,10 @@ const CELL_LIMIT = 1e6;
|
|
|
11085
11479
|
* @param display 展示名
|
|
11086
11480
|
* @param oldText 基线全文
|
|
11087
11481
|
* @param newText 当前全文
|
|
11088
|
-
* @returns
|
|
11482
|
+
* @returns 文本差异;coarse 表示比较中确有区域退化为整段替换
|
|
11089
11483
|
*/
|
|
11090
11484
|
function textDiff(path, display, oldText, newText) {
|
|
11091
|
-
const hunks =
|
|
11485
|
+
const { hunks, degraded } = lineHunks(logicalLines(oldText), logicalLines(newText));
|
|
11092
11486
|
return {
|
|
11093
11487
|
kind: "text",
|
|
11094
11488
|
path,
|
|
@@ -11096,7 +11490,7 @@ function textDiff(path, display, oldText, newText) {
|
|
|
11096
11490
|
before: true,
|
|
11097
11491
|
after: true,
|
|
11098
11492
|
hunks,
|
|
11099
|
-
coarse:
|
|
11493
|
+
coarse: degraded
|
|
11100
11494
|
};
|
|
11101
11495
|
}
|
|
11102
11496
|
/**
|
|
@@ -11173,180 +11567,6 @@ function locateSpan(lines, pattern) {
|
|
|
11173
11567
|
}
|
|
11174
11568
|
return at >= 0 ? at : null;
|
|
11175
11569
|
}
|
|
11176
|
-
/**
|
|
11177
|
-
* @author ddj 2026年10月29号
|
|
11178
|
-
* @description: 生成带上下文的标准 hunk 列表。
|
|
11179
|
-
* @param oldLines 基线行
|
|
11180
|
-
* @param newLines 当前行
|
|
11181
|
-
* @returns hunk 列表
|
|
11182
|
-
*/
|
|
11183
|
-
function buildHunks(oldLines, newLines) {
|
|
11184
|
-
const ops = diffLines(oldLines, newLines);
|
|
11185
|
-
const hunks = [];
|
|
11186
|
-
let index = 0;
|
|
11187
|
-
let oldLine = 0;
|
|
11188
|
-
let newLine = 0;
|
|
11189
|
-
while (index < ops.length) {
|
|
11190
|
-
if (ops[index].kind === " ") {
|
|
11191
|
-
oldLine++;
|
|
11192
|
-
newLine++;
|
|
11193
|
-
index++;
|
|
11194
|
-
continue;
|
|
11195
|
-
}
|
|
11196
|
-
const head = ops.slice(Math.max(0, index - CONTEXT), index);
|
|
11197
|
-
const collected = [];
|
|
11198
|
-
let probe = index;
|
|
11199
|
-
while (probe < ops.length) {
|
|
11200
|
-
const op = ops[probe];
|
|
11201
|
-
if (op.kind !== " ") {
|
|
11202
|
-
collected.push(op);
|
|
11203
|
-
probe++;
|
|
11204
|
-
continue;
|
|
11205
|
-
}
|
|
11206
|
-
if (ops.slice(probe, probe + 6 + 1).some((item) => item.kind !== " ")) {
|
|
11207
|
-
collected.push(op);
|
|
11208
|
-
probe++;
|
|
11209
|
-
continue;
|
|
11210
|
-
}
|
|
11211
|
-
break;
|
|
11212
|
-
}
|
|
11213
|
-
const tail = ops.slice(probe, probe + CONTEXT);
|
|
11214
|
-
const oldStartLine = oldLine - head.length + 1;
|
|
11215
|
-
const newStartLine = newLine - head.length + 1;
|
|
11216
|
-
hunks.push(makeHunk([
|
|
11217
|
-
...head,
|
|
11218
|
-
...collected,
|
|
11219
|
-
...tail
|
|
11220
|
-
], oldStartLine, newStartLine));
|
|
11221
|
-
for (const op of collected) {
|
|
11222
|
-
if (op.kind !== "+") oldLine++;
|
|
11223
|
-
if (op.kind !== "-") newLine++;
|
|
11224
|
-
}
|
|
11225
|
-
oldLine += head.length + tail.length;
|
|
11226
|
-
newLine += head.length + tail.length;
|
|
11227
|
-
index = probe + tail.length;
|
|
11228
|
-
}
|
|
11229
|
-
return hunks;
|
|
11230
|
-
}
|
|
11231
|
-
/**
|
|
11232
|
-
* @author ddj 2026年10月29号
|
|
11233
|
-
* @description: 由一段操作装配 hunk。
|
|
11234
|
-
* @param ops 该 hunk 的操作
|
|
11235
|
-
* @param oldStart 基线起始行
|
|
11236
|
-
* @param newStart 当前起始行
|
|
11237
|
-
* @returns hunk
|
|
11238
|
-
*/
|
|
11239
|
-
function makeHunk(ops, oldStart, newStart) {
|
|
11240
|
-
return {
|
|
11241
|
-
oldStart,
|
|
11242
|
-
oldLines: ops.filter((op) => op.kind !== "+").length,
|
|
11243
|
-
newStart,
|
|
11244
|
-
newLines: ops.filter((op) => op.kind !== "-").length,
|
|
11245
|
-
lines: ops.map((op) => op.kind + op.text)
|
|
11246
|
-
};
|
|
11247
|
-
}
|
|
11248
|
-
/**
|
|
11249
|
-
* @author ddj 2026年10月29号
|
|
11250
|
-
* @description: 行级差异:先剪公共前后缀,剩余部分在限额内做 LCS,超限按整段替换。
|
|
11251
|
-
* @param oldLines 基线行
|
|
11252
|
-
* @param newLines 当前行
|
|
11253
|
-
* @returns 顺序完整的操作列表
|
|
11254
|
-
*/
|
|
11255
|
-
function diffLines(oldLines, newLines) {
|
|
11256
|
-
let head = 0;
|
|
11257
|
-
while (head < oldLines.length && head < newLines.length && oldLines[head] === newLines[head]) head++;
|
|
11258
|
-
let tail = 0;
|
|
11259
|
-
while (tail < oldLines.length - head && tail < newLines.length - head && oldLines[oldLines.length - 1 - tail] === newLines[newLines.length - 1 - tail]) tail++;
|
|
11260
|
-
const left = oldLines.slice(head, oldLines.length - tail);
|
|
11261
|
-
const right = newLines.slice(head, newLines.length - tail);
|
|
11262
|
-
const middle = left.length * right.length > CELL_LIMIT ? coarseOps(left, right) : lcsOps(left, right);
|
|
11263
|
-
return [
|
|
11264
|
-
...oldLines.slice(0, head).map((text) => ({
|
|
11265
|
-
kind: " ",
|
|
11266
|
-
text
|
|
11267
|
-
})),
|
|
11268
|
-
...middle,
|
|
11269
|
-
...oldLines.slice(oldLines.length - tail).map((text) => ({
|
|
11270
|
-
kind: " ",
|
|
11271
|
-
text
|
|
11272
|
-
}))
|
|
11273
|
-
];
|
|
11274
|
-
}
|
|
11275
|
-
/**
|
|
11276
|
-
* @author ddj 2026年10月29号
|
|
11277
|
-
* @description: 超限时的退化口径:整段删除再整段新增,绝不谎报逐行对应。
|
|
11278
|
-
* @param left 基线剩余行
|
|
11279
|
-
* @param right 当前剩余行
|
|
11280
|
-
* @returns 操作列表
|
|
11281
|
-
*/
|
|
11282
|
-
function coarseOps(left, right) {
|
|
11283
|
-
return [...left.map((text) => ({
|
|
11284
|
-
kind: "-",
|
|
11285
|
-
text
|
|
11286
|
-
})), ...right.map((text) => ({
|
|
11287
|
-
kind: "+",
|
|
11288
|
-
text
|
|
11289
|
-
}))];
|
|
11290
|
-
}
|
|
11291
|
-
/**
|
|
11292
|
-
* @author ddj 2026年10月29号
|
|
11293
|
-
* @description: 限额内的 LCS 行差异。
|
|
11294
|
-
* @param left 基线剩余行
|
|
11295
|
-
* @param right 当前剩余行
|
|
11296
|
-
* @returns 操作列表
|
|
11297
|
-
*/
|
|
11298
|
-
function lcsOps(left, right) {
|
|
11299
|
-
const rows = left.length + 1;
|
|
11300
|
-
const cols = right.length + 1;
|
|
11301
|
-
const table = new Uint32Array(rows * cols);
|
|
11302
|
-
for (let i = left.length - 1; i >= 0; i--) for (let j = right.length - 1; j >= 0; j--) table[i * cols + j] = left[i] === right[j] ? table[(i + 1) * cols + j + 1] + 1 : Math.max(table[(i + 1) * cols + j], table[i * cols + j + 1]);
|
|
11303
|
-
const ops = [];
|
|
11304
|
-
let i = 0;
|
|
11305
|
-
let j = 0;
|
|
11306
|
-
while (i < left.length && j < right.length) {
|
|
11307
|
-
if (left[i] === right[j]) {
|
|
11308
|
-
ops.push({
|
|
11309
|
-
kind: " ",
|
|
11310
|
-
text: left[i]
|
|
11311
|
-
});
|
|
11312
|
-
i++;
|
|
11313
|
-
j++;
|
|
11314
|
-
continue;
|
|
11315
|
-
}
|
|
11316
|
-
if (table[(i + 1) * cols + j] >= table[i * cols + j + 1]) {
|
|
11317
|
-
ops.push({
|
|
11318
|
-
kind: "-",
|
|
11319
|
-
text: left[i]
|
|
11320
|
-
});
|
|
11321
|
-
i++;
|
|
11322
|
-
} else {
|
|
11323
|
-
ops.push({
|
|
11324
|
-
kind: "+",
|
|
11325
|
-
text: right[j]
|
|
11326
|
-
});
|
|
11327
|
-
j++;
|
|
11328
|
-
}
|
|
11329
|
-
}
|
|
11330
|
-
while (i < left.length) ops.push({
|
|
11331
|
-
kind: "-",
|
|
11332
|
-
text: left[i++]
|
|
11333
|
-
});
|
|
11334
|
-
while (j < right.length) ops.push({
|
|
11335
|
-
kind: "+",
|
|
11336
|
-
text: right[j++]
|
|
11337
|
-
});
|
|
11338
|
-
return ops;
|
|
11339
|
-
}
|
|
11340
|
-
/**
|
|
11341
|
-
* @author ddj 2026年10月29号
|
|
11342
|
-
* @description: 判断这次差异是否触发了大文件退化口径。
|
|
11343
|
-
* @param oldText 基线全文
|
|
11344
|
-
* @param newText 当前全文
|
|
11345
|
-
* @returns 是否粗粒度
|
|
11346
|
-
*/
|
|
11347
|
-
function isCoarse(oldText, newText) {
|
|
11348
|
-
return logicalLines(oldText).length * logicalLines(newText).length > CELL_LIMIT;
|
|
11349
|
-
}
|
|
11350
11570
|
//#endregion
|
|
11351
11571
|
//#region src/pending/kernel.ts
|
|
11352
11572
|
/**
|