Wilfred / difftastic

a structural diff that understands syntax 🟥🟩
https://difftastic.wilfred.me.uk/
MIT License
20.48k stars 328 forks source link

Highlight moved, novel regions #508

Open Wilfred opened 1 year ago

Wilfred commented 1 year ago

If a subtree is deeply novel on both sides but exists on both sides, it could be treated as a move.

QuarticCat commented 11 months ago

VSCode recently added a new feature called move code detection. The algorithm is surprisingly simple: after normal diff, calculate similarity(deletion, insertion) for every deletion & insertion pair. The code can be found here https://github.com/microsoft/vscode/blob/166097a20cbd06d10d255ef561837c439f372de3/src/vs/editor/common/diff/defaultLinesDiffComputer/computeMovedLines.ts#L44-L85.

But intuitively demonstrating the moving relationship on TUI might be a challenge.

nzbart commented 8 months ago

Git supports this through --color-moved, and delta displays the moved code nicely. It would be great if difftastic could also do this to detect code moved between files.

0xdevalias commented 6 months ago

Capturing my notes on --color-moved from another issue here:

Have you looked into git diff's --color-moved at all?

Personally I tend to use the zebra mode of it. From my git aliases:

  # https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---color-movedltmodegt
  # https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---color-moved-wsltmodesgt
  # https://git-scm.com/docs/git-config#Documentation/git-config.txt-color
  # https://git-scm.com/docs/git-config#Documentation/git-config.txt-colordiffltslotgt
  diff-refactor = \
    -c color.diff.oldMoved='white dim' \
    -c color.diff.oldMovedAlternative='white dim' \
    -c color.diff.newMoved='white dim' \
    -c color.diff.newMovedAlternative='white dim' \
    -c color.diff.newMovedDimmed='white dim' \
    -c color.diff.newMovedAlternativeDimmed='white dim' \
    diff --ignore-blank-lines --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --minimal

Originally posted by @0xdevalias in https://github.com/Wilfred/difftastic/issues/539#issuecomment-1916033305