@criogaid/pi-hashline-edit 0.9.0 β 0.11.0
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 +12 -12
- package/README.zh-CN.md +12 -12
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
package/src/core/apply.test.ts
CHANGED
|
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
|
|
|
3
3
|
import { computeLineHash } from "./hash.ts";
|
|
4
4
|
import { splitLines } from "./lines.ts";
|
|
5
5
|
import { applyEdits } from "./apply.ts";
|
|
6
|
+
import { HashlineError } from "./errors.ts";
|
|
6
7
|
import type { Anchor, Edit } from "./types.ts";
|
|
7
8
|
|
|
8
9
|
/** Live anchor: hash the current content at the given line (1-based). */
|
|
@@ -102,7 +103,6 @@ test("line out of range rejected", () => {
|
|
|
102
103
|
15,
|
|
103
104
|
);
|
|
104
105
|
assert.equal(r.ok, false);
|
|
105
|
-
if (!r.ok) assert.equal(r.failure.kind, "anchor");
|
|
106
106
|
});
|
|
107
107
|
|
|
108
108
|
test("anchor mismatch when the cited line's content differs (live verification)", () => {
|
|
@@ -115,37 +115,30 @@ test("anchor mismatch when the cited line's content differs (live verification)"
|
|
|
115
115
|
15,
|
|
116
116
|
);
|
|
117
117
|
assert.equal(r.ok, false);
|
|
118
|
-
if (!r.ok) assert.equal(r.failure.kind, "anchor");
|
|
119
118
|
});
|
|
120
119
|
|
|
121
|
-
test("
|
|
122
|
-
const text = "a\nb\nc\n";
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
[{ op: "replace", anchor: at(text, 3),
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
4,
|
|
144
|
-
0,
|
|
145
|
-
);
|
|
146
|
-
assert(!result.ok && result.failure.kind === "range");
|
|
147
|
-
assert.equal(result.failure.code, "OVERLAPPING_EDITS");
|
|
148
|
-
assert.match(result.failure.message, /overlap/);
|
|
120
|
+
test("overlapping edit spans β report conflicting operations in input order", () => {
|
|
121
|
+
const text = "a\nb\nc\nd\ne\nf\n";
|
|
122
|
+
const range: Edit = { op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["x"] };
|
|
123
|
+
const cases: Edit[][] = [
|
|
124
|
+
[range, { op: "replace", anchor: at(text, 3), body: ["y"] }],
|
|
125
|
+
[range, { op: "delete", anchor: at(text, 2) }],
|
|
126
|
+
[range, { op: "insert_after", anchor: at(text, 2), body: ["y"] }],
|
|
127
|
+
];
|
|
128
|
+
for (const edits of cases) {
|
|
129
|
+
for (const pair of [edits, [...edits].reverse()]) {
|
|
130
|
+
const batch: Edit[] = [{ op: "append", body: ["tail"] }, ...pair];
|
|
131
|
+
assert.throws(
|
|
132
|
+
() => applyEdits(text, batch, 4, 0),
|
|
133
|
+
(error: unknown) => {
|
|
134
|
+
assert(error instanceof HashlineError);
|
|
135
|
+
assert.equal(error.errorCode, "OVERLAPPING_EDITS");
|
|
136
|
+
assert.equal(error.message, "Two edits overlap.");
|
|
137
|
+
assert.deepEqual(error.facts?.conflict, ["steps[1]", "steps[2]"]);
|
|
138
|
+
return true;
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
}
|
|
149
142
|
}
|
|
150
143
|
});
|
|
151
144
|
|
|
@@ -178,14 +171,6 @@ test("insertions sharing a gap β reject the batch", () => {
|
|
|
178
171
|
{ op: "insert_before", anchor: at(text, 3), body: ["x"] },
|
|
179
172
|
{ op: "insert_before", anchor: at(text, 3), body: ["y"] },
|
|
180
173
|
],
|
|
181
|
-
[
|
|
182
|
-
{ op: "copy", anchor: at(text, 1), after: at(text, 2) },
|
|
183
|
-
{ op: "insert_before", anchor: at(text, 3), body: ["y"] },
|
|
184
|
-
],
|
|
185
|
-
[
|
|
186
|
-
{ op: "move", anchor: at(text, 1), after: at(text, 2) },
|
|
187
|
-
{ op: "insert_before", anchor: at(text, 3), body: ["y"] },
|
|
188
|
-
],
|
|
189
174
|
[
|
|
190
175
|
{ op: "append", body: ["x"] },
|
|
191
176
|
{ op: "append", body: ["y"] },
|
|
@@ -197,10 +182,16 @@ test("insertions sharing a gap β reject the batch", () => {
|
|
|
197
182
|
];
|
|
198
183
|
for (const edits of cases) {
|
|
199
184
|
for (const batch of [edits, [...edits].reverse()]) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
185
|
+
assert.throws(
|
|
186
|
+
() => applyEdits(text, batch, 4, 0),
|
|
187
|
+
(error: unknown) => {
|
|
188
|
+
assert(error instanceof HashlineError);
|
|
189
|
+
assert.equal(error.errorCode, "OVERLAPPING_EDITS");
|
|
190
|
+
assert.equal(error.message, "Two edits insert at one position.");
|
|
191
|
+
assert.deepEqual(error.facts?.conflict, ["steps[0]", "steps[1]"]);
|
|
192
|
+
return true;
|
|
193
|
+
},
|
|
194
|
+
);
|
|
204
195
|
}
|
|
205
196
|
}
|
|
206
197
|
});
|
|
@@ -217,7 +208,7 @@ test("byte-identical edits β validate anchors and preserve the source bytes",
|
|
|
217
208
|
4,
|
|
218
209
|
15,
|
|
219
210
|
);
|
|
220
|
-
assert.ok(!invalid.ok
|
|
211
|
+
assert.ok(!invalid.ok);
|
|
221
212
|
}
|
|
222
213
|
});
|
|
223
214
|
|
|
@@ -379,11 +370,10 @@ test("rescued anchor lets the retry succeed without a re-read", () => {
|
|
|
379
370
|
4,
|
|
380
371
|
15,
|
|
381
372
|
);
|
|
382
|
-
assert.ok(!r1.ok
|
|
383
|
-
const f = r1.
|
|
373
|
+
assert.ok(!r1.ok);
|
|
374
|
+
const f = r1.failures[0];
|
|
384
375
|
assert.ok(f.recovery.kind === "found");
|
|
385
376
|
assert.equal(f.recovery.newLine, 4);
|
|
386
|
-
assert.equal(f.recovery.scope, "local");
|
|
387
377
|
assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
|
|
388
378
|
// Retry with the recovered anchor; the rest of the file stays unchanged.
|
|
389
379
|
const r2 = applyEdits(
|
|
@@ -411,17 +401,16 @@ test("shifted recovery: duplicate content β ambiguous candidates", () => {
|
|
|
411
401
|
4,
|
|
412
402
|
15,
|
|
413
403
|
);
|
|
414
|
-
assert.ok(!r.ok
|
|
415
|
-
const f = r.
|
|
404
|
+
assert.ok(!r.ok);
|
|
405
|
+
const f = r.failures[0];
|
|
416
406
|
assert.ok(f.recovery.kind === "ambiguous");
|
|
417
|
-
assert.equal(f.recovery.scope, "local");
|
|
418
407
|
assert.deepEqual(
|
|
419
408
|
f.recovery.candidates.map((c) => c.line),
|
|
420
409
|
[3, 5],
|
|
421
410
|
);
|
|
422
411
|
});
|
|
423
412
|
|
|
424
|
-
test("shifted recovery: content genuinely changed β none
|
|
413
|
+
test("shifted recovery: content genuinely changed β none", () => {
|
|
425
414
|
const readText = "a\nb\nc\n";
|
|
426
415
|
const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
|
|
427
416
|
const r = applyEdits(
|
|
@@ -430,10 +419,9 @@ test("shifted recovery: content genuinely changed β none, with live content",
|
|
|
430
419
|
4,
|
|
431
420
|
15,
|
|
432
421
|
);
|
|
433
|
-
assert.ok(!r.ok
|
|
434
|
-
const f = r.
|
|
422
|
+
assert.ok(!r.ok);
|
|
423
|
+
const f = r.failures[0];
|
|
435
424
|
assert.equal(f.recovery.kind, "none");
|
|
436
|
-
assert.equal(f.current?.content, "BCHANGED");
|
|
437
425
|
});
|
|
438
426
|
|
|
439
427
|
test("full-file recovery finds distant content and anchors beyond the current EOF", () => {
|
|
@@ -450,10 +438,9 @@ test("full-file recovery finds distant content and anchors beyond the current EO
|
|
|
450
438
|
4,
|
|
451
439
|
15,
|
|
452
440
|
);
|
|
453
|
-
assert.ok(!result.ok
|
|
454
|
-
assert.deepEqual(result.
|
|
441
|
+
assert.ok(!result.ok);
|
|
442
|
+
assert.deepEqual(result.failures[0].recovery, {
|
|
455
443
|
kind: "found",
|
|
456
|
-
scope: "full-file",
|
|
457
444
|
newLine,
|
|
458
445
|
newHash: computeLineHash(newLine, "target", 4),
|
|
459
446
|
});
|
|
@@ -461,10 +448,10 @@ test("full-file recovery finds distant content and anchors beyond the current EO
|
|
|
461
448
|
});
|
|
462
449
|
|
|
463
450
|
test("full-file recovery collects both sides while preserving local candidate priority", () => {
|
|
464
|
-
for (const { positions, selected
|
|
465
|
-
{ positions: [3, 95], selected: [3, 95]
|
|
466
|
-
{ positions: [3, 49, 95], selected: [49]
|
|
467
|
-
{ positions: [3, 49, 51, 95], selected: [49, 51]
|
|
451
|
+
for (const { positions, selected } of [
|
|
452
|
+
{ positions: [3, 95], selected: [3, 95] },
|
|
453
|
+
{ positions: [3, 49, 95], selected: [49] },
|
|
454
|
+
{ positions: [3, 49, 51, 95], selected: [49, 51] },
|
|
468
455
|
]) {
|
|
469
456
|
const lines = Array.from({ length: 100 }, () => "filler");
|
|
470
457
|
for (const line of positions) lines[line - 1] = "target";
|
|
@@ -474,19 +461,17 @@ test("full-file recovery collects both sides while preserving local candidate pr
|
|
|
474
461
|
4,
|
|
475
462
|
15,
|
|
476
463
|
);
|
|
477
|
-
assert.ok(!result.ok
|
|
464
|
+
assert.ok(!result.ok);
|
|
478
465
|
assert.deepEqual(
|
|
479
|
-
result.
|
|
466
|
+
result.failures[0].recovery,
|
|
480
467
|
selected.length === 1
|
|
481
468
|
? {
|
|
482
469
|
kind: "found",
|
|
483
|
-
scope,
|
|
484
470
|
newLine: selected[0],
|
|
485
471
|
newHash: computeLineHash(selected[0], "target", 4),
|
|
486
472
|
}
|
|
487
473
|
: {
|
|
488
474
|
kind: "ambiguous",
|
|
489
|
-
scope,
|
|
490
475
|
candidates: selected.map((line) => ({
|
|
491
476
|
line,
|
|
492
477
|
hash: computeLineHash(line, "target", 4),
|
|
@@ -496,7 +481,7 @@ test("full-file recovery collects both sides while preserving local candidate pr
|
|
|
496
481
|
}
|
|
497
482
|
});
|
|
498
483
|
|
|
499
|
-
test("
|
|
484
|
+
test("two stale anchors and overlapping verified edits β return both anchor failures first", () => {
|
|
500
485
|
const readText = "a\nb\nc\nd\n";
|
|
501
486
|
const currentText = "X\na\nb\nc\nd\n"; // inserted X at top β all shifted +1
|
|
502
487
|
const r = applyEdits(
|
|
@@ -504,15 +489,15 @@ test("collect-all: two stale anchors in one batch β both failures returned", (
|
|
|
504
489
|
[
|
|
505
490
|
{ op: "replace", anchor: at(readText, 2), body: ["B"] },
|
|
506
491
|
{ op: "replace", anchor: at(readText, 4), body: ["D"] },
|
|
492
|
+
{ op: "delete", anchor: at(currentText, 1) },
|
|
493
|
+
{ op: "replace", anchor: at(currentText, 1), body: ["conflict"] },
|
|
507
494
|
],
|
|
508
495
|
4,
|
|
509
496
|
15,
|
|
510
497
|
);
|
|
511
|
-
assert.ok(!r.ok
|
|
512
|
-
assert.equal(r.
|
|
513
|
-
const found = r.
|
|
514
|
-
f.recovery.kind === "found" ? f.recovery.newLine : -1,
|
|
515
|
-
);
|
|
498
|
+
assert.ok(!r.ok);
|
|
499
|
+
assert.equal(r.failures.length, 2);
|
|
500
|
+
const found = r.failures.map((f) => (f.recovery.kind === "found" ? f.recovery.newLine : -1));
|
|
516
501
|
assert.deepEqual(found, [3, 5]);
|
|
517
502
|
});
|
|
518
503
|
|
|
@@ -525,8 +510,8 @@ test("shiftRadius=0 disables rescue (always none)", () => {
|
|
|
525
510
|
4,
|
|
526
511
|
0,
|
|
527
512
|
);
|
|
528
|
-
assert.ok(!r.ok
|
|
529
|
-
assert.equal(r.
|
|
513
|
+
assert.ok(!r.ok);
|
|
514
|
+
assert.equal(r.failures[0].recovery.kind, "none");
|
|
530
515
|
});
|
|
531
516
|
|
|
532
517
|
test("BOM stays at byte zero through first-line edits while anchors retain their original hashes", () => {
|
|
@@ -579,228 +564,68 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
|
|
|
579
564
|
assert.equal(result.text, "\uFEFFchanged\ninside\uFEFFcontent\n");
|
|
580
565
|
});
|
|
581
566
|
|
|
582
|
-
test("
|
|
583
|
-
const text = "a\nb\nc\nd\ne\nf\n";
|
|
584
|
-
const stale = { line: 2, hash: "ZZ" };
|
|
585
|
-
const edits: Edit[] = [
|
|
586
|
-
{ op: "replace", anchor: at(text, 1), end: stale, body: ["A"] },
|
|
587
|
-
{ op: "delete", anchor: stale, end: at(text, 4) },
|
|
588
|
-
{ op: "insert_after", anchor: at(text, 5), body: ["E"] },
|
|
589
|
-
{ op: "insert_before", anchor: stale, body: ["B"] },
|
|
590
|
-
{ op: "append", body: ["last"] },
|
|
591
|
-
{ op: "prepend", body: ["first"] },
|
|
592
|
-
];
|
|
593
|
-
const result = applyEdits(text, edits, 4, 15);
|
|
594
|
-
assert.ok(!result.ok && result.failure.kind === "anchor");
|
|
595
|
-
assert.deepEqual(result.failure.checks, [
|
|
596
|
-
{ opIndex: 0, which: "anchor", status: "matched" },
|
|
597
|
-
{ opIndex: 0, which: "end", status: "mismatched" },
|
|
598
|
-
{ opIndex: 1, which: "anchor", status: "mismatched" },
|
|
599
|
-
{ opIndex: 1, which: "end", status: "matched" },
|
|
600
|
-
{ opIndex: 2, which: "anchor", status: "matched" },
|
|
601
|
-
{ opIndex: 3, which: "anchor", status: "mismatched" },
|
|
602
|
-
]);
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
test("matched anchor checks do not imply valid ranges", () => {
|
|
606
|
-
const text = "a\nb\nc\n";
|
|
607
|
-
const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
|
|
608
|
-
{ edits: [{ op: "delete", anchor: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
|
|
609
|
-
{
|
|
610
|
-
edits: [
|
|
611
|
-
{ op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["x"] },
|
|
612
|
-
{ op: "delete", anchor: at(text, 2) },
|
|
613
|
-
],
|
|
614
|
-
kind: "range",
|
|
615
|
-
checks: 3,
|
|
616
|
-
},
|
|
617
|
-
];
|
|
618
|
-
for (const { edits, kind, checks } of cases) {
|
|
619
|
-
const result = applyEdits(text, edits, 4, 15);
|
|
620
|
-
assert.ok(!result.ok);
|
|
621
|
-
assert.equal(result.failure.kind, kind);
|
|
622
|
-
assert.equal(result.failure.checks.length, checks);
|
|
623
|
-
assert.ok(result.failure.checks.every((check) => check.status === "matched"));
|
|
624
|
-
}
|
|
625
|
-
});
|
|
626
|
-
|
|
627
|
-
test("copy/move original ranges β retain captured text and order", () => {
|
|
628
|
-
const text = "A\nB\nC\nD\n";
|
|
629
|
-
const cases: { edit: Edit; expected: string }[] = [
|
|
630
|
-
{
|
|
631
|
-
edit: { op: "copy", anchor: at(text, 2), end: at(text, 3), after: at(text, 4) },
|
|
632
|
-
expected: "A\nB\nC\nD\nB\nC\n",
|
|
633
|
-
},
|
|
634
|
-
{
|
|
635
|
-
edit: { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) },
|
|
636
|
-
expected: "C\nD\nA\nB\n",
|
|
637
|
-
},
|
|
638
|
-
{
|
|
639
|
-
edit: { op: "move", anchor: at(text, 3), end: at(text, 4), before: at(text, 1) },
|
|
640
|
-
expected: "C\nD\nA\nB\n",
|
|
641
|
-
},
|
|
642
|
-
{
|
|
643
|
-
edit: { op: "copy", anchor: at(text, 1), end: at(text, 3), after: at(text, 1) },
|
|
644
|
-
expected: "A\nA\nB\nC\nB\nC\nD\n",
|
|
645
|
-
},
|
|
646
|
-
];
|
|
647
|
-
for (const { edit, expected } of cases) {
|
|
648
|
-
const result = applyEdits(text, [edit], 4, 0);
|
|
649
|
-
assert(result.ok);
|
|
650
|
-
assert.equal(result.text, expected);
|
|
651
|
-
}
|
|
652
|
-
});
|
|
653
|
-
|
|
654
|
-
test("copy source also replaced or deleted β capture original text regardless of batch order", () => {
|
|
655
|
-
const text = "A\nB\nC\nD\n";
|
|
656
|
-
const copy: Edit = { op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) };
|
|
657
|
-
for (const edit of [
|
|
658
|
-
{ op: "replace", anchor: at(text, 1), end: at(text, 2), body: ["NEW"] },
|
|
659
|
-
{ op: "delete", anchor: at(text, 1), end: at(text, 2) },
|
|
660
|
-
] satisfies Edit[]) {
|
|
661
|
-
for (const edits of [
|
|
662
|
-
[copy, edit],
|
|
663
|
-
[edit, copy],
|
|
664
|
-
]) {
|
|
665
|
-
const result = applyEdits(text, edits, 4, 0);
|
|
666
|
-
assert(result.ok);
|
|
667
|
-
assert.equal(result.text, edit.op === "replace" ? "NEW\nC\nD\nA\nB\n" : "C\nD\nA\nB\n");
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
});
|
|
671
|
-
|
|
672
|
-
test("copy/move first-line mixed-ending range β preserve source gaps and keep one file BOM", () => {
|
|
567
|
+
test("replacing a first-line mixed-ending range β preserve gaps and keep one file BOM", () => {
|
|
673
568
|
const text = "\uFEFFA\r\nB\nC\r\nD\n";
|
|
674
|
-
for (const op of ["copy", "move"] as const) {
|
|
675
|
-
const result = applyEdits(
|
|
676
|
-
text,
|
|
677
|
-
[{ op, anchor: at(text, 1), end: at(text, 3), after: at(text, 4) }],
|
|
678
|
-
4,
|
|
679
|
-
0,
|
|
680
|
-
);
|
|
681
|
-
assert(result.ok);
|
|
682
|
-
assert.equal(result.text, op === "copy" ? text + "A\r\nB\nC\r\n" : "\uFEFFD\nA\r\nB\nC\r\n");
|
|
683
|
-
}
|
|
684
|
-
});
|
|
685
|
-
|
|
686
|
-
test("copy/move unterminated final line β add interior connectors and preserve final-newline state", () => {
|
|
687
|
-
for (const ending of ["\n", "\r\n"]) {
|
|
688
|
-
const text = `A${ending}B`;
|
|
689
|
-
for (const op of ["copy", "move"] as const) {
|
|
690
|
-
const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
|
|
691
|
-
assert(result.ok);
|
|
692
|
-
assert.equal(result.text, op === "copy" ? `B${ending}A${ending}B` : `B${ending}A`);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
});
|
|
696
|
-
|
|
697
|
-
test("copy/move line ending in standalone CR β connector retains the CR as line content", () => {
|
|
698
|
-
const text = "A\nB\r";
|
|
699
|
-
for (const op of ["copy", "move"] as const) {
|
|
700
|
-
const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
|
|
701
|
-
assert(result.ok);
|
|
702
|
-
assert.equal(result.text, op === "copy" ? "B\r\r\nA\nB\r" : "B\r\r\nA");
|
|
703
|
-
assert.equal(splitLines(result.text)[0], "B\r");
|
|
704
|
-
}
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
test("copy Unicode and standalone-CR content β preserve every source code unit", () => {
|
|
708
|
-
const text = "πδΈζ e\u0301\nkeep\rcontent\n";
|
|
709
569
|
const result = applyEdits(
|
|
710
570
|
text,
|
|
711
|
-
[{ op: "
|
|
571
|
+
[{ op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["X", "Y", "Z"] }],
|
|
712
572
|
4,
|
|
713
573
|
0,
|
|
714
574
|
);
|
|
715
575
|
assert(result.ok);
|
|
716
|
-
assert.equal(result.text,
|
|
576
|
+
assert.equal(result.text, "\uFEFFX\r\nY\nZ\r\nD\n");
|
|
717
577
|
});
|
|
718
578
|
|
|
719
|
-
test("
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
["
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
4,
|
|
728
|
-
0,
|
|
729
|
-
);
|
|
579
|
+
test("inserting beside a line ending in standalone CR β connector retains the CR as content", () => {
|
|
580
|
+
const text = "A\nB\r";
|
|
581
|
+
const cases: [Edit, string][] = [
|
|
582
|
+
[{ op: "append", body: ["C"] }, "A\nB\r\r\nC"],
|
|
583
|
+
[{ op: "prepend", body: ["B\r"] }, "B\r\r\nA\nB\r"],
|
|
584
|
+
];
|
|
585
|
+
for (const [edit, expected] of cases) {
|
|
586
|
+
const result = applyEdits(text, [edit], 4, 0);
|
|
730
587
|
assert(result.ok);
|
|
731
588
|
assert.equal(result.text, expected);
|
|
732
|
-
assert.equal(splitLines(result.text).
|
|
589
|
+
assert.equal(splitLines(result.text)[edit.op === "append" ? 1 : 0], "B\r");
|
|
733
590
|
}
|
|
734
591
|
});
|
|
735
592
|
|
|
736
|
-
test("
|
|
737
|
-
const text = "
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
[{ op: "move", anchor: at(text, 2), end: at(text, 3), ...destination }],
|
|
742
|
-
4,
|
|
743
|
-
0,
|
|
744
|
-
);
|
|
745
|
-
assert(result.ok);
|
|
746
|
-
assert.equal(result.text, text);
|
|
747
|
-
}
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
test("copy/move bad source and destination anchors β aggregate every supplied field", () => {
|
|
751
|
-
const text = "A\nB\nC\nD\n";
|
|
752
|
-
for (const destination of [
|
|
753
|
-
{ before: { line: 4, hash: "XXXX" } },
|
|
754
|
-
{ after: { line: 4, hash: "XXXX" } },
|
|
755
|
-
]) {
|
|
756
|
-
const result = applyEdits(
|
|
757
|
-
text,
|
|
758
|
-
[
|
|
759
|
-
{
|
|
760
|
-
op: "copy",
|
|
761
|
-
anchor: { line: 1, hash: "XXXX" },
|
|
762
|
-
end: { line: 2, hash: "XXXX" },
|
|
763
|
-
...destination,
|
|
764
|
-
},
|
|
765
|
-
],
|
|
766
|
-
4,
|
|
767
|
-
0,
|
|
768
|
-
);
|
|
769
|
-
assert(!result.ok && result.failure.kind === "anchor");
|
|
770
|
-
assert.deepEqual(
|
|
771
|
-
result.failure.failures.map((failure) => failure.which),
|
|
772
|
-
["anchor", "end", Object.keys(destination)[0]],
|
|
773
|
-
);
|
|
774
|
-
assert(result.failure.checks.every((check) => check.status === "mismatched"));
|
|
775
|
-
}
|
|
593
|
+
test("editing beside Unicode and standalone-CR content β preserve every untouched code unit", () => {
|
|
594
|
+
const text = "πδΈζ e\u0301\nkeep\rcontent\n";
|
|
595
|
+
const result = applyEdits(text, [{ op: "append", body: ["tail"] }], 4, 0);
|
|
596
|
+
assert(result.ok);
|
|
597
|
+
assert.equal(result.text, text + "tail\n");
|
|
776
598
|
});
|
|
777
599
|
|
|
778
|
-
test("
|
|
779
|
-
const text = "A\
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
] satisfies Edit[]) {
|
|
785
|
-
const result = applyEdits(text, [edit], 4, 0);
|
|
786
|
-
assert(!result.ok && result.failure.kind === "range");
|
|
787
|
-
assert.equal(result.failure.code, "INVALID_RANGE");
|
|
788
|
-
assert(result.failure.checks.every((check) => check.status === "matched"));
|
|
789
|
-
}
|
|
600
|
+
test("appending a blank line to a terminated blank line β retain the added logical line", () => {
|
|
601
|
+
const text = "A\n\n";
|
|
602
|
+
const result = applyEdits(text, [{ op: "append", body: [""] }], 4, 0);
|
|
603
|
+
assert(result.ok);
|
|
604
|
+
assert.equal(result.text, "A\n\n\n");
|
|
605
|
+
assert.equal(splitLines(result.text).length, splitLines(text).length + 1);
|
|
790
606
|
});
|
|
791
607
|
|
|
792
|
-
test("
|
|
793
|
-
const text = "A\nB\nC\nD\
|
|
608
|
+
test("bad range anchors β aggregate anchor and end failures for every operation", () => {
|
|
609
|
+
const text = "A\nB\nC\nD\n";
|
|
610
|
+
const anchor = { line: 1, hash: "XXXX" };
|
|
611
|
+
const end = { line: 2, hash: "XXXX" };
|
|
794
612
|
const result = applyEdits(
|
|
795
613
|
text,
|
|
796
614
|
[
|
|
797
|
-
{ op: "
|
|
798
|
-
{ op: "
|
|
615
|
+
{ op: "replace", anchor, end, body: ["new"] },
|
|
616
|
+
{ op: "delete", anchor, end },
|
|
799
617
|
],
|
|
800
618
|
4,
|
|
801
619
|
0,
|
|
802
620
|
);
|
|
803
|
-
assert(!result.ok
|
|
804
|
-
assert.
|
|
805
|
-
|
|
621
|
+
assert(!result.ok);
|
|
622
|
+
assert.deepEqual(
|
|
623
|
+
result.failures.map(({ opIndex, op, which }) => ({ opIndex, op, which })),
|
|
624
|
+
[
|
|
625
|
+
{ opIndex: 0, op: "replace", which: "anchor" },
|
|
626
|
+
{ opIndex: 0, op: "replace", which: "end" },
|
|
627
|
+
{ opIndex: 1, op: "delete", which: "anchor" },
|
|
628
|
+
{ opIndex: 1, op: "delete", which: "end" },
|
|
629
|
+
],
|
|
630
|
+
);
|
|
806
631
|
});
|