Skip to content

Extract duplicated diff merge algorithm into shared module #248

Description

@glehmann

The diff merge algorithm (findBestInsertForDelete / buildInitialPairs / emitLines / mergeModifiedLines et al.) is copy-pasted across three files with slight, mostly-accidental divergences:

  • src/browser/lib/diff-worker.ts
  • src/browser/ui/diff/utils/parse.ts
  • src/api/diff.ts

Plan:

  1. Create src/shared/diff-merge.ts — extract the shared types (UNPAIRED, ReplaceKey, Line, ParseOptions, defaultOptions, Hunk, SkipBlock) and shared functions (calculateChangeRatio, changeToLine, buildChangeIndices, findBestInsertForDelete, buildInitialPairs, buildUnpairedDeletePrefix, hasUnpairedDeleteBetween, emitNormal, emitModified, emitLines, mergeModifiedLines, isSimilarEnough).

  2. Unify to one correct findBestInsertForDelete — use array-index bounds (already fixed forward-only in Fix diff merge algorithm pairing deletes with wrong inserts across sections #247). The parse.ts variant using lineNumber bounds is a bug, not intentional.

  3. Fix insertSkipBlocks in api/diff.ts — its fallback context ?? hunk.content shows a raw diff header to users (e.g. @@ -48,10 +48,16 @@). Should match the other two: "N lines hidden".

  4. Update all three files to import from the shared module instead of defining their own copies.

  5. Keep per-file: parseHunk (each has its own hunk-parsing wrapper), mergeAdjacentLines (only parse.ts uses it), insertSkipBlocks (different fallback logic that will be unified), worker infrastructure, and high-level parsing entry points.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Nice-to-have

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions