sergi / go-diff

Diff, match and patch text in Go
MIT License
1.84k stars 210 forks source link

Is it possible to prefer single line changes instead of multi-line? #91

Open Aaron3 opened 6 years ago

Aaron3 commented 6 years ago

When diffing JSON, we have some json like:

{
    "del": "^2.2.0",
    "es6-symbol": "^3.1.1",
    "eslint": "^4.11.0",
    "eslint-config-enough": "0.2.5",
}

If we compare to

{
    "del": "^2.2.0",
    "eslint": "^4.11.0",
    "eslint-config-enough": "0.2.5",
}

We end up with:

image

It would be nice to be able to prefer full-line or full word changes instead of single-character changes.

If you change the word "hello" to "goodbye", you see the diff as:

[{Delete hell} {Insert g} {Equal o} {Insert odbye}]

Which is much harder for a human to read than if the lib had the ability to prefer word/line boundaries and showed: [{Delete hello} {Insert goodbye}]

brunoluiz commented 4 years ago

Have you had any luck on doing this with the actual library?

wcevans commented 4 years ago

Try using DiffCleanupSemantic. That seems to give the result you are looking for: https://play.golang.org/p/G5QUQZifL-2