The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { diffLines } from "diff"; |
| 2 | import type { Beat } from "../../shared/types.ts"; |
| 3 | |
| 4 | type Edit = Extract<Beat, { kind: "editor.edit" }>; |
| 5 | |
| 6 | const lines = (value: string) => { |
| 7 | const out = value.split("\n"); |
| 8 | if (out.at(-1) === "") out.pop(); |
| 9 | return out; |
| 10 | }; |
| 11 | |
| 12 | type Op = { kind: "same" | "del" | "add"; lines: string[] }; |
| 13 | |
| 14 | /** Unchanged lines that may sit inside one added block without splitting it. */ |
| 15 | const GAP = 2; |
| 16 | |
| 17 | /** |
| 18 | * One recorded edit as the steps you'd make by hand, one keypress each: first |
| 19 | * remove the code that goes away, then add each new block, top to bottom (so a |
| 20 | * binding is declared before the code that uses it). A block that only rewrites |
| 21 | * lines in place (same number of lines out and in) is edited in its own step. |
| 22 | */ |
| 23 | export const splitEdit = (beat: Edit): Edit[] => { |
| 24 | const ops: Op[] = diffLines(beat.before, beat.after).map((part) => ({ |
| 25 | kind: part.added ? "add" : part.removed ? "del" : "same", |
| 26 | lines: lines(part.value), |
| 27 | })); |
| 28 | // A removal directly followed by an addition of the same size is a rewrite. |
| 29 | const rewrite = new Set<number>(); |
| 30 | ops.forEach((op, i) => { |
| 31 | const next = ops[i + 1]; |
| 32 | if (op.kind === "del" && next?.kind === "add" && next.lines.length === op.lines.length) { |
| 33 | rewrite.add(i); |
| 34 | rewrite.add(i + 1); |
| 35 | } |
| 36 | }); |
| 37 | // Changes to apply after the removals, grouped into blocks. |
| 38 | const groups: number[][] = []; |
| 39 | let lastChange = -Infinity; |
| 40 | let gap = 0; |
| 41 | ops.forEach((op, i) => { |
| 42 | if (op.kind === "same") { |
| 43 | gap += op.lines.length; |
| 44 | return; |
| 45 | } |
| 46 | if (op.kind === "del" && !rewrite.has(i)) return; |
| 47 | if (groups.length && gap <= GAP && lastChange >= 0) groups.at(-1)!.push(i); |
| 48 | else groups.push([i]); |
| 49 | lastChange = i; |
| 50 | gap = 0; |
| 51 | }); |
| 52 | // Import lines arrive with the code that needs them, not as steps of their own. |
| 53 | const isImports = (group: number[]) => |
| 54 | group.every( |
| 55 | (i) => ops[i]!.kind !== "add" || ops[i]!.lines.every((l) => /^\s*(import\b|$)/.test(l)), |
| 56 | ); |
| 57 | for (let g = groups.length - 2; g >= 0; g--) { |
| 58 | if (isImports(groups[g]!)) groups.splice(g, 2, [...groups[g]!, ...groups[g + 1]!]); |
| 59 | } |
| 60 | const removals = ops.some((op, i) => op.kind === "del" && !rewrite.has(i)); |
| 61 | const stages = (removals ? 1 : 0) + groups.length; |
| 62 | if (stages <= 1) return [beat]; |
| 63 | |
| 64 | // The file once removals and the first `n` groups are applied. |
| 65 | const textAt = (n: number) => { |
| 66 | const applied = new Set(groups.slice(0, n).flat()); |
| 67 | const out: string[] = []; |
| 68 | ops.forEach((op, i) => { |
| 69 | if (op.kind === "same") out.push(...op.lines); |
| 70 | else if (op.kind === "add" && applied.has(i)) out.push(...op.lines); |
| 71 | else if (op.kind === "del" && rewrite.has(i) && !applied.has(i + 1)) out.push(...op.lines); |
| 72 | }); |
| 73 | return `${out.join("\n")}\n`; |
| 74 | }; |
| 75 | const texts = [ |
| 76 | beat.before, |
| 77 | ...(removals ? [textAt(0)] : []), |
| 78 | ...groups.map((_, g) => textAt(g + 1)), |
| 79 | ]; |
| 80 | texts[texts.length - 1] = beat.after; |
| 81 | const edits: Edit[] = []; |
| 82 | for (let k = 1; k < texts.length; k++) { |
| 83 | if (texts[k] !== texts[k - 1]) edits.push({ ...beat, before: texts[k - 1]!, after: texts[k]! }); |
| 84 | } |
| 85 | return edits.length ? edits : [beat]; |
| 86 | }; |
| 87 | |
| 88 | /** Every recorded edit in a scene, split into hand-sized steps. */ |
| 89 | export const splitEdits = (beats: readonly Beat[]): Beat[] => |
| 90 | beats.flatMap((beat): Beat[] => (beat.kind === "editor.edit" ? splitEdit(beat) : [beat])); |