Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Spread from anchors breadth-first, so the nearest anchor wins
nyblnet2242f232a2ed
diff --git a/scripts/build-code-matrix.mjs b/scripts/build-code-matrix.mjsindex c1bbbc85..5474bdd2 100644--- a/scripts/build-code-matrix.mjs+++ b/scripts/build-code-matrix.mjs@@ -44,7 +44,7 @@ const CASES = [['1. Swap — distinct lines', 'clean: the two lines trade places',`function f() {\n alpha(one)\n beta(two)\n done()\n}`,`function f() {\n beta(two)\n alpha(one)\n done()\n}`],- ['2. Swap — shared prefix', 'TEARS: both lines start with const, so it cannot tell them apart',+ ['2. Swap — shared prefix', 'each const travels with its own line — nearest anchor wins',`function f() {\n const a = one()\n const b = two()\n done()\n}`,`function f() {\n const b = two()\n const a = one()\n done()\n}`],['3. Move a line far', 'first() travels to the bottom, the rest shift up',@@ -67,7 +67,7 @@ const CASES = [const slides = [slide([text('Seven ways to change code', 96, 250, 1088, 80, { size: 64, weight: 700 }),text('Each pair is one edit. Watch what travels, and what tears.', 96, 350, 900, 50, { size: 22, color: MIST }),- text('Scenario 2 is the known-bad case — a swap of two lines sharing a prefix.', 96, 410, 950, 50, { size: 18, color: PEACH }),+ text('Scenario 2 was the known-tear case — fixed by breadth-first pairing.', 96, 410, 950, 50, { size: 18, color: PEACH }),], { transition: 'fade', name: 'title' })]for (const [label, expect, A, B] of CASES) {diff --git a/scripts/test-codediff.ts b/scripts/test-codediff.tsindex 3f674a7b..52ffc537 100644--- a/scripts/test-codediff.ts+++ b/scripts/test-codediff.ts@@ -124,5 +124,29 @@ const toksOf = (states: State[] | undefined) =>JSON.stringify([...two.entries()].map(([k, v]) => [k, idsOf(v)])))}+// 8. SHARED-PREFIX SWAP — the case greedy spreading lost. Two lines starting+// with `const` trade places; each const must travel WITH ITS LINE, claimed+// by the anchor two steps away (b / a), not by the header cascade three+// steps above. Regression for the breadth-first spread: before it, one+// const paired top-to-top and the other was minted fresh — it vanished for+// 40% of the morph and faded back in, on screen.+{+ const A = `function f() {\n const a = one()\n const b = two()\n done()\n}`+ const B = `function f() {\n const b = two()\n const a = one()\n done()\n}`+ const states = new HeckelDiff(codeDoc([A, B])).computeDiffs('g')+ const consts = (i: number) => toksOf(states.get(i))+ .filter((t: any) => t.content === 'const')+ .sort((a: any, b: any) => a.lineNumber - b.lineNumber)+ const [p1, p2] = consts(0)+ const [c1, c2] = consts(1)+ check('shared-prefix swap: both consts pair', !!c1?.morphId() && !!c2?.morphId())+ check('shared-prefix swap: each const travels with its line',+ c1.morphId() === p2.morphId() && c2.morphId() === p1.morphId())+ const inserts = (states.get(1) ?? []).filter((s: any) => s.kind === 'insert')+ .map((s: any) => s.token.content)+ check('shared-prefix swap: no ink token is minted fresh',+ inserts.every((v: string) => !/\S/.test(v)))+}+console.log(FAILS.length ? `\n${FAILS.length} FAILED` : '\nall passed')process.exit(FAILS.length ? 1 : 0)diff --git a/slides/src/diff.ts b/slides/src/diff.tsindex 47e97d07..9475b49b 100644--- a/slides/src/diff.ts+++ b/slides/src/diff.ts@@ -226,59 +226,53 @@ export class HeckelDiff {statesC[anchC.index] = match}}- // Phase 2: We found unique anchors.- // Move forward and find the ones that are matching adjacent to the ones that already matched.- for (let i = 0; i < statesC.length - 1; i += 1) {- const state = statesC[i]- const nextState = statesC[i + 1]- if (state.kind === 'match' && nextState.kind === 'empty') {- const matchState = state as Match- const nextToken: Token | undefined = current[i + 1]- const nextPreviousToken: Token | undefined = previous[matchState.previousIdx + 1]- // The previous side must be unclaimed too. Without this, duplicating a- // token beside an anchor (X B Y -> X B B Y) let the backward pass claim- // the same previous token a second time: two current-side tokens shared- // one morph id, and duplicate flip ids corrupt pairing downstream.- if (statesP[matchState.previousIdx + 1]?.kind !== 'empty') continue- if (nextToken && nextPreviousToken && nextToken.key() == nextPreviousToken.key()) {- const match: Match = {- kind: 'match',- previousIdx: matchState.previousIdx + 1,- previous: nextPreviousToken,- current: nextToken,- currentIdx: i + 1- }- // Propagate morph id- nextToken.setMorphId(nextPreviousToken.morphId())- statesP[match.previousIdx] = match- statesC[match.currentIdx] = match- }- }+ // Phases 2 & 3 — spread from every anchor, breadth-first. One ring per+ // round: each match may claim ONE neighbour per direction per round, so a+ // token is claimed by its NEAREST anchor, not by whichever cascade happens+ // to run first. The previous greedy sweeps lost the shared-prefix swap:+ //+ // const a = one() const b = two()+ // const b = two() -> const a = one()+ //+ // The header cascade (function f() { ...) walked forward straight into the+ // first `const` and paired it top-to-top, three steps from its anchor —+ // while `b`, only two steps away, was still waiting for the backward+ // sweep. Its own `const` then found the previous side already claimed and+ // was minted fresh: it vanished for 40% of the morph and faded back in,+ // on screen, in a deck. Distance-first, each `const` is claimed by the+ // anchor on its own line and travels with it. Claims never overwrite+ // (both sides must be unclaimed), so equal-distance conflicts — the+ // genuinely ambiguous ones — resolve deterministically to the earlier+ // token, and everything a cascade could reach is still reached.+ let frontier: number[] = []+ for (let i = 0; i < statesC.length; i += 1) {+ if (statesC[i].kind === 'match') frontier.push(i)}- // Phase 3: Same as Phase 2 but backwards.- for (let i = statesC.length - 1; i >= 1; i -= 1) {- const state = statesC[i]- const priorState = statesC[i - 1]- if (state.kind === 'match' && priorState.kind === 'empty') {- const matchState = state as Match- const priorToken = current[i - 1]- const priorPreviousToken = previous[matchState.previousIdx - 1]- // Same previous-side guard as Phase 2, mirrored.- if (statesP[matchState.previousIdx - 1]?.kind !== 'empty') continue- if (priorToken && priorPreviousToken && priorToken.key() == priorPreviousToken.key()) {+ while (frontier.length > 0) {+ const next: number[] = []+ for (const ci of frontier) {+ const m = statesC[ci] as Match+ for (const step of [1, -1]) {+ const nc = ci + step+ const np = m.previousIdx + step+ if (nc < 0 || nc >= statesC.length || statesC[nc].kind !== 'empty') continue+ if (np < 0 || np >= previous.length || statesP[np].kind !== 'empty') continue+ if (current[nc].key() !== previous[np].key()) continueconst match: Match = {kind: 'match',- previousIdx: matchState.previousIdx - 1,- previous: priorPreviousToken,- current: priorToken,- currentIdx: i - 1+ previousIdx: np,+ previous: previous[np],+ currentIdx: nc,+ current: current[nc]}// Propagate morph id- priorToken.setMorphId(priorPreviousToken.morphId())- statesP[match.previousIdx] = match- statesC[match.currentIdx] = match+ current[nc].setMorphId(previous[np].morphId())+ statesP[np] = match+ statesC[nc] = match+ next.push(nc)}}+ frontier = next}// Phase 4: Final pass.// Anything that did not match in: