@criogaid/pi-hashline-edit 0.6.1 → 0.8.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 +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
package/src/core/apply.test.ts
CHANGED
|
@@ -14,7 +14,7 @@ function at(text: string, line: number): Anchor {
|
|
|
14
14
|
|
|
15
15
|
test("replace single line", () => {
|
|
16
16
|
const text = "a\nb\nc\n";
|
|
17
|
-
const r = applyEdits(text, [{ op: "replace",
|
|
17
|
+
const r = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body: ["B"] }], 4, 15);
|
|
18
18
|
assert.equal(r.ok, true);
|
|
19
19
|
if (r.ok) assert.equal(r.text, "a\nB\nc\n");
|
|
20
20
|
});
|
|
@@ -22,7 +22,7 @@ test("replace single line", () => {
|
|
|
22
22
|
test("editing another line preserves standalone carriage returns at EOF", () => {
|
|
23
23
|
for (const ending of ["\n", "\r\n"]) {
|
|
24
24
|
const text = `first${ending}last\r`;
|
|
25
|
-
const r = applyEdits(text, [{ op: "replace",
|
|
25
|
+
const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["changed"] }], 4, 15);
|
|
26
26
|
assert.equal(r.ok, true);
|
|
27
27
|
if (r.ok) assert.equal(r.text, `changed${ending}last\r`);
|
|
28
28
|
}
|
|
@@ -32,7 +32,7 @@ test("replace range", () => {
|
|
|
32
32
|
const text = "a\nb\nc\nd\ne\n";
|
|
33
33
|
const r = applyEdits(
|
|
34
34
|
text,
|
|
35
|
-
[{ op: "replace",
|
|
35
|
+
[{ op: "replace", anchor: at(text, 2), end: at(text, 4), body: ["X", "Y"] }],
|
|
36
36
|
4,
|
|
37
37
|
15,
|
|
38
38
|
);
|
|
@@ -42,7 +42,7 @@ test("replace range", () => {
|
|
|
42
42
|
|
|
43
43
|
test("delete single line / range", () => {
|
|
44
44
|
const text = "a\nb\nc\nd\n";
|
|
45
|
-
const r = applyEdits(text, [{ op: "delete",
|
|
45
|
+
const r = applyEdits(text, [{ op: "delete", anchor: at(text, 2), end: at(text, 3) }], 4, 15);
|
|
46
46
|
assert.equal(r.ok, true);
|
|
47
47
|
if (r.ok) assert.equal(r.text, "a\nd\n");
|
|
48
48
|
});
|
|
@@ -82,7 +82,7 @@ test("multiple out-of-order ops → applied at the right positions", () => {
|
|
|
82
82
|
text,
|
|
83
83
|
[
|
|
84
84
|
{ op: "insert_after", anchor: at(text, 3), body: ["z"] },
|
|
85
|
-
{ op: "replace",
|
|
85
|
+
{ op: "replace", anchor: at(text, 1), body: ["A"] },
|
|
86
86
|
],
|
|
87
87
|
4,
|
|
88
88
|
15,
|
|
@@ -91,60 +91,13 @@ test("multiple out-of-order ops → applied at the right positions", () => {
|
|
|
91
91
|
if (r.ok) assert.equal(r.text, "A\nb\nc\nz\n");
|
|
92
92
|
});
|
|
93
93
|
|
|
94
|
-
test("touchedLines covers exactly the lines this edit produced (new-file indices)", () => {
|
|
95
|
-
const text = "a\nb\nc\n";
|
|
96
|
-
const r = applyEdits(
|
|
97
|
-
text,
|
|
98
|
-
[
|
|
99
|
-
{ op: "insert_after", anchor: at(text, 3), body: ["z"] },
|
|
100
|
-
{ op: "replace", start: at(text, 1), body: ["A"] },
|
|
101
|
-
],
|
|
102
|
-
4,
|
|
103
|
-
15,
|
|
104
|
-
);
|
|
105
|
-
assert.equal(r.ok, true);
|
|
106
|
-
if (r.ok) {
|
|
107
|
-
// new file: [A, b, c, z]; this edit produced line index 0 (A) and 3 (z)
|
|
108
|
-
assert.deepEqual([...r.touchedLines], [0, 3]);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("touchedLines for append", () => {
|
|
113
|
-
const text = "a\n";
|
|
114
|
-
const r = applyEdits(text, [{ op: "append", body: ["b", "c"] }], 4, 15);
|
|
115
|
-
assert.equal(r.ok, true);
|
|
116
|
-
if (r.ok) assert.deepEqual([...r.touchedLines], [1, 2]);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test("touchedLines for delete re-anchors the line that shifted into the gap", () => {
|
|
120
|
-
const text = "a\nb\nc\nd\n";
|
|
121
|
-
const r = applyEdits(text, [{ op: "delete", start: at(text, 2) }], 4, 15);
|
|
122
|
-
assert.equal(r.ok, true);
|
|
123
|
-
if (r.ok) {
|
|
124
|
-
// new file: [a, c, d]; deleting line 2 (b) shifts c into index 1
|
|
125
|
-
assert.deepEqual([...r.touchedLines], [1]);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
|
|
129
94
|
// --- error paths ---
|
|
130
95
|
|
|
131
|
-
test("anchor hash mismatch rejected (line changed)", () => {
|
|
132
|
-
const text = "a\nb\n";
|
|
133
|
-
const r = applyEdits(
|
|
134
|
-
text,
|
|
135
|
-
[{ op: "replace", start: { line: 1, hash: "WRONG" }, body: ["x"] }],
|
|
136
|
-
4,
|
|
137
|
-
15,
|
|
138
|
-
);
|
|
139
|
-
assert.equal(r.ok, false);
|
|
140
|
-
if (!r.ok) assert.equal(r.failure.kind, "anchor");
|
|
141
|
-
});
|
|
142
|
-
|
|
143
96
|
test("line out of range rejected", () => {
|
|
144
97
|
const text = "a\n";
|
|
145
98
|
const r = applyEdits(
|
|
146
99
|
text,
|
|
147
|
-
[{ op: "replace",
|
|
100
|
+
[{ op: "replace", anchor: { line: 5, hash: computeLineHash(1, "a", 4) }, body: ["x"] }],
|
|
148
101
|
4,
|
|
149
102
|
15,
|
|
150
103
|
);
|
|
@@ -157,7 +110,7 @@ test("anchor mismatch when the cited line's content differs (live verification)"
|
|
|
157
110
|
const text = "a\nb\n";
|
|
158
111
|
const r = applyEdits(
|
|
159
112
|
text,
|
|
160
|
-
[{ op: "replace",
|
|
113
|
+
[{ op: "replace", anchor: { line: 1, hash: computeLineHash(2, "b", 4) }, body: ["x"] }],
|
|
161
114
|
4,
|
|
162
115
|
15,
|
|
163
116
|
);
|
|
@@ -169,7 +122,7 @@ test("reverse-order range rejected", () => {
|
|
|
169
122
|
const text = "a\nb\nc\n";
|
|
170
123
|
const r = applyEdits(
|
|
171
124
|
text,
|
|
172
|
-
[{ op: "replace",
|
|
125
|
+
[{ op: "replace", anchor: at(text, 3), end: at(text, 1), body: ["x"] }],
|
|
173
126
|
4,
|
|
174
127
|
15,
|
|
175
128
|
);
|
|
@@ -177,44 +130,67 @@ test("reverse-order range rejected", () => {
|
|
|
177
130
|
if (!r.ok) assert.equal(r.failure.kind, "range");
|
|
178
131
|
});
|
|
179
132
|
|
|
180
|
-
test("overlapping edits
|
|
133
|
+
test("range starts inside a range or shares its start → reject overlapping edits", () => {
|
|
181
134
|
const text = "a\nb\nc\nd\n";
|
|
182
|
-
const
|
|
183
|
-
text,
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
135
|
+
for (const edit of [
|
|
136
|
+
{ op: "replace", anchor: at(text, 3), body: ["y"] },
|
|
137
|
+
{ op: "delete", anchor: at(text, 2) },
|
|
138
|
+
{ op: "insert_after", anchor: at(text, 2), body: ["y"] },
|
|
139
|
+
] satisfies Edit[]) {
|
|
140
|
+
const result = applyEdits(
|
|
141
|
+
text,
|
|
142
|
+
[{ op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["x"] }, edit],
|
|
143
|
+
4,
|
|
144
|
+
0,
|
|
145
|
+
);
|
|
146
|
+
assert(!result.ok && result.failure.kind === "range");
|
|
147
|
+
assert.equal(result.failure.code, "OVERLAPPING_EDITS");
|
|
148
|
+
}
|
|
193
149
|
});
|
|
194
150
|
|
|
195
|
-
test("
|
|
151
|
+
test("insertion at a replaced or deleted range start → insert before the range in either batch order", () => {
|
|
152
|
+
const text = "a\nb\nc\nd\n";
|
|
153
|
+
const insertion: Edit = { op: "insert_after", anchor: at(text, 1), body: ["x"] };
|
|
154
|
+
for (const [edit, expected] of [
|
|
155
|
+
[{ op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["B"] }, "a\nx\nB\nd\n"],
|
|
156
|
+
[{ op: "delete", anchor: at(text, 2), end: at(text, 3) }, "a\nx\nd\n"],
|
|
157
|
+
] satisfies [Edit, string][]) {
|
|
158
|
+
for (const edits of [
|
|
159
|
+
[insertion, edit],
|
|
160
|
+
[edit, insertion],
|
|
161
|
+
]) {
|
|
162
|
+
const result = applyEdits(text, edits, 4, 0);
|
|
163
|
+
assert(result.ok);
|
|
164
|
+
assert.equal(result.text, expected);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("insertions sharing a gap → preserve array order", () => {
|
|
196
170
|
const text = "a\nb\n";
|
|
197
|
-
const
|
|
198
|
-
text,
|
|
199
|
-
[
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
],
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
171
|
+
const edits: Edit[] = [
|
|
172
|
+
{ op: "insert_after", anchor: at(text, 1), body: ["x"] },
|
|
173
|
+
{ op: "insert_before", anchor: at(text, 2), body: ["y"] },
|
|
174
|
+
];
|
|
175
|
+
for (const [batch, expected] of [
|
|
176
|
+
[edits, "a\nx\ny\nb\n"],
|
|
177
|
+
[[...edits].reverse(), "a\ny\nx\nb\n"],
|
|
178
|
+
] satisfies [Edit[], string][]) {
|
|
179
|
+
const result = applyEdits(text, batch, 4, 0);
|
|
180
|
+
assert(result.ok);
|
|
181
|
+
assert.equal(result.text, expected);
|
|
182
|
+
}
|
|
208
183
|
});
|
|
209
184
|
|
|
210
|
-
test("byte-identical edits
|
|
185
|
+
test("byte-identical edits → validate anchors and preserve the source bytes", () => {
|
|
211
186
|
for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
|
|
212
187
|
const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
|
|
213
|
-
const r = applyEdits(text, [{ op: "replace",
|
|
214
|
-
assert.
|
|
188
|
+
const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: [body] }], 4, 15);
|
|
189
|
+
assert.ok(r.ok);
|
|
190
|
+
assert.equal(r.text, text);
|
|
215
191
|
const invalid = applyEdits(
|
|
216
192
|
text,
|
|
217
|
-
[{ op: "replace",
|
|
193
|
+
[{ op: "replace", anchor: at("wrong\nb\n", 1), body: [body] }],
|
|
218
194
|
4,
|
|
219
195
|
15,
|
|
220
196
|
);
|
|
@@ -228,7 +204,7 @@ test("unrelated change elsewhere does NOT block the edit (no global stale check)
|
|
|
228
204
|
const currentText = "a\nb\nCHANGED\n";
|
|
229
205
|
const r = applyEdits(
|
|
230
206
|
currentText,
|
|
231
|
-
[{ op: "replace",
|
|
207
|
+
[{ op: "replace", anchor: at(readText, 1), body: ["A"] }],
|
|
232
208
|
4,
|
|
233
209
|
15,
|
|
234
210
|
);
|
|
@@ -238,7 +214,7 @@ test("unrelated change elsewhere does NOT block the edit (no global stale check)
|
|
|
238
214
|
|
|
239
215
|
test("CRLF line endings preserved", () => {
|
|
240
216
|
const text = "a\r\nb\r\n";
|
|
241
|
-
const r = applyEdits(text, [{ op: "replace",
|
|
217
|
+
const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
|
|
242
218
|
assert.equal(r.ok, true);
|
|
243
219
|
if (r.ok) assert.equal(r.text, "A\r\nb\r\n");
|
|
244
220
|
});
|
|
@@ -247,7 +223,7 @@ test("editing a mixed-ending file preserves untouched separators", () => {
|
|
|
247
223
|
const before = "first\r\nsecond\nthird\r\n";
|
|
248
224
|
const result = applyEdits(
|
|
249
225
|
before,
|
|
250
|
-
[{ op: "replace",
|
|
226
|
+
[{ op: "replace", anchor: at(before, 2), body: ["SECOND"] }],
|
|
251
227
|
4,
|
|
252
228
|
15,
|
|
253
229
|
);
|
|
@@ -265,7 +241,7 @@ test("inserting and deleting in mixed-ending files preserves surviving separator
|
|
|
265
241
|
);
|
|
266
242
|
assert.equal(inserted.ok, true);
|
|
267
243
|
if (inserted.ok) assert.equal(inserted.text, "a\r\nb\nx\r\nc\r\nlast");
|
|
268
|
-
const deleted = applyEdits(before, [{ op: "delete",
|
|
244
|
+
const deleted = applyEdits(before, [{ op: "delete", anchor: at(before, 2) }], 4, 15);
|
|
269
245
|
assert.equal(deleted.ok, true);
|
|
270
246
|
if (deleted.ok) assert.equal(deleted.text, "a\r\nc\r\nlast");
|
|
271
247
|
});
|
|
@@ -274,7 +250,7 @@ test("replacing one line with several keeps the block's trailing gap", () => {
|
|
|
274
250
|
const before = "a\r\nb\nc\r\nd";
|
|
275
251
|
const result = applyEdits(
|
|
276
252
|
before,
|
|
277
|
-
[{ op: "replace",
|
|
253
|
+
[{ op: "replace", anchor: at(before, 2), body: ["X", "Y", "Z"] }],
|
|
278
254
|
4,
|
|
279
255
|
15,
|
|
280
256
|
);
|
|
@@ -286,14 +262,14 @@ test("replacing one line with several keeps the block's trailing gap", () => {
|
|
|
286
262
|
|
|
287
263
|
test("a file without a final newline keeps not having one", () => {
|
|
288
264
|
const text = "a\nb";
|
|
289
|
-
const r = applyEdits(text, [{ op: "replace",
|
|
265
|
+
const r = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body: ["B"] }], 4, 15);
|
|
290
266
|
assert.equal(r.ok, true);
|
|
291
267
|
if (r.ok) assert.equal(r.text, "a\nB");
|
|
292
268
|
});
|
|
293
269
|
|
|
294
270
|
test("CRLF without a final newline keeps not having one", () => {
|
|
295
271
|
const text = "a\r\nb";
|
|
296
|
-
const r = applyEdits(text, [{ op: "replace",
|
|
272
|
+
const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
|
|
297
273
|
assert.equal(r.ok, true);
|
|
298
274
|
if (r.ok) assert.equal(r.text, "A\r\nb");
|
|
299
275
|
});
|
|
@@ -310,110 +286,92 @@ test("edits retain empty final logical lines even without an original final newl
|
|
|
310
286
|
const line = splitLines(text).length;
|
|
311
287
|
const ending = text.includes("\r\n") ? "\r\n" : "\n";
|
|
312
288
|
const prefix = text.slice(0, text.lastIndexOf("\n") + 1);
|
|
313
|
-
const cases: [Edit, string
|
|
314
|
-
[{ op: "replace",
|
|
289
|
+
const cases: [Edit, string][] = [
|
|
290
|
+
[{ op: "replace", anchor: at(text, line), body: [""] }, prefix + ending],
|
|
315
291
|
[
|
|
316
|
-
{ op: "replace",
|
|
292
|
+
{ op: "replace", anchor: at(text, line), body: ["new", ""] },
|
|
317
293
|
`${prefix}new${ending}${ending}`,
|
|
318
|
-
[line - 1, line],
|
|
319
294
|
],
|
|
320
|
-
[{ op: "append", body: [""] }, text + ending + ending
|
|
321
|
-
[{ op: "insert_after", anchor: at(text, line), body: [""] }, text + ending + ending
|
|
295
|
+
[{ op: "append", body: [""] }, text + ending + ending],
|
|
296
|
+
[{ op: "insert_after", anchor: at(text, line), body: [""] }, text + ending + ending],
|
|
322
297
|
];
|
|
323
|
-
for (const [edit, expected
|
|
298
|
+
for (const [edit, expected] of cases) {
|
|
324
299
|
const result = applyEdits(text, [edit], 4, 15);
|
|
325
300
|
assert.ok(result.ok);
|
|
326
301
|
assert.equal(result.text, expected, `${edit.op}: ${JSON.stringify(text)}`);
|
|
327
|
-
assert.deepEqual(result.touchedLines, touched);
|
|
328
|
-
const newLines = splitLines(result.text);
|
|
329
|
-
assert.ok(result.touchedLines.every((index) => index < newLines.length));
|
|
330
302
|
}
|
|
331
303
|
}
|
|
332
304
|
// Deleting an unterminated suffix must also retain an existing blank predecessor.
|
|
333
305
|
for (const text of ["\nlast", "head\r\n\r\nlast"]) {
|
|
334
306
|
const result = applyEdits(
|
|
335
307
|
text,
|
|
336
|
-
[{ op: "delete",
|
|
308
|
+
[{ op: "delete", anchor: at(text, splitLines(text).length) }],
|
|
337
309
|
4,
|
|
338
310
|
15,
|
|
339
311
|
);
|
|
340
312
|
assert.ok(result.ok);
|
|
341
313
|
assert.equal(result.text, text.slice(0, -"last".length));
|
|
342
|
-
assert.deepEqual(result.touchedLines, []);
|
|
343
314
|
}
|
|
344
315
|
});
|
|
345
316
|
|
|
346
317
|
test("a BOM represents an empty first line without a terminator, but not an empty later line", () => {
|
|
347
|
-
const cases: [string, Edit[], string
|
|
348
|
-
["\uFEFFold", [{ op: "replace",
|
|
349
|
-
["\uFEFF", [{ op: "replace",
|
|
350
|
-
["\uFEFF", [{ op: "append", body: [""] }], "\uFEFF\n\n"
|
|
351
|
-
["\uFEFF", [{ op: "prepend", body: ["new"] }], "\uFEFFnew\n\n"
|
|
352
|
-
["\uFEFF\nlast", [{ op: "delete",
|
|
318
|
+
const cases: [string, Edit[], string][] = [
|
|
319
|
+
["\uFEFFold", [{ op: "replace", anchor: at("\uFEFFold", 1), body: [""] }], "\uFEFF"],
|
|
320
|
+
["\uFEFF", [{ op: "replace", anchor: at("\uFEFF", 1), body: [""] }], "\uFEFF"],
|
|
321
|
+
["\uFEFF", [{ op: "append", body: [""] }], "\uFEFF\n\n"],
|
|
322
|
+
["\uFEFF", [{ op: "prepend", body: ["new"] }], "\uFEFFnew\n\n"],
|
|
323
|
+
["\uFEFF\nlast", [{ op: "delete", anchor: at("\uFEFF\nlast", 2) }], "\uFEFF"],
|
|
353
324
|
];
|
|
354
|
-
for (const [text, edits, expected
|
|
325
|
+
for (const [text, edits, expected] of cases) {
|
|
355
326
|
const result = applyEdits(text, edits, 4, 15);
|
|
356
327
|
assert.ok(result.ok);
|
|
357
328
|
assert.equal(result.text, expected, JSON.stringify({ text, edits }));
|
|
358
|
-
assert.deepEqual(result.touchedLines, touched);
|
|
359
329
|
}
|
|
360
330
|
});
|
|
361
331
|
|
|
362
332
|
test("large replacement bodies retain snapshot boundaries without a function argument limit", () => {
|
|
363
333
|
const text = "head\r\nold\ntail";
|
|
364
334
|
const body = Array<string>(150_000).fill("x");
|
|
365
|
-
const result = applyEdits(text, [{ op: "replace",
|
|
335
|
+
const result = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body }], 4, 15);
|
|
366
336
|
assert.ok(result.ok);
|
|
367
337
|
assert.equal(result.text, `head\r\n${"x\r\n".repeat(body.length - 1)}x\ntail`);
|
|
368
|
-
assert.equal(result.touchedLines.length, body.length);
|
|
369
|
-
assert.equal(result.touchedLines[0], 1);
|
|
370
|
-
assert.equal(result.touchedLines.at(-1), body.length);
|
|
371
|
-
assert.deepEqual(result.contextLines, []);
|
|
372
338
|
});
|
|
373
339
|
|
|
374
340
|
test("noop is still detected when the file lacks a final newline", () => {
|
|
375
341
|
const text = "a\nb";
|
|
376
|
-
const r = applyEdits(text, [{ op: "replace",
|
|
377
|
-
assert.
|
|
342
|
+
const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["a"] }], 4, 15);
|
|
343
|
+
assert.ok(r.ok);
|
|
344
|
+
assert.equal(r.text, text);
|
|
378
345
|
});
|
|
379
346
|
|
|
380
347
|
// --- shifted-anchor recovery ---
|
|
381
348
|
|
|
382
|
-
test("shifted recovery: content moved down → found with a fresh anchor", () => {
|
|
383
|
-
const readText = "a\nb\nc\nd\ne\n";
|
|
384
|
-
const currentText = "a\nX\nb\nc\nd\ne\n"; // inserted X after line 1 → "c" moved 3→4
|
|
385
|
-
const r = applyEdits(
|
|
386
|
-
currentText,
|
|
387
|
-
[{ op: "replace", start: at(readText, 3), body: ["C"] }],
|
|
388
|
-
4,
|
|
389
|
-
15,
|
|
390
|
-
);
|
|
391
|
-
assert.ok(!r.ok && r.failure.kind === "anchor");
|
|
392
|
-
const f = r.failure.failures[0];
|
|
393
|
-
assert.ok(f.recovery.kind === "found");
|
|
394
|
-
assert.equal(f.recovery.newLine, 4);
|
|
395
|
-
assert.equal(f.recovery.scope, "local");
|
|
396
|
-
// The recovered anchor must verify against the current file.
|
|
397
|
-
assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
|
|
398
|
-
});
|
|
399
|
-
|
|
400
349
|
test("rescued anchor lets the retry succeed without a re-read", () => {
|
|
401
350
|
const readText = "a\nb\nc\nd\ne\n";
|
|
402
351
|
const currentText = "a\nX\nb\nc\nd\ne\n";
|
|
403
352
|
// first attempt with a stale anchor → rescued
|
|
404
353
|
const r1 = applyEdits(
|
|
405
354
|
currentText,
|
|
406
|
-
[{ op: "replace",
|
|
355
|
+
[{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
|
|
407
356
|
4,
|
|
408
357
|
15,
|
|
409
358
|
);
|
|
410
359
|
assert.ok(!r1.ok && r1.failure.kind === "anchor");
|
|
411
360
|
const f = r1.failure.failures[0];
|
|
412
361
|
assert.ok(f.recovery.kind === "found");
|
|
362
|
+
assert.equal(f.recovery.newLine, 4);
|
|
363
|
+
assert.equal(f.recovery.scope, "local");
|
|
364
|
+
assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
|
|
413
365
|
// Retry with the recovered anchor; the rest of the file stays unchanged.
|
|
414
366
|
const r2 = applyEdits(
|
|
415
367
|
currentText,
|
|
416
|
-
[
|
|
368
|
+
[
|
|
369
|
+
{
|
|
370
|
+
op: "replace",
|
|
371
|
+
anchor: { line: f.recovery.newLine, hash: f.recovery.newHash },
|
|
372
|
+
body: ["C"],
|
|
373
|
+
},
|
|
374
|
+
],
|
|
417
375
|
4,
|
|
418
376
|
15,
|
|
419
377
|
);
|
|
@@ -426,7 +384,7 @@ test("shifted recovery: duplicate content → ambiguous candidates", () => {
|
|
|
426
384
|
const currentText = "a\nY\nx\nb\nx\nc\n"; // both "x" shifted
|
|
427
385
|
const r = applyEdits(
|
|
428
386
|
currentText,
|
|
429
|
-
[{ op: "replace",
|
|
387
|
+
[{ op: "replace", anchor: at(readText, 2), body: ["Z"] }],
|
|
430
388
|
4,
|
|
431
389
|
15,
|
|
432
390
|
);
|
|
@@ -445,7 +403,7 @@ test("shifted recovery: content genuinely changed → none, with live content",
|
|
|
445
403
|
const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
|
|
446
404
|
const r = applyEdits(
|
|
447
405
|
currentText,
|
|
448
|
-
[{ op: "replace",
|
|
406
|
+
[{ op: "replace", anchor: at(readText, 2), body: ["B"] }],
|
|
449
407
|
4,
|
|
450
408
|
15,
|
|
451
409
|
);
|
|
@@ -465,7 +423,7 @@ test("full-file recovery finds distant content and anchors beyond the current EO
|
|
|
465
423
|
lines[newLine - 1] = "target";
|
|
466
424
|
const result = applyEdits(
|
|
467
425
|
lines.join("\n"),
|
|
468
|
-
[{ op: "delete",
|
|
426
|
+
[{ op: "delete", anchor: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
|
|
469
427
|
4,
|
|
470
428
|
15,
|
|
471
429
|
);
|
|
@@ -489,7 +447,7 @@ test("full-file recovery collects both sides while preserving local candidate pr
|
|
|
489
447
|
for (const line of positions) lines[line - 1] = "target";
|
|
490
448
|
const result = applyEdits(
|
|
491
449
|
lines.join("\n"),
|
|
492
|
-
[{ op: "delete",
|
|
450
|
+
[{ op: "delete", anchor: { line: 50, hash: computeLineHash(50, "target", 4) } }],
|
|
493
451
|
4,
|
|
494
452
|
15,
|
|
495
453
|
);
|
|
@@ -521,8 +479,8 @@ test("collect-all: two stale anchors in one batch → both failures returned", (
|
|
|
521
479
|
const r = applyEdits(
|
|
522
480
|
currentText,
|
|
523
481
|
[
|
|
524
|
-
{ op: "replace",
|
|
525
|
-
{ op: "replace",
|
|
482
|
+
{ op: "replace", anchor: at(readText, 2), body: ["B"] },
|
|
483
|
+
{ op: "replace", anchor: at(readText, 4), body: ["D"] },
|
|
526
484
|
],
|
|
527
485
|
4,
|
|
528
486
|
15,
|
|
@@ -538,7 +496,12 @@ test("collect-all: two stale anchors in one batch → both failures returned", (
|
|
|
538
496
|
test("shiftRadius=0 disables rescue (always none)", () => {
|
|
539
497
|
const readText = "a\nb\nc\nd\ne\n";
|
|
540
498
|
const currentText = "a\nX\nb\nc\nd\ne\n";
|
|
541
|
-
const r = applyEdits(
|
|
499
|
+
const r = applyEdits(
|
|
500
|
+
currentText,
|
|
501
|
+
[{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
|
|
502
|
+
4,
|
|
503
|
+
0,
|
|
504
|
+
);
|
|
542
505
|
assert.ok(!r.ok && r.failure.kind === "anchor");
|
|
543
506
|
assert.equal(r.failure.failures[0].recovery.kind, "none");
|
|
544
507
|
});
|
|
@@ -553,18 +516,18 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
|
|
|
553
516
|
const ending = text.includes("\r\n") ? "\r\n" : "\n";
|
|
554
517
|
const rest = text.slice(text.indexOf("\n") + 1);
|
|
555
518
|
const cases: [Edit, string][] = [
|
|
556
|
-
[{ op: "replace",
|
|
519
|
+
[{ op: "replace", anchor: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
|
|
557
520
|
[
|
|
558
|
-
{ op: "replace",
|
|
521
|
+
{ op: "replace", anchor: at(text, 1), body: ["\uFEFFchanged"] },
|
|
559
522
|
`\uFEFFchanged${ending}${rest}`,
|
|
560
523
|
],
|
|
561
|
-
[{ op: "delete",
|
|
524
|
+
[{ op: "delete", anchor: at(text, 1) }, `\uFEFF${rest}`],
|
|
562
525
|
[{ op: "prepend", body: ["new"] }, `\uFEFFnew${ending}${text.slice(1)}`],
|
|
563
526
|
[
|
|
564
527
|
{ op: "insert_before", anchor: at(text, 1), body: ["new"] },
|
|
565
528
|
`\uFEFFnew${ending}${text.slice(1)}`,
|
|
566
529
|
],
|
|
567
|
-
[{ op: "delete",
|
|
530
|
+
[{ op: "delete", anchor: at(text, 1), end: at(text, 2) }, "\uFEFF"],
|
|
568
531
|
];
|
|
569
532
|
for (const [edit, expected] of cases) {
|
|
570
533
|
const result = applyEdits(text, [edit], 4, 15);
|
|
@@ -575,7 +538,7 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
|
|
|
575
538
|
for (const text of ["\uFEFF", "\uFEFFfirst"]) {
|
|
576
539
|
const result = applyEdits(
|
|
577
540
|
text,
|
|
578
|
-
[{ op: "replace",
|
|
541
|
+
[{ op: "replace", anchor: at(text, 1), body: ["changed"] }],
|
|
579
542
|
4,
|
|
580
543
|
15,
|
|
581
544
|
);
|
|
@@ -585,7 +548,7 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
|
|
|
585
548
|
const embedded = "\uFEFFfirst\ninside\uFEFFcontent\n";
|
|
586
549
|
const result = applyEdits(
|
|
587
550
|
embedded,
|
|
588
|
-
[{ op: "replace",
|
|
551
|
+
[{ op: "replace", anchor: at(embedded, 1), body: ["changed"] }],
|
|
589
552
|
4,
|
|
590
553
|
15,
|
|
591
554
|
);
|
|
@@ -597,8 +560,8 @@ test("failed batches report every supplied anchor in input order from one snapsh
|
|
|
597
560
|
const text = "a\nb\nc\nd\ne\nf\n";
|
|
598
561
|
const stale = { line: 2, hash: "ZZ" };
|
|
599
562
|
const edits: Edit[] = [
|
|
600
|
-
{ op: "replace",
|
|
601
|
-
{ op: "delete",
|
|
563
|
+
{ op: "replace", anchor: at(text, 1), end: stale, body: ["A"] },
|
|
564
|
+
{ op: "delete", anchor: stale, end: at(text, 4) },
|
|
602
565
|
{ op: "insert_after", anchor: at(text, 5), body: ["E"] },
|
|
603
566
|
{ op: "insert_before", anchor: stale, body: ["B"] },
|
|
604
567
|
{ op: "append", body: ["last"] },
|
|
@@ -607,23 +570,23 @@ test("failed batches report every supplied anchor in input order from one snapsh
|
|
|
607
570
|
const result = applyEdits(text, edits, 4, 15);
|
|
608
571
|
assert.ok(!result.ok && result.failure.kind === "anchor");
|
|
609
572
|
assert.deepEqual(result.failure.checks, [
|
|
610
|
-
{ opIndex: 0,
|
|
611
|
-
{ opIndex: 0,
|
|
612
|
-
{ opIndex: 1,
|
|
613
|
-
{ opIndex: 1,
|
|
614
|
-
{ opIndex: 2,
|
|
615
|
-
{ opIndex: 3,
|
|
573
|
+
{ opIndex: 0, which: "anchor", status: "matched" },
|
|
574
|
+
{ opIndex: 0, which: "end", status: "mismatched" },
|
|
575
|
+
{ opIndex: 1, which: "anchor", status: "mismatched" },
|
|
576
|
+
{ opIndex: 1, which: "end", status: "matched" },
|
|
577
|
+
{ opIndex: 2, which: "anchor", status: "matched" },
|
|
578
|
+
{ opIndex: 3, which: "anchor", status: "mismatched" },
|
|
616
579
|
]);
|
|
617
580
|
});
|
|
618
581
|
|
|
619
582
|
test("matched anchor checks do not imply valid ranges", () => {
|
|
620
583
|
const text = "a\nb\nc\n";
|
|
621
584
|
const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
|
|
622
|
-
{ edits: [{ op: "delete",
|
|
585
|
+
{ edits: [{ op: "delete", anchor: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
|
|
623
586
|
{
|
|
624
587
|
edits: [
|
|
625
|
-
{ op: "replace",
|
|
626
|
-
{ op: "delete",
|
|
588
|
+
{ op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["x"] },
|
|
589
|
+
{ op: "delete", anchor: at(text, 2) },
|
|
627
590
|
],
|
|
628
591
|
kind: "range",
|
|
629
592
|
checks: 3,
|
|
@@ -637,3 +600,207 @@ test("matched anchor checks do not imply valid ranges", () => {
|
|
|
637
600
|
assert.ok(result.failure.checks.every((check) => check.status === "matched"));
|
|
638
601
|
}
|
|
639
602
|
});
|
|
603
|
+
|
|
604
|
+
test("copy/move original ranges → retain captured text and order", () => {
|
|
605
|
+
const text = "A\nB\nC\nD\n";
|
|
606
|
+
const cases: { edit: Edit; expected: string }[] = [
|
|
607
|
+
{
|
|
608
|
+
edit: { op: "copy", anchor: at(text, 2), end: at(text, 3), after: at(text, 4) },
|
|
609
|
+
expected: "A\nB\nC\nD\nB\nC\n",
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
edit: { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) },
|
|
613
|
+
expected: "C\nD\nA\nB\n",
|
|
614
|
+
},
|
|
615
|
+
{
|
|
616
|
+
edit: { op: "move", anchor: at(text, 3), end: at(text, 4), before: at(text, 1) },
|
|
617
|
+
expected: "C\nD\nA\nB\n",
|
|
618
|
+
},
|
|
619
|
+
{
|
|
620
|
+
edit: { op: "copy", anchor: at(text, 1), end: at(text, 3), after: at(text, 1) },
|
|
621
|
+
expected: "A\nA\nB\nC\nB\nC\nD\n",
|
|
622
|
+
},
|
|
623
|
+
];
|
|
624
|
+
for (const { edit, expected } of cases) {
|
|
625
|
+
const result = applyEdits(text, [edit], 4, 0);
|
|
626
|
+
assert(result.ok);
|
|
627
|
+
assert.equal(result.text, expected);
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
test("copy source also replaced or deleted → capture original text regardless of batch order", () => {
|
|
632
|
+
const text = "A\nB\nC\nD\n";
|
|
633
|
+
const copy: Edit = { op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) };
|
|
634
|
+
for (const edit of [
|
|
635
|
+
{ op: "replace", anchor: at(text, 1), end: at(text, 2), body: ["NEW"] },
|
|
636
|
+
{ op: "delete", anchor: at(text, 1), end: at(text, 2) },
|
|
637
|
+
] satisfies Edit[]) {
|
|
638
|
+
for (const edits of [
|
|
639
|
+
[copy, edit],
|
|
640
|
+
[edit, copy],
|
|
641
|
+
]) {
|
|
642
|
+
const result = applyEdits(text, edits, 4, 0);
|
|
643
|
+
assert(result.ok);
|
|
644
|
+
assert.equal(result.text, edit.op === "replace" ? "NEW\nC\nD\nA\nB\n" : "C\nD\nA\nB\n");
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
test("copy/move first-line mixed-ending range → preserve source gaps and keep one file BOM", () => {
|
|
650
|
+
const text = "\uFEFFA\r\nB\nC\r\nD\n";
|
|
651
|
+
for (const op of ["copy", "move"] as const) {
|
|
652
|
+
const result = applyEdits(
|
|
653
|
+
text,
|
|
654
|
+
[{ op, anchor: at(text, 1), end: at(text, 3), after: at(text, 4) }],
|
|
655
|
+
4,
|
|
656
|
+
0,
|
|
657
|
+
);
|
|
658
|
+
assert(result.ok);
|
|
659
|
+
assert.equal(result.text, op === "copy" ? text + "A\r\nB\nC\r\n" : "\uFEFFD\nA\r\nB\nC\r\n");
|
|
660
|
+
}
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
test("copy/move unterminated final line → add interior connectors and preserve final-newline state", () => {
|
|
664
|
+
for (const ending of ["\n", "\r\n"]) {
|
|
665
|
+
const text = `A${ending}B`;
|
|
666
|
+
for (const op of ["copy", "move"] as const) {
|
|
667
|
+
const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
|
|
668
|
+
assert(result.ok);
|
|
669
|
+
assert.equal(result.text, op === "copy" ? `B${ending}A${ending}B` : `B${ending}A`);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
test("copy/move line ending in standalone CR → connector retains the CR as line content", () => {
|
|
675
|
+
const text = "A\nB\r";
|
|
676
|
+
for (const op of ["copy", "move"] as const) {
|
|
677
|
+
const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
|
|
678
|
+
assert(result.ok);
|
|
679
|
+
assert.equal(result.text, op === "copy" ? "B\r\r\nA\nB\r" : "B\r\r\nA");
|
|
680
|
+
assert.equal(splitLines(result.text)[0], "B\r");
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
test("copy Unicode and standalone-CR content → preserve every source code unit", () => {
|
|
685
|
+
const text = "🙂中文 e\u0301\nkeep\rcontent\n";
|
|
686
|
+
const result = applyEdits(
|
|
687
|
+
text,
|
|
688
|
+
[{ op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 2) }],
|
|
689
|
+
4,
|
|
690
|
+
0,
|
|
691
|
+
);
|
|
692
|
+
assert(result.ok);
|
|
693
|
+
assert.equal(result.text, text + text);
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
test("copy blank or BOM-only line → retain the added logical blank line", () => {
|
|
697
|
+
for (const [text, line, expected] of [
|
|
698
|
+
["A\n\n", 2, "A\n\n\n"],
|
|
699
|
+
["\uFEFF", 1, "\uFEFF\n\n"],
|
|
700
|
+
] as const) {
|
|
701
|
+
const result = applyEdits(
|
|
702
|
+
text,
|
|
703
|
+
[{ op: "copy", anchor: at(text, line), after: at(text, line) }],
|
|
704
|
+
4,
|
|
705
|
+
0,
|
|
706
|
+
);
|
|
707
|
+
assert(result.ok);
|
|
708
|
+
assert.equal(result.text, expected);
|
|
709
|
+
assert.equal(splitLines(result.text).length, splitLines(text).length + 1);
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
test("move to either adjacent gap → byte-identical no-op", () => {
|
|
714
|
+
const text = "A\r\nB\nC\r\nD";
|
|
715
|
+
for (const destination of [{ before: at(text, 2) }, { after: at(text, 3) }]) {
|
|
716
|
+
const result = applyEdits(
|
|
717
|
+
text,
|
|
718
|
+
[{ op: "move", anchor: at(text, 2), end: at(text, 3), ...destination }],
|
|
719
|
+
4,
|
|
720
|
+
0,
|
|
721
|
+
);
|
|
722
|
+
assert(result.ok);
|
|
723
|
+
assert.equal(result.text, text);
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
test("copy/move bad source and destination anchors → aggregate every supplied field", () => {
|
|
728
|
+
const text = "A\nB\nC\nD\n";
|
|
729
|
+
for (const destination of [
|
|
730
|
+
{ before: { line: 4, hash: "XXXX" } },
|
|
731
|
+
{ after: { line: 4, hash: "XXXX" } },
|
|
732
|
+
]) {
|
|
733
|
+
const result = applyEdits(
|
|
734
|
+
text,
|
|
735
|
+
[
|
|
736
|
+
{
|
|
737
|
+
op: "copy",
|
|
738
|
+
anchor: { line: 1, hash: "XXXX" },
|
|
739
|
+
end: { line: 2, hash: "XXXX" },
|
|
740
|
+
...destination,
|
|
741
|
+
},
|
|
742
|
+
],
|
|
743
|
+
4,
|
|
744
|
+
0,
|
|
745
|
+
);
|
|
746
|
+
assert(!result.ok && result.failure.kind === "anchor");
|
|
747
|
+
assert.deepEqual(
|
|
748
|
+
result.failure.failures.map((failure) => failure.which),
|
|
749
|
+
["anchor", "end", Object.keys(destination)[0]],
|
|
750
|
+
);
|
|
751
|
+
assert(result.failure.checks.every((check) => check.status === "mismatched"));
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
test("copy/move captures sharing a gap → preserve original captures in array order", () => {
|
|
756
|
+
const text = "A\nB\nC\nD\nE\n";
|
|
757
|
+
for (const [first, second, remaining] of [
|
|
758
|
+
["copy", "copy", text],
|
|
759
|
+
["copy", "move", "A\nC\nD\nE\n"],
|
|
760
|
+
["move", "copy", "B\nC\nD\nE\n"],
|
|
761
|
+
["move", "move", "C\nD\nE\n"],
|
|
762
|
+
] as const) {
|
|
763
|
+
const edits: Edit[] = [
|
|
764
|
+
{ op: first, anchor: at(text, 1), after: at(text, 5) },
|
|
765
|
+
{ op: second, anchor: at(text, 2), after: at(text, 5) },
|
|
766
|
+
];
|
|
767
|
+
for (const [batch, suffix] of [
|
|
768
|
+
[edits, "A\nB\n"],
|
|
769
|
+
[[...edits].reverse(), "B\nA\n"],
|
|
770
|
+
] satisfies [Edit[], string][]) {
|
|
771
|
+
const result = applyEdits(text, batch, 4, 0);
|
|
772
|
+
assert(result.ok);
|
|
773
|
+
assert.equal(result.text, remaining + suffix);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
test("move interior destination or reversed capture range → reject invalid ranges", () => {
|
|
779
|
+
const text = "A\nB\nC\nD\nE\n";
|
|
780
|
+
for (const edit of [
|
|
781
|
+
{ op: "move", anchor: at(text, 1), end: at(text, 3), after: at(text, 2) },
|
|
782
|
+
{ op: "move", anchor: at(text, 1), end: at(text, 3), before: at(text, 2) },
|
|
783
|
+
{ op: "copy", anchor: at(text, 3), end: at(text, 2), after: at(text, 5) },
|
|
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
|
+
}
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
test("overlapping move sources → reject the complete batch", () => {
|
|
793
|
+
const text = "A\nB\nC\nD\nE\n";
|
|
794
|
+
const result = applyEdits(
|
|
795
|
+
text,
|
|
796
|
+
[
|
|
797
|
+
{ op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 5) },
|
|
798
|
+
{ op: "move", anchor: at(text, 2), end: at(text, 3), before: at(text, 1) },
|
|
799
|
+
],
|
|
800
|
+
4,
|
|
801
|
+
0,
|
|
802
|
+
);
|
|
803
|
+
assert(!result.ok && result.failure.kind === "range");
|
|
804
|
+
assert.equal(result.failure.code, "OVERLAPPING_EDITS");
|
|
805
|
+
assert(result.failure.checks.every((check) => check.status === "matched"));
|
|
806
|
+
});
|