@jerryan/pi-hashline-edit 0.7.4 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/edit-diff.ts CHANGED
@@ -1,390 +1,201 @@
1
- import * as Diff from "diff";
2
- import {
3
- computeLineHash,
4
- FUZZY_HYPHENS_RE,
5
- FUZZY_DOUBLE_QUOTES_RE,
6
- FUZZY_SINGLE_QUOTES_RE,
7
- FUZZY_UNICODE_SPACES_RE,
8
- ANCHOR_SEP,
9
- CONTENT_SEP,
10
- } from "./hashline";
11
-
12
- // ─── Line ending normalization ──────────────────────────────────────────
13
-
14
- export function detectLineEnding(content: string): "\r\n" | "\n" {
15
- const crlfIdx = content.indexOf("\r\n");
16
- const lfIdx = content.indexOf("\n");
17
- if (lfIdx === -1 || crlfIdx === -1) return "\n";
18
- return crlfIdx < lfIdx ? "\r\n" : "\n";
19
- }
20
-
21
- export function normalizeToLF(text: string): string {
22
- return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
23
- }
24
-
25
- export function restoreLineEndings(
26
- text: string,
27
- ending: "\r\n" | "\n",
28
- ): string {
29
- return ending === "\r\n" ? text.replace(/\n/g, "\r\n") : text;
30
- }
31
-
32
- export function stripBom(content: string): { bom: string; text: string } {
33
- return content.startsWith("\uFEFF")
34
- ? { bom: "\uFEFF", text: content.slice(1) }
35
- : { bom: "", text: content };
36
- }
37
-
38
- // ─── Fuzzy text matching ────────────────────────────────────────────────
39
-
40
- function normalizeFuzzyChar(ch: string): string {
41
- return ch
42
- .replace(FUZZY_SINGLE_QUOTES_RE, "'")
43
- .replace(FUZZY_DOUBLE_QUOTES_RE, '"')
44
- .replace(FUZZY_HYPHENS_RE, "-")
45
- .replace(FUZZY_UNICODE_SPACES_RE, " ");
46
- }
47
-
48
- function normalizeForFuzzyMatch(text: string): string {
49
- return text
50
- .split("\n")
51
- .map((line) => line.trimEnd())
52
- .join("\n")
53
- .replace(FUZZY_SINGLE_QUOTES_RE, "'")
54
- .replace(FUZZY_DOUBLE_QUOTES_RE, '"')
55
- .replace(FUZZY_HYPHENS_RE, "-")
56
- .replace(FUZZY_UNICODE_SPACES_RE, " ");
57
- }
58
-
59
- function buildNormalizedWithMap(text: string): {
60
- normalized: string;
61
- indexMap: number[];
62
- } {
63
- const lines = text.split("\n");
64
- const normalizedChars: string[] = [];
65
- const indexMap: number[] = [];
66
- let originalOffset = 0;
67
-
68
- for (let i = 0; i < lines.length; i++) {
69
- const line = lines[i]!;
70
- const trimmed = line.replace(/\s+$/u, "");
71
-
72
- for (let j = 0; j < trimmed.length; j++) {
73
- normalizedChars.push(normalizeFuzzyChar(trimmed[j]!));
74
- indexMap.push(originalOffset + j);
75
- }
76
-
77
- if (i < lines.length - 1) {
78
- normalizedChars.push("\n");
79
- indexMap.push(originalOffset + line.length);
80
- }
81
-
82
- originalOffset += line.length + 1;
83
- }
84
-
85
- return { normalized: normalizedChars.join(""), indexMap };
86
- }
87
-
88
- function mapNormalizedSpanToOriginal(
89
- indexMap: number[],
90
- normalizedStart: number,
91
- normalizedLength: number,
92
- ): { index: number; matchLength: number } | null {
93
- if (normalizedStart < 0 || normalizedLength <= 0) return null;
94
- const normalizedEnd = normalizedStart + normalizedLength;
95
- if (normalizedEnd > indexMap.length) return null;
96
-
97
- const start = indexMap[normalizedStart];
98
- const end = indexMap[normalizedEnd - 1];
99
- if (start === undefined || end === undefined || end < start) return null;
100
-
101
- return { index: start, matchLength: end - start + 1 };
102
- }
103
-
104
- /**
105
- * Find `oldText` in `content` with optional fuzzy whitespace/unicode matching.
106
- * Always returns an index/length in the original content.
107
- */
108
- export function fuzzyFindText(
109
- content: string,
110
- oldText: string,
111
- ): {
112
- found: boolean;
113
- index: number;
114
- matchLength: number;
115
- usedFuzzyMatch: boolean;
116
- } {
117
- const exactIndex = content.indexOf(oldText);
118
- if (exactIndex !== -1) {
119
- return {
120
- found: true,
121
- index: exactIndex,
122
- matchLength: oldText.length,
123
- usedFuzzyMatch: false,
124
- };
125
- }
126
-
127
- const normalizedNeedle = normalizeForFuzzyMatch(oldText);
128
- if (!normalizedNeedle.length)
129
- return { found: false, index: -1, matchLength: 0, usedFuzzyMatch: false };
130
-
131
- const { normalized, indexMap } = buildNormalizedWithMap(content);
132
- const normalizedIndex = normalized.indexOf(normalizedNeedle);
133
- if (normalizedIndex === -1) {
134
- return { found: false, index: -1, matchLength: 0, usedFuzzyMatch: false };
135
- }
136
-
137
- const mapped = mapNormalizedSpanToOriginal(
138
- indexMap,
139
- normalizedIndex,
140
- normalizedNeedle.length,
141
- );
142
- if (!mapped) {
143
- return { found: false, index: -1, matchLength: 0, usedFuzzyMatch: false };
144
- }
145
-
146
- return {
147
- found: true,
148
- index: mapped.index,
149
- matchLength: mapped.matchLength,
150
- usedFuzzyMatch: true,
151
- };
152
- }
153
-
154
- /**
155
- * Replace `oldText` with `newText` in `content`.
156
- * Fuzzy matching only determines target spans; replacement always applies to
157
- * the original content (never normalizes the whole file).
158
- */
159
- export function replaceText(
160
- content: string,
161
- oldText: string,
162
- newText: string,
163
- opts: { all?: boolean },
164
- ): { content: string; count: number } {
165
- if (!oldText.length) return { content, count: 0 };
166
- const normalizedNew = normalizeToLF(newText);
167
-
168
- if (opts.all) {
169
- const exactCount = content.split(oldText).length - 1;
170
- if (exactCount > 0) {
171
- return {
172
- content: content.split(oldText).join(normalizedNew),
173
- count: exactCount,
174
- };
175
- }
176
-
177
- const normalizedNeedle = normalizeForFuzzyMatch(oldText);
178
- if (!normalizedNeedle.length) return { content, count: 0 };
179
-
180
- const { normalized, indexMap } = buildNormalizedWithMap(content);
181
- const spans: Array<{ index: number; matchLength: number }> = [];
182
- let searchFrom = 0;
183
-
184
- while (searchFrom <= normalized.length - normalizedNeedle.length) {
185
- const pos = normalized.indexOf(normalizedNeedle, searchFrom);
186
- if (pos === -1) break;
187
- const mapped = mapNormalizedSpanToOriginal(
188
- indexMap,
189
- pos,
190
- normalizedNeedle.length,
191
- );
192
- if (mapped) {
193
- const prev = spans[spans.length - 1];
194
- if (!prev || mapped.index >= prev.index + prev.matchLength) {
195
- spans.push(mapped);
196
- }
197
- }
198
- searchFrom = pos + Math.max(1, normalizedNeedle.length);
199
- }
200
-
201
- if (!spans.length) return { content, count: 0 };
202
-
203
- let out = content;
204
- for (let i = spans.length - 1; i >= 0; i--) {
205
- const span = spans[i]!;
206
- out =
207
- out.substring(0, span.index) +
208
- normalizedNew +
209
- out.substring(span.index + span.matchLength);
210
- }
211
- return { content: out, count: spans.length };
212
- }
213
-
214
- const result = fuzzyFindText(content, oldText);
215
- if (!result.found) return { content, count: 0 };
216
-
217
- return {
218
- content:
219
- content.substring(0, result.index) +
220
- normalizedNew +
221
- content.substring(result.index + result.matchLength),
222
- count: 1,
223
- };
224
- }
225
-
226
- // ─── Diff generation ────────────────────────────────────────────────────
227
-
228
- export function generateDiffString(
229
- oldContent: string,
230
- newContent: string,
231
- contextLines = 4,
232
- ): { diff: string } {
233
- const patch = Diff.structuredPatch("a", "b", oldContent, newContent, undefined, undefined, {
234
- context: contextLines,
235
- });
236
-
237
- if (!patch.hunks.length) {
238
- return { diff: "" };
239
- }
240
-
241
- const maxLineNum = Math.max(
242
- oldContent.split("\n").length,
243
- newContent.split("\n").length,
244
- );
245
- const lineNumWidth = String(maxLineNum).length;
246
- const hashPad = " ".repeat(ANCHOR_SEP.length + 2); // align with `${ANCHOR_SEP}HH${CONTENT_SEP}`
247
- const output: string[] = [];
248
-
249
- for (let h = 0; h < patch.hunks.length; h++) {
250
- const hunk = patch.hunks[h]!;
251
- if (h > 0) {
252
- output.push(" ...");
253
- }
254
-
255
- let oldLineNum = hunk.oldStart;
256
- let newLineNum = hunk.newStart;
257
-
258
- for (const line of hunk.lines) {
259
- if (line === "\") continue;
260
-
261
- const prefix = line[0] as " " | "+" | "-";
262
- const text = line.slice(1);
263
-
264
- if (prefix === "-") {
265
- const padded = String(oldLineNum).padStart(lineNumWidth, " ");
266
- output.push(`-${padded}${hashPad}${CONTENT_SEP}${text}`);
267
- oldLineNum++;
268
- } else if (prefix === "+") {
269
- const padded = String(newLineNum).padStart(lineNumWidth, " ");
270
- const hash = computeLineHash(newLineNum, text);
271
- output.push(`+${padded}${ANCHOR_SEP}${hash}${CONTENT_SEP}${text}`);
272
- newLineNum++;
273
- } else {
274
- const padded = String(newLineNum).padStart(lineNumWidth, " ");
275
- const hash = computeLineHash(newLineNum, text);
276
- output.push(` ${padded}${ANCHOR_SEP}${hash}${CONTENT_SEP}${text}`);
277
- oldLineNum++;
278
- newLineNum++;
279
- }
280
- }
281
- }
282
-
283
- return { diff: output.join("\n") };
284
- }
285
-
286
- export interface CompactHashlineDiffPreview {
287
- preview: string;
288
- addedLines: number;
289
- removedLines: number;
290
- }
291
-
292
- type DiffPreviewKind = "context" | "addition" | "deletion";
293
-
294
- function classifyDiffPreviewLine(line: string): DiffPreviewKind | null {
295
- if (line.startsWith("+")) return "addition";
296
- if (line.startsWith("-")) return "deletion";
297
- if (line.startsWith(" ")) return "context";
298
- return null;
299
- }
300
-
301
- function summarizeOmitted(count: number, label: string): string {
302
- return `... ${count} more ${label} line${count === 1 ? "" : "s"}`;
303
- }
304
-
305
- function collapseDiffPreviewRun(
306
- lines: string[],
307
- maxVisible: number,
308
- label: string,
309
- ): string[] {
310
- if (lines.length <= maxVisible) {
311
- return lines;
312
- }
313
-
314
- return [
315
- ...lines.slice(0, maxVisible),
316
- summarizeOmitted(lines.length - maxVisible, label),
317
- ];
318
- }
319
-
320
- export function buildCompactHashlineDiffPreview(
321
- diff: string,
322
- options: {
323
- maxUnchangedRun?: number;
324
- maxAdditionRun?: number;
325
- maxDeletionRun?: number;
326
- maxOutputLines?: number;
327
- } = {},
328
- ): CompactHashlineDiffPreview {
329
- const {
330
- maxUnchangedRun = 2,
331
- maxAdditionRun = 4,
332
- maxDeletionRun = 4,
333
- maxOutputLines = 12,
334
- } = options;
335
-
336
- if (!diff.trim()) {
337
- return { preview: "", addedLines: 0, removedLines: 0 };
338
- }
339
-
340
- const lines = diff.split("\n").filter((line) => line.length > 0);
341
- const previewLines: string[] = [];
342
- let addedLines = 0;
343
- let removedLines = 0;
344
-
345
- for (let index = 0; index < lines.length; ) {
346
- const kind = classifyDiffPreviewLine(lines[index]!);
347
- let end = index + 1;
348
- while (end < lines.length && classifyDiffPreviewLine(lines[end]!) === kind) {
349
- end += 1;
350
- }
351
-
352
- const run = lines.slice(index, end);
353
- switch (kind) {
354
- case "addition":
355
- addedLines += run.length;
356
- previewLines.push(...collapseDiffPreviewRun(run, maxAdditionRun, "added"));
357
- break;
358
- case "deletion":
359
- removedLines += run.length;
360
- previewLines.push(...collapseDiffPreviewRun(run, maxDeletionRun, "removed"));
361
- break;
362
- case "context":
363
- previewLines.push(...collapseDiffPreviewRun(run, maxUnchangedRun, "unchanged"));
364
- break;
365
- default:
366
- previewLines.push(...run);
367
- break;
368
- }
369
-
370
- index = end;
371
- }
372
-
373
- if (previewLines.length > maxOutputLines) {
374
- const visibleLines = previewLines.slice(0, maxOutputLines);
375
- visibleLines.push(
376
- summarizeOmitted(previewLines.length - maxOutputLines, "preview"),
377
- );
378
- return {
379
- preview: visibleLines.join("\n"),
380
- addedLines,
381
- removedLines,
382
- };
383
- }
384
-
385
- return {
386
- preview: previewLines.join("\n"),
387
- addedLines,
388
- removedLines,
389
- };
390
- }
1
+ import * as Diff from "diff";
2
+ import { computeLineHash, ANCHOR_SEP, CONTENT_SEP } from "./hashline";
3
+
4
+ // ─── Line ending normalization ──────────────────────────────────────────
5
+
6
+ export function detectLineEnding(content: string): "\r\n" | "\n" {
7
+ const crlfIdx = content.indexOf("\r\n");
8
+ const lfIdx = content.indexOf("\n");
9
+ if (lfIdx === -1 || crlfIdx === -1) return "\n";
10
+ return crlfIdx < lfIdx ? "\r\n" : "\n";
11
+ }
12
+
13
+ export function normalizeToLF(text: string): string {
14
+ return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
15
+ }
16
+
17
+ export function restoreLineEndings(
18
+ text: string,
19
+ ending: "\r\n" | "\n",
20
+ ): string {
21
+ return ending === "\r\n" ? text.replace(/\n/g, "\r\n") : text;
22
+ }
23
+
24
+ export function stripBom(content: string): { bom: string; text: string } {
25
+ return content.startsWith("\uFEFF")
26
+ ? { bom: "\uFEFF", text: content.slice(1) }
27
+ : { bom: "", text: content };
28
+ }
29
+
30
+ // ─── Diff generation ────────────────────────────────────────────────────
31
+
32
+ export function generateDiffString(
33
+ oldContent: string,
34
+ newContent: string,
35
+ contextLines = 4,
36
+ ): { diff: string } {
37
+ const patch = Diff.structuredPatch("a", "b", oldContent, newContent, undefined, undefined, {
38
+ context: contextLines,
39
+ });
40
+
41
+ if (!patch.hunks.length) {
42
+ return { diff: "" };
43
+ }
44
+
45
+ const maxLineNum = Math.max(
46
+ oldContent.split("\n").length,
47
+ newContent.split("\n").length,
48
+ );
49
+ const lineNumWidth = String(maxLineNum).length;
50
+ const hashPad = " ".repeat(ANCHOR_SEP.length + 2); // align with `${ANCHOR_SEP}HH${CONTENT_SEP}`
51
+ const output: string[] = [];
52
+
53
+ // Build context array for hash computation (same normalization as getPreviewLines)
54
+ const newFileLines = newContent.length === 0
55
+ ? []
56
+ : newContent.endsWith("\n")
57
+ ? newContent.split("\n").slice(0, -1)
58
+ : newContent.split("\n");
59
+
60
+ for (let h = 0; h < patch.hunks.length; h++) {
61
+ const hunk = patch.hunks[h]!;
62
+ if (h > 0) {
63
+ output.push(" ...");
64
+ }
65
+
66
+ let oldLineNum = hunk.oldStart;
67
+ let newLineNum = hunk.newStart;
68
+
69
+ for (const line of hunk.lines) {
70
+ if (line === "\") continue;
71
+
72
+ const prefix = line[0] as " " | "+" | "-";
73
+ const text = line.slice(1);
74
+
75
+ if (prefix === "-") {
76
+ const padded = String(oldLineNum).padStart(lineNumWidth, " ");
77
+ output.push(`-${padded}${hashPad}${CONTENT_SEP}${text}`);
78
+ oldLineNum++;
79
+ } else if (prefix === "+") {
80
+ const padded = String(newLineNum).padStart(lineNumWidth, " ");
81
+ const hash = computeLineHash(newFileLines, newLineNum - 1);
82
+ output.push(`+${padded}${ANCHOR_SEP}${hash}${CONTENT_SEP}${text}`);
83
+ newLineNum++;
84
+ } else {
85
+ const padded = String(newLineNum).padStart(lineNumWidth, " ");
86
+ const hash = computeLineHash(newFileLines, newLineNum - 1);
87
+ output.push(` ${padded}${ANCHOR_SEP}${hash}${CONTENT_SEP}${text}`);
88
+ oldLineNum++;
89
+ newLineNum++;
90
+ }
91
+ }
92
+ }
93
+
94
+ return { diff: output.join("\n") };
95
+ }
96
+
97
+ export interface CompactHashlineDiffPreview {
98
+ preview: string;
99
+ addedLines: number;
100
+ removedLines: number;
101
+ }
102
+
103
+ type DiffPreviewKind = "context" | "addition" | "deletion";
104
+
105
+ function classifyDiffPreviewLine(line: string): DiffPreviewKind | null {
106
+ if (line.startsWith("+")) return "addition";
107
+ if (line.startsWith("-")) return "deletion";
108
+ if (line.startsWith(" ")) return "context";
109
+ return null;
110
+ }
111
+
112
+ function summarizeOmitted(count: number, label: string): string {
113
+ return `... ${count} more ${label} line${count === 1 ? "" : "s"}`;
114
+ }
115
+
116
+ function collapseDiffPreviewRun(
117
+ lines: string[],
118
+ maxVisible: number,
119
+ label: string,
120
+ ): string[] {
121
+ if (lines.length <= maxVisible) {
122
+ return lines;
123
+ }
124
+
125
+ return [
126
+ ...lines.slice(0, maxVisible),
127
+ summarizeOmitted(lines.length - maxVisible, label),
128
+ ];
129
+ }
130
+
131
+ export function buildCompactHashlineDiffPreview(
132
+ diff: string,
133
+ options: {
134
+ maxUnchangedRun?: number;
135
+ maxAdditionRun?: number;
136
+ maxDeletionRun?: number;
137
+ maxOutputLines?: number;
138
+ } = {},
139
+ ): CompactHashlineDiffPreview {
140
+ const {
141
+ maxUnchangedRun = 2,
142
+ maxAdditionRun = 4,
143
+ maxDeletionRun = 4,
144
+ maxOutputLines = 12,
145
+ } = options;
146
+
147
+ if (!diff.trim()) {
148
+ return { preview: "", addedLines: 0, removedLines: 0 };
149
+ }
150
+
151
+ const lines = diff.split("\n").filter((line) => line.length > 0);
152
+ const previewLines: string[] = [];
153
+ let addedLines = 0;
154
+ let removedLines = 0;
155
+
156
+ for (let index = 0; index < lines.length; ) {
157
+ const kind = classifyDiffPreviewLine(lines[index]!);
158
+ let end = index + 1;
159
+ while (end < lines.length && classifyDiffPreviewLine(lines[end]!) === kind) {
160
+ end += 1;
161
+ }
162
+
163
+ const run = lines.slice(index, end);
164
+ switch (kind) {
165
+ case "addition":
166
+ addedLines += run.length;
167
+ previewLines.push(...collapseDiffPreviewRun(run, maxAdditionRun, "added"));
168
+ break;
169
+ case "deletion":
170
+ removedLines += run.length;
171
+ previewLines.push(...collapseDiffPreviewRun(run, maxDeletionRun, "removed"));
172
+ break;
173
+ case "context":
174
+ previewLines.push(...collapseDiffPreviewRun(run, maxUnchangedRun, "unchanged"));
175
+ break;
176
+ default:
177
+ previewLines.push(...run);
178
+ break;
179
+ }
180
+
181
+ index = end;
182
+ }
183
+
184
+ if (previewLines.length > maxOutputLines) {
185
+ const visibleLines = previewLines.slice(0, maxOutputLines);
186
+ visibleLines.push(
187
+ summarizeOmitted(previewLines.length - maxOutputLines, "preview"),
188
+ );
189
+ return {
190
+ preview: visibleLines.join("\n"),
191
+ addedLines,
192
+ removedLines,
193
+ };
194
+ }
195
+
196
+ return {
197
+ preview: previewLines.join("\n"),
198
+ addedLines,
199
+ removedLines,
200
+ };
201
+ }