-
Notifications
You must be signed in to change notification settings - Fork 501
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
Conversation
This file contains 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
Conflicts: src/diff/word.js
…s sense where it is
ExplodingCabbage
changed the title
Fix diff words handling of whitespace
Fix diffWords handling of whitespace
Mar 6, 2024
…o diffTrimmedLines
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
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
diffWords
to diff this text...against
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 phraseFOUR FIVE SIX
that's common to both texts, then delete the three wordsSEVEN 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.
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
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.