Fix diffWords handling of whitespace#497
Merged
ExplodingCabbage merged 56 commits intomasterfrom Mar 11, 2024
Merged
Conversation
Conflicts: src/diff/word.js
…s sense where it is
…o diffTrimmedLines
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! |
Conflicts: release-notes.md
ryota-ka
added a commit
to ryota-ka/DefinitelyTyped
that referenced
this pull request
Oct 1, 2024
ryota-ka
added a commit
to ryota-ka/DefinitelyTyped
that referenced
this pull request
Oct 8, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This resolves #436.
Consider the dramatic example of using
diffWordsto diff this text...against
The diff we intuitively expect to get is one where we insert the three words
ONE TWO THREEat the beginning, keep the three-word phraseFOUR FIVE SIXthat's common to both texts, then delete the three wordsSEVEN EIGHT NINEfrom 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.
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:
It's annoyingly complicated. But IMO this should convert
diffWordsfrom 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.