Coverage / src/markers.ts 88.8%
87 of 98 lines run, 6 of 6 functions, 33 of 33 branches · run #26 on main · run missed
| 1 | 0× | // Parser for oxy conflict markers (spec §9.5). No vscode imports, so it is |
| 2 | 0× | // tested with plain Node. |
| 3 | 0× | // |
| 4 | 0× | // <<<<<<< oxy conflict 1 of 2, side 1 of 2: change k7m2p9 "Add retry loop" |
| 5 | 0× | // ... |
| 6 | 0× | // ======= oxy conflict 1 of 2, side 2 of 2: change q3x8w1 "Refactor client" |
| 7 | 0× | // ... |
| 8 | 0× | // >>>>>>> oxy conflict 1 of 2 end |
| 9 | 0× | // |
| 10 | 0× | // A delete/insert conflict's first side reads `deleted by change X` and is |
| 11 | 0× | // followed by a `-------` line and the deleted context. |
| 12 | 1× | |
| 13 | export interface ConflictSide { | |
| 14 | /** 1-based side number. */ | |
| 15 | number: number; | |
| 16 | /** `k7m2p9 "Add retry loop"` or `deleted by change k7m2p9`. */ | |
| 17 | label: string; | |
| 18 | deleted: boolean; | |
| 19 | /** Line index of the side's header. */ | |
| 20 | header: number; | |
| 21 | /** Content lines (exclusive end). */ | |
| 22 | start: number; | |
| 23 | end: number; | |
| 24 | } | |
| 25 | 1× | |
| 26 | export interface ConflictRegion { | |
| 27 | number: number; | |
| 28 | total: number; | |
| 29 | /** Line index of `<<<<<<<`. */ | |
| 30 | start: number; | |
| 31 | /** Line index of `>>>>>>>`. */ | |
| 32 | end: number; | |
| 33 | sides: ConflictSide[]; | |
| 34 | } | |
| 35 | 1× | |
| 36 | 1× | const OPEN = /^<<<<<<< oxy conflict (\d+) of (\d+), side (\d+) of (\d+): (.*)$/; |
| 37 | 1× | const NEXT = /^======= oxy conflict (\d+) of (\d+), side (\d+) of (\d+): (.*)$/; |
| 38 | 1× | const CLOSE = /^>>>>>>> oxy conflict (\d+) of (\d+) end$/; |
| 39 | 3× | |
| 40 | 3× | export function parseConflicts(lines: string[]): ConflictRegion[] { |
| 41 | 3× | const regions: ConflictRegion[] = []; |
| 42 | 3× | let current: ConflictRegion | undefined; |
| 43 | 3× | let side: ConflictSide | undefined; |
| 44 | 3× | for (let i = 0; i < lines.length; i++) { |
| 45 | 17× | const line = lines[i]; |
| 46 | 17× | const open = OPEN.exec(line); |
| 47 | 17× | if (open && !current) { |
| 48 | 2× | current = { number: Number(open[1]), total: Number(open[2]), start: i, end: -1, sides: [] }; |
| 49 | 2× | side = newSide(Number(open[3]), open[5], i); |
| 50 | 2× | if (side.deleted && lines[i + 1] === "-------") { |
| 51 | 1× | side.start = i + 2; |
| 52 | 1× | i += 1; |
| 53 | 1× | } |
| 54 | 2× | current.sides.push(side); |
| 55 | 2× | continue; |
| 56 | 2× | } |
| 57 | 17× | const next = current ? NEXT.exec(line) : null; |
| 58 | 17× | if (next && current && side) { |
| 59 | 3× | side.end = i; |
| 60 | 3× | side = newSide(Number(next[3]), next[5], i); |
| 61 | 3× | current.sides.push(side); |
| 62 | 3× | continue; |
| 63 | 3× | } |
| 64 | 17× | const close = current ? CLOSE.exec(line) : null; |
| 65 | 17× | if (close && current && side) { |
| 66 | 2× | side.end = i; |
| 67 | 2× | current.end = i; |
| 68 | 2× | regions.push(current); |
| 69 | 2× | current = undefined; |
| 70 | 2× | side = undefined; |
| 71 | 2× | } |
| 72 | 17× | } |
| 73 | 3× | return regions; |
| 74 | 5× | } |
| 75 | 5× | |
| 76 | 5× | function newSide(number: number, label: string, header: number): ConflictSide { |
| 77 | 5× | const deleted = label.startsWith("deleted by change "); |
| 78 | 5× | return { number, label: deleted ? label : label.replace(/^change /, ""), deleted, header, start: header + 1, end: header + 1 }; |
| 79 | 4× | } |
| 80 | 4× | |
| 81 | 4× | /** The lines that replace a region when `keep` (side numbers) are kept, in order. */ |
| 82 | 4× | export function resolveRegion(lines: string[], region: ConflictRegion, keep: number[]): string[] { |
| 83 | 4× | const out: string[] = []; |
| 84 | 4× | for (const n of keep) { |
| 85 | 5× | const side = region.sides.find((s) => s.number === n); |
| 86 | 5× | if (!side || side.deleted) { |
| 87 | 1× | continue; |
| 88 | 1× | } |
| 89 | 4× | out.push(...lines.slice(side.start, side.end)); |
| 90 | 4× | } |
| 91 | 4× | return out; |
| 92 | 2× | } |
| 93 | 2× | |
| 94 | 2× | /** A conflict candidate file's real name, or undefined. */ |
| 95 | 2× | export function candidateBase(path: string): string | undefined { |
| 96 | 2× | const m = /^(.*)\.oxy-conflict-\d+$/.exec(path); |
| 97 | 2× | return m ? m[1] : undefined; |
| 98 | 2× | } |