@xynogen/pix-pretty 1.7.6 → 1.7.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/package.json +1 -1
- package/src/diff.test.ts +23 -0
- package/src/diff.ts +6 -2
package/package.json
CHANGED
package/src/diff.test.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { parseDiff } from "./diff.js";
|
|
4
|
+
|
|
5
|
+
const OLD = "line1\nline2\nline3";
|
|
6
|
+
const NEW = "line1\nCHANGED\nline3";
|
|
7
|
+
|
|
8
|
+
describe("parseDiff baseLine", () => {
|
|
9
|
+
it("is snippet-relative when baseLine omitted (default 0)", () => {
|
|
10
|
+
const { lines } = parseDiff(OLD, NEW);
|
|
11
|
+
const del = lines.find((l) => l.type === "del");
|
|
12
|
+
expect(del?.oldNum).toBe(2); // line2 is the 2nd line of the snippet
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("shifts gutter numbers to absolute when baseLine given", () => {
|
|
16
|
+
// Snippet begins at file line 84 → snippet line 2 becomes file line 85.
|
|
17
|
+
const { lines } = parseDiff(OLD, NEW, 3, 84);
|
|
18
|
+
const del = lines.find((l) => l.type === "del");
|
|
19
|
+
const add = lines.find((l) => l.type === "add");
|
|
20
|
+
expect(del?.oldNum).toBe(85);
|
|
21
|
+
expect(add?.newNum).toBe(85);
|
|
22
|
+
});
|
|
23
|
+
});
|
package/src/diff.ts
CHANGED
|
@@ -22,6 +22,9 @@ export function parseDiff(
|
|
|
22
22
|
oldContent: string,
|
|
23
23
|
newContent: string,
|
|
24
24
|
ctx = 3,
|
|
25
|
+
// 1-based file line where the snippet begins; shifts gutter numbers from
|
|
26
|
+
// snippet-relative to absolute. 0 = no shift (snippet-relative, the default).
|
|
27
|
+
baseLine = 0,
|
|
25
28
|
): ParsedDiff {
|
|
26
29
|
const patch = Diff.structuredPatch("", "", oldContent, newContent, "", "", {
|
|
27
30
|
context: ctx,
|
|
@@ -42,8 +45,9 @@ export function parseDiff(
|
|
|
42
45
|
});
|
|
43
46
|
}
|
|
44
47
|
const h = patch.hunks[hi];
|
|
45
|
-
|
|
46
|
-
let
|
|
48
|
+
const shift = baseLine > 0 ? baseLine - 1 : 0;
|
|
49
|
+
let oL = h.oldStart + shift;
|
|
50
|
+
let nL = h.newStart + shift;
|
|
47
51
|
for (const raw of h.lines) {
|
|
48
52
|
if (raw === "\") continue;
|
|
49
53
|
const ch = raw[0];
|