Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix diffWords handling of whitespace #497

Merged
merged 56 commits into from
Mar 11, 2024
Merged

Fix diffWords handling of whitespace #497

merged 56 commits into from
Mar 11, 2024

Conversation

ExplodingCabbage
Copy link
Collaborator

@ExplodingCabbage ExplodingCabbage commented Mar 5, 2024

This resolves #436.

Consider the dramatic example of using diffWords to diff this text...

FOUR FIVE SIX SEVEN EIGHT NINE

against

ONE TWO THREE FOUR FIVE SIX

The diff we intuitively expect to get is one where we insert the three words ONE TWO THREE at the beginning, keep the three-word phrase FOUR FIVE SIX that's common to both texts, then delete the three words SEVEN EIGHT NINE from the end.

On master as it currently exists, though, we instead get this absurd and unintuitive diff, where ALL SIX WORDS from the original text get deleted and all six words from the new text get inserted. Thus we delete the words "FOUR", "FIVE", and "SIX" and then reinsert them.

image

Why this absurd result? Because on master, runs of whitespace are also tokens, and preserving one of those is worth as much to the score of a diff as preserving an actual word. The six extra insertions/deletions of words necessitated by not preserving any words saves us from having to insert or delete six spaces, so is considered just as good an outcome!

On this branch, we stop treating spaces as tokens, and therefore get this much more sensible diff:

image

It's annoyingly complicated. But IMO this should convert diffWords from basically being a horrible trap for users that you should probably never use for anything, into providing pretty decent word-level diffs most of the time (albeit with some slightly weird handling of whitespace changes). IMO this is a definite improvement.

@ExplodingCabbage ExplodingCabbage self-assigned this Mar 5, 2024
@ExplodingCabbage ExplodingCabbage changed the title Fix diff words handling of whitespace Fix diffWords handling of whitespace Mar 6, 2024
@ExplodingCabbage ExplodingCabbage marked this pull request as ready for review March 8, 2024 15:09
@ExplodingCabbage
Copy link
Collaborator Author

Okay, I reckon this is ready to merge. Dunno if anyone is paying attention to what I'm doing but I'll leave this open until Monday just in case someone wants to comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

diffWords doesn't consistently ignore whitespace, despite the README saying it should
1 participant