pi-charset 0.1.5 → 0.1.7
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/CHANGELOG.md +32 -0
- package/CHANGELOG.zh-CN.md +32 -0
- package/README.md +3 -0
- package/README.zh-CN.md +3 -0
- package/dist/config.js +943 -0
- package/dist/edit/apply-patch-core.js +274 -0
- package/dist/edit/diagnostics.js +46 -0
- package/dist/edit/hashline.js +400 -0
- package/dist/edit/patch.js +634 -0
- package/dist/edit/paths.js +48 -0
- package/dist/edit/plan.js +90 -0
- package/dist/encoding/classify.js +430 -0
- package/dist/encoding/common-scripts.js +58 -0
- package/dist/encoding/converter.js +311 -0
- package/dist/encoding/fast-codec.js +476 -0
- package/dist/encoding/file-stamp.js +32 -0
- package/dist/encoding/line-endings.js +118 -0
- package/dist/encoding/mime.js +88 -0
- package/dist/encoding/stream-transcode.js +287 -0
- package/dist/grep-core.js +730 -0
- package/dist/journal.js +494 -0
- package/dist/maintenance.js +333 -0
- package/dist/mcp/server.js +866 -0
- package/dist/notify.js +60 -0
- package/dist/operations.js +1021 -0
- package/dist/path-key.js +10 -0
- package/dist/read-view.js +927 -0
- package/dist/resolve.js +325 -0
- package/dist/version.js +2 -0
- package/docs/MCP-AND-MULTI-AGENT.md +119 -0
- package/docs/zh-CN/MCP-AND-MULTI-AGENT.md +119 -0
- package/package.json +18 -4
- package/schema/pi-charset.schema.json +6 -0
- package/src/config.ts +82 -6
- package/src/edit/apply-patch-core.ts +304 -0
- package/src/edit/apply-patch.ts +11 -321
- package/src/edit/diagnostics.ts +5 -2
- package/src/edit/paths.ts +42 -0
- package/src/edit/tool.ts +2 -39
- package/src/encoding/classify.ts +13 -1
- package/src/encoding/converter.ts +3 -1
- package/src/grep-core.ts +729 -0
- package/src/grep.ts +21 -714
- package/src/index.ts +8 -5
- package/src/journal.ts +167 -42
- package/src/mcp/server.ts +923 -0
- package/src/operations.ts +198 -67
- package/src/read-view.ts +111 -8
- package/src/resolve.ts +1 -0
- package/src/shell-guard.ts +38 -1
- package/src/version.ts +1 -1
- package/tools/charset-core.mjs +13 -2
- package/tools/pi-charset-cli.mjs +548 -114
- package/tools/pi-charset-mcp.mjs +140 -0
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
// Hashline content-addressed line editing (pure, zero Pi SDK imports):
|
|
2
|
+
// Every line is tagged `<1-based-line>:<2-hex>|` derived from an FNV-1a hash of the line body (CR ignored).
|
|
3
|
+
// Edits reference anchors (`42:a3`) and fail loudly with current hashes if any anchor line shifted or changed.
|
|
4
|
+
import { detectLineEndingKindFromText } from "../encoding/line-endings.js";
|
|
5
|
+
import { EditError } from "./diagnostics.js";
|
|
6
|
+
import { assertNoMixedResult, assertUniformSource, assertValidText, normalizeReplacementEol } from "./plan.js";
|
|
7
|
+
const HEX2 = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
8
|
+
/**
|
|
9
|
+
* Deterministic, zero-allocation 2-hex-char content tag for `text[start..end)` (trailing \r\n or \n or \r ignored).
|
|
10
|
+
* Uses FNV-1a 32-bit folded to 8 bits for uniform distribution across 256 buckets.
|
|
11
|
+
*/
|
|
12
|
+
export function lineHashRange(text, start, end) {
|
|
13
|
+
if (end > start && text.charCodeAt(end - 1) === 10)
|
|
14
|
+
end--;
|
|
15
|
+
if (end > start && text.charCodeAt(end - 1) === 13)
|
|
16
|
+
end--;
|
|
17
|
+
let h = 0x811c9dc5;
|
|
18
|
+
for (let i = start; i < end; i++) {
|
|
19
|
+
const c = text.charCodeAt(i);
|
|
20
|
+
h ^= c & 0xff;
|
|
21
|
+
h = Math.imul(h, 0x01000193);
|
|
22
|
+
if (c > 0xff) {
|
|
23
|
+
h ^= c >>> 8;
|
|
24
|
+
h = Math.imul(h, 0x01000193);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
h ^= h >>> 16;
|
|
28
|
+
h ^= h >>> 8;
|
|
29
|
+
return HEX2[h & 0xff];
|
|
30
|
+
}
|
|
31
|
+
/** Deterministic, zero-allocation 2-hex-char content tag for a single line (trailing \r\n or \n or \r ignored). */
|
|
32
|
+
export function lineHash(line) {
|
|
33
|
+
return lineHashRange(line, 0, line.length);
|
|
34
|
+
}
|
|
35
|
+
/** Index every line of `text` in a single SIMD `indexOf("\n")` pass. */
|
|
36
|
+
export function indexLines(text) {
|
|
37
|
+
const spans = [];
|
|
38
|
+
const len = text.length;
|
|
39
|
+
if (len === 0)
|
|
40
|
+
return spans;
|
|
41
|
+
let pos = 0;
|
|
42
|
+
let line = 1;
|
|
43
|
+
while (pos < len) {
|
|
44
|
+
const nl = text.indexOf("\n", pos);
|
|
45
|
+
if (nl === -1) {
|
|
46
|
+
spans.push({ line, start: pos, bodyEnd: len, fullEnd: len, hasEol: false });
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
const bodyEnd = nl > pos && text.charCodeAt(nl - 1) === 13 ? nl - 1 : nl;
|
|
50
|
+
spans.push({ line, start: pos, bodyEnd, fullEnd: nl + 1, hasEol: true });
|
|
51
|
+
pos = nl + 1;
|
|
52
|
+
line++;
|
|
53
|
+
}
|
|
54
|
+
return spans;
|
|
55
|
+
}
|
|
56
|
+
/** Annotate each line of `text` with `<line>:<hash>|` starting at `startLine` (1-based) in a single pass. */
|
|
57
|
+
export function formatHashlines(text, startLine = 1) {
|
|
58
|
+
if (!text)
|
|
59
|
+
return text;
|
|
60
|
+
// Strip leading UTF-8 BOM on line 1 so line-1 hashes match edit's loadSnapshot() (which preserves BOM separately).
|
|
61
|
+
const len = text.length;
|
|
62
|
+
let pos = startLine === 1 && text.charCodeAt(0) === 0xfeff ? 1 : 0;
|
|
63
|
+
if (pos >= len)
|
|
64
|
+
return "";
|
|
65
|
+
const out = [];
|
|
66
|
+
let line = startLine;
|
|
67
|
+
while (pos < len) {
|
|
68
|
+
const nl = text.indexOf("\n", pos);
|
|
69
|
+
const bodyEnd = nl === -1 ? len : nl > pos && text.charCodeAt(nl - 1) === 13 ? nl - 1 : nl;
|
|
70
|
+
out.push(`${line}:${lineHashRange(text, pos, bodyEnd)}|${text.slice(pos, bodyEnd)}`);
|
|
71
|
+
line++;
|
|
72
|
+
if (nl === -1)
|
|
73
|
+
return out.join("\n");
|
|
74
|
+
pos = nl + 1;
|
|
75
|
+
}
|
|
76
|
+
return out.join("\n") + "\n";
|
|
77
|
+
}
|
|
78
|
+
const ANCHOR_RE = /^L?(\d+)[:#]([0-9a-fA-F]{2})\|?$/;
|
|
79
|
+
const ECHOED_PREFIX_RE = /^L?\d+[:#][0-9a-fA-F]{2}\|/;
|
|
80
|
+
/**
|
|
81
|
+
* Absorb accidentally echoed `<line>:<2-hex>|` read prefixes (OMP Context Echo Absorption),
|
|
82
|
+
* ONLY when every non-empty line of `content` carries the prefix and none of the replaced source lines did.
|
|
83
|
+
*/
|
|
84
|
+
function stripEchoedHashPrefixes(content, replacedLines) {
|
|
85
|
+
if (!content)
|
|
86
|
+
return content;
|
|
87
|
+
const parts = content.split("\n");
|
|
88
|
+
let sawNonEmpty = false;
|
|
89
|
+
for (const line of parts) {
|
|
90
|
+
if (line === "" || line === "\r")
|
|
91
|
+
continue;
|
|
92
|
+
sawNonEmpty = true;
|
|
93
|
+
if (!ECHOED_PREFIX_RE.test(line))
|
|
94
|
+
return content;
|
|
95
|
+
}
|
|
96
|
+
if (!sawNonEmpty)
|
|
97
|
+
return content;
|
|
98
|
+
if (replacedLines.some((l) => ECHOED_PREFIX_RE.test(l)))
|
|
99
|
+
return content;
|
|
100
|
+
return parts.map((line) => (line === "" || line === "\r" ? line : line.replace(ECHOED_PREFIX_RE, ""))).join("\n");
|
|
101
|
+
}
|
|
102
|
+
export function parseHashlineAnchor(ref, label) {
|
|
103
|
+
if (typeof ref !== "string") {
|
|
104
|
+
throw new EditError("INVALID_INPUT", `${label} must be a "<line>:<2-hex>" anchor string (e.g. "42:a3").`);
|
|
105
|
+
}
|
|
106
|
+
const m = ANCHOR_RE.exec(ref.trim());
|
|
107
|
+
if (!m) {
|
|
108
|
+
throw new EditError("INVALID_INPUT", `${label} "${ref}" is not a valid "<line>:<2-hex>" anchor.`);
|
|
109
|
+
}
|
|
110
|
+
const line = Number(m[1]);
|
|
111
|
+
if (!Number.isSafeInteger(line) || line < 1) {
|
|
112
|
+
throw new EditError("INVALID_INPUT", `${label} line number must be >= 1.`);
|
|
113
|
+
}
|
|
114
|
+
return { line, hash: m[2].toLowerCase() };
|
|
115
|
+
}
|
|
116
|
+
function bodyOf(source, span) {
|
|
117
|
+
return source.slice(span.start, span.bodyEnd);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Plan content-addressed Hashline edits against one immutable source snapshot.
|
|
121
|
+
* Every anchor is validated before applying any change; any mismatch reports all stale anchors with their
|
|
122
|
+
* actual current `<line>:<hash>|<prefix>` lines (bounded within 4 KiB) so the caller can recover in one turn.
|
|
123
|
+
*/
|
|
124
|
+
export function planHashlineEdits(before, ops) {
|
|
125
|
+
const kind = assertUniformSource(before);
|
|
126
|
+
if (!Array.isArray(ops) || ops.length === 0) {
|
|
127
|
+
throw new EditError("INVALID_INPUT", "hashlines must contain at least one operation.");
|
|
128
|
+
}
|
|
129
|
+
const lines = indexLines(before);
|
|
130
|
+
const eol = kind === "CRLF" ? "\r\n" : "\n";
|
|
131
|
+
const mismatches = [];
|
|
132
|
+
const shownContextLines = new Set();
|
|
133
|
+
const verifyAnchor = (anchor, label) => {
|
|
134
|
+
if (anchor.line < 1 || anchor.line > lines.length) {
|
|
135
|
+
mismatches.push(`${label} ${anchor.line}:${anchor.hash} -> out of range (${lines.length} lines)`);
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
const span = lines[anchor.line - 1];
|
|
139
|
+
const actual = lineHashRange(before, span.start, span.bodyEnd);
|
|
140
|
+
if (actual !== anchor.hash) {
|
|
141
|
+
// OMP-style contextual recovery: include +-2 surrounding lines (deduplicated across adjacent mismatches)
|
|
142
|
+
// and detect if the expected hash shifted within +-25 lines.
|
|
143
|
+
const ctxLines = [];
|
|
144
|
+
const lo = Math.max(0, anchor.line - 3);
|
|
145
|
+
const hi = Math.min(lines.length - 1, anchor.line + 1);
|
|
146
|
+
for (let k = lo; k <= hi; k++) {
|
|
147
|
+
const isTarget = k === anchor.line - 1;
|
|
148
|
+
if (!isTarget && shownContextLines.has(k))
|
|
149
|
+
continue;
|
|
150
|
+
shownContextLines.add(k);
|
|
151
|
+
const sp = lines[k];
|
|
152
|
+
ctxLines.push(`${isTarget ? ">" : " "}${k + 1}:${lineHashRange(before, sp.start, sp.bodyEnd)}|${before.slice(sp.start, Math.min(sp.bodyEnd, sp.start + 120))}`);
|
|
153
|
+
}
|
|
154
|
+
const movedHits = [];
|
|
155
|
+
const searchLo = Math.max(0, anchor.line - 26);
|
|
156
|
+
const searchHi = Math.min(lines.length - 1, anchor.line + 24);
|
|
157
|
+
for (let k = searchLo; k <= searchHi; k++) {
|
|
158
|
+
if (k === anchor.line - 1)
|
|
159
|
+
continue;
|
|
160
|
+
if (lineHashRange(before, lines[k].start, lines[k].bodyEnd) === anchor.hash)
|
|
161
|
+
movedHits.push(k);
|
|
162
|
+
}
|
|
163
|
+
let hint = "";
|
|
164
|
+
if (movedHits.length === 1) {
|
|
165
|
+
const idx = movedHits[0];
|
|
166
|
+
const sp = lines[idx];
|
|
167
|
+
hint = `\n hint: hash ${anchor.hash} moved to ${idx + 1}:${anchor.hash}|${before.slice(sp.start, Math.min(sp.bodyEnd, sp.start + 120))}`;
|
|
168
|
+
}
|
|
169
|
+
mismatches.push(`${label} expected ${anchor.line}:${anchor.hash} (actual ${anchor.line}:${actual}):\n${ctxLines.join("\n")}${hint}`);
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return span;
|
|
173
|
+
};
|
|
174
|
+
const parsedOps = [];
|
|
175
|
+
for (let i = 0; i < ops.length; i++) {
|
|
176
|
+
const raw = ops[i];
|
|
177
|
+
if (!raw || typeof raw !== "object") {
|
|
178
|
+
throw new EditError("INVALID_INPUT", `hashlines[${i}] must be an operation object.`);
|
|
179
|
+
}
|
|
180
|
+
if (raw.op === "replace" || raw.op === "delete") {
|
|
181
|
+
const startAnchor = parseHashlineAnchor(raw.start, `hashlines[${i}].start`);
|
|
182
|
+
const endAnchor = raw.end !== undefined ? parseHashlineAnchor(raw.end, `hashlines[${i}].end`) : startAnchor;
|
|
183
|
+
if (endAnchor.line < startAnchor.line) {
|
|
184
|
+
throw new EditError("INVALID_INPUT", `hashlines[${i}] end line (${endAnchor.line}) precedes start line (${startAnchor.line}).`);
|
|
185
|
+
}
|
|
186
|
+
if (raw.op === "replace") {
|
|
187
|
+
assertValidText(raw.content, `hashlines[${i}].content`);
|
|
188
|
+
parsedOps.push({ index: i, op: "replace", startAnchor, endAnchor, content: raw.content });
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
parsedOps.push({ index: i, op: "delete", startAnchor, endAnchor });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else if (raw.op === "insert_after" || raw.op === "insert_before") {
|
|
195
|
+
const startAnchor = parseHashlineAnchor(raw.anchor, `hashlines[${i}].anchor`);
|
|
196
|
+
assertValidText(raw.content, `hashlines[${i}].content`);
|
|
197
|
+
parsedOps.push({ index: i, op: raw.op, startAnchor, content: raw.content });
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
throw new EditError("INVALID_INPUT", `hashlines[${i}].op must be "replace", "delete", "insert_after", or "insert_before".`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const resolved = [];
|
|
204
|
+
for (const item of parsedOps) {
|
|
205
|
+
const startSpan = verifyAnchor(item.startAnchor, `hashlines[${item.index}]`);
|
|
206
|
+
const endSpan = item.endAnchor && item.endAnchor !== item.startAnchor
|
|
207
|
+
? verifyAnchor(item.endAnchor, `hashlines[${item.index}].end`)
|
|
208
|
+
: startSpan;
|
|
209
|
+
if (!startSpan || !endSpan)
|
|
210
|
+
continue;
|
|
211
|
+
if (item.op === "replace") {
|
|
212
|
+
const origSlice = [];
|
|
213
|
+
for (let ln = startSpan.line - 1; ln <= endSpan.line - 1; ln++)
|
|
214
|
+
origSlice.push(bodyOf(before, lines[ln]));
|
|
215
|
+
const cleanContent = stripEchoedHashPrefixes(item.content, origSlice);
|
|
216
|
+
const text = normalizeReplacementEol(cleanContent, kind);
|
|
217
|
+
resolved.push({
|
|
218
|
+
index: item.index,
|
|
219
|
+
slotStart: startSpan.line * 2,
|
|
220
|
+
slotEnd: endSpan.line * 2,
|
|
221
|
+
start: startSpan.start,
|
|
222
|
+
end: endSpan.bodyEnd,
|
|
223
|
+
text,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
else if (item.op === "delete") {
|
|
227
|
+
// Deleting the final line of a file without trailing EOL also removes the preceding line's EOL so no
|
|
228
|
+
// stray trailing newline is synthesized.
|
|
229
|
+
const deleteFrom = !endSpan.hasEol && startSpan.line > 1 ? lines[startSpan.line - 2].bodyEnd : startSpan.start;
|
|
230
|
+
const deleteTo = endSpan.fullEnd;
|
|
231
|
+
resolved.push({
|
|
232
|
+
index: item.index,
|
|
233
|
+
slotStart: startSpan.line * 2,
|
|
234
|
+
slotEnd: endSpan.line * 2,
|
|
235
|
+
start: deleteFrom,
|
|
236
|
+
end: deleteTo,
|
|
237
|
+
text: "",
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
else if (item.op === "insert_after") {
|
|
241
|
+
const cleanContent = stripEchoedHashPrefixes(item.content, []);
|
|
242
|
+
const body = normalizeReplacementEol(cleanContent, kind);
|
|
243
|
+
const opEol = kind === "NONE" && detectLineEndingKindFromText(body) === "CRLF" ? "\r\n" : eol;
|
|
244
|
+
const pos = startSpan.hasEol ? startSpan.fullEnd : startSpan.bodyEnd;
|
|
245
|
+
const text = startSpan.hasEol ? body + opEol : opEol + body;
|
|
246
|
+
resolved.push({
|
|
247
|
+
index: item.index,
|
|
248
|
+
slotStart: startSpan.line * 2 + 1,
|
|
249
|
+
slotEnd: startSpan.line * 2 + 1,
|
|
250
|
+
start: pos,
|
|
251
|
+
end: pos,
|
|
252
|
+
text,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
const cleanContent = stripEchoedHashPrefixes(item.content, []);
|
|
257
|
+
const body = normalizeReplacementEol(cleanContent, kind);
|
|
258
|
+
const opEol = kind === "NONE" && detectLineEndingKindFromText(body) === "CRLF" ? "\r\n" : eol;
|
|
259
|
+
resolved.push({
|
|
260
|
+
index: item.index,
|
|
261
|
+
slotStart: startSpan.line * 2 - 1,
|
|
262
|
+
slotEnd: startSpan.line * 2 - 1,
|
|
263
|
+
start: startSpan.start,
|
|
264
|
+
end: startSpan.start,
|
|
265
|
+
text: body + opEol,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (mismatches.length > 0) {
|
|
270
|
+
let summary = "";
|
|
271
|
+
let shown = 0;
|
|
272
|
+
for (const m of mismatches) {
|
|
273
|
+
const sep = summary ? "\n" : "";
|
|
274
|
+
if (summary.length + sep.length + m.length > 3500 && shown > 0)
|
|
275
|
+
break;
|
|
276
|
+
summary += sep + m;
|
|
277
|
+
shown++;
|
|
278
|
+
}
|
|
279
|
+
const more = mismatches.length > shown ? `\n... (${mismatches.length - shown} more)` : "";
|
|
280
|
+
throw new EditError("HASH_MISMATCH", `Hashline anchor verification failed; no changes applied. Current lines:\n${summary}${more}`);
|
|
281
|
+
}
|
|
282
|
+
// Sort by slot position so overlap detection and assembly are completely order-independent.
|
|
283
|
+
resolved.sort((a, b) => a.slotStart - b.slotStart || a.slotEnd - b.slotEnd || a.index - b.index);
|
|
284
|
+
for (let i = 1; i < resolved.length; i++) {
|
|
285
|
+
const prev = resolved[i - 1];
|
|
286
|
+
const cur = resolved[i];
|
|
287
|
+
const slotOverlap = prev.slotEnd >= cur.slotStart;
|
|
288
|
+
const charOverlap = prev.end > cur.start || (prev.start === prev.end && cur.start === cur.end && prev.start === cur.start);
|
|
289
|
+
if (slotOverlap || charOverlap) {
|
|
290
|
+
throw new EditError("OVERLAPPING_EDITS", `hashlines[${prev.index}] and hashlines[${cur.index}] overlap or target the same anchor point.`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const parts = [];
|
|
294
|
+
let pos = 0;
|
|
295
|
+
const ranges = [];
|
|
296
|
+
for (const r of resolved) {
|
|
297
|
+
parts.push(before.slice(pos, r.start), r.text);
|
|
298
|
+
ranges.push({ index: r.index, start: r.start, end: r.end, text: r.text });
|
|
299
|
+
pos = r.end;
|
|
300
|
+
}
|
|
301
|
+
parts.push(before.slice(pos));
|
|
302
|
+
const after = parts.join("");
|
|
303
|
+
assertNoMixedResult(after, kind);
|
|
304
|
+
return { before, after, ranges, changed: after !== before };
|
|
305
|
+
}
|
|
306
|
+
function countLf(text, from, to) {
|
|
307
|
+
let n = 0;
|
|
308
|
+
for (let p = text.indexOf("\n", from); p !== -1 && p < to; p = text.indexOf("\n", p + 1))
|
|
309
|
+
n++;
|
|
310
|
+
return n;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Format compact post-edit `<newLine>:<newHash>|` anchors for modified/inserted spans in `plan.after`,
|
|
314
|
+
* plus net line delta, so the model can chain subsequent Hashline edits without re-reading the file.
|
|
315
|
+
* Runs in O(last edited offset) with zero full-file line array allocation.
|
|
316
|
+
*/
|
|
317
|
+
export function formatUpdatedHashlines(plan, maxLines = 20, maxChars = 1200) {
|
|
318
|
+
if (!plan.changed || plan.ranges.length === 0)
|
|
319
|
+
return { lines: [], lineDelta: 0, omitted: 0 };
|
|
320
|
+
const after = plan.after;
|
|
321
|
+
const out = [];
|
|
322
|
+
let chars = 0;
|
|
323
|
+
let omitted = 0;
|
|
324
|
+
let charDelta = 0;
|
|
325
|
+
let lineDelta = 0;
|
|
326
|
+
let scanPos = 0;
|
|
327
|
+
let scanLine = 1;
|
|
328
|
+
const emitted = new Set();
|
|
329
|
+
const advanceTo = (offset) => {
|
|
330
|
+
while (scanPos < offset) {
|
|
331
|
+
const nl = after.indexOf("\n", scanPos);
|
|
332
|
+
if (nl === -1 || nl >= offset)
|
|
333
|
+
break;
|
|
334
|
+
scanPos = nl + 1;
|
|
335
|
+
scanLine++;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
for (const r of plan.ranges) {
|
|
339
|
+
const newStart = r.start + charDelta;
|
|
340
|
+
const newEnd = newStart + r.text.length;
|
|
341
|
+
const oldNls = countLf(plan.before, r.start, r.end);
|
|
342
|
+
const newNls = countLf(r.text, 0, r.text.length);
|
|
343
|
+
lineDelta += newNls - oldNls;
|
|
344
|
+
charDelta += r.text.length - (r.end - r.start);
|
|
345
|
+
if (r.text.length === 0) {
|
|
346
|
+
// Pure line deletion removes the line completely; only replacing a line body with "" keeps an empty line to anchor.
|
|
347
|
+
if (oldNls > 0 || (r.start === 0 && r.end === plan.before.length))
|
|
348
|
+
continue;
|
|
349
|
+
advanceTo(newStart);
|
|
350
|
+
if (!emitted.has(scanLine) && scanLine >= 1) {
|
|
351
|
+
emitted.add(scanLine);
|
|
352
|
+
const tag = `${scanLine}:${lineHash("")}|`;
|
|
353
|
+
if (out.length < maxLines && chars + tag.length + 1 <= maxChars) {
|
|
354
|
+
out.push(tag);
|
|
355
|
+
chars += tag.length + 1;
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
omitted++;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
advanceTo(newStart);
|
|
364
|
+
let ls = scanPos;
|
|
365
|
+
let ln = scanLine;
|
|
366
|
+
// When inserting after a last line that lacked a trailing EOL, newStart sits at the preceding line's bodyEnd
|
|
367
|
+
// (scanPos < newStart) and r.text begins with the separator EOL; advance past that untouched line first.
|
|
368
|
+
if (ls < newStart && (r.text.charCodeAt(0) === 10 || (r.text.charCodeAt(0) === 13 && r.text.charCodeAt(1) === 10))) {
|
|
369
|
+
const firstNl = after.indexOf("\n", ls);
|
|
370
|
+
if (firstNl !== -1 && firstNl < newEnd) {
|
|
371
|
+
ls = firstNl + 1;
|
|
372
|
+
ln++;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
while (ls < newEnd && ls < after.length) {
|
|
376
|
+
const nl = after.indexOf("\n", ls);
|
|
377
|
+
const fullEnd = nl === -1 ? after.length : nl + 1;
|
|
378
|
+
const bodyEnd = nl === -1 ? after.length : nl > ls && after.charCodeAt(nl - 1) === 13 ? nl - 1 : nl;
|
|
379
|
+
if (!emitted.has(ln)) {
|
|
380
|
+
emitted.add(ln);
|
|
381
|
+
const body = after.slice(ls, bodyEnd);
|
|
382
|
+
const tag = `${ln}:${lineHash(body)}|${body.slice(0, 100)}`;
|
|
383
|
+
if (out.length < maxLines && chars + tag.length + 1 <= maxChars) {
|
|
384
|
+
out.push(tag);
|
|
385
|
+
chars += tag.length + 1;
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
omitted++;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (nl === -1)
|
|
392
|
+
break;
|
|
393
|
+
ls = fullEnd;
|
|
394
|
+
ln++;
|
|
395
|
+
}
|
|
396
|
+
scanPos = ls;
|
|
397
|
+
scanLine = ln;
|
|
398
|
+
}
|
|
399
|
+
return { lines: out, lineDelta, omitted };
|
|
400
|
+
}
|