-
Notifications
You must be signed in to change notification settings - Fork 249
feat(diff): add word-level highlighting for modified lines #368
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
base: main
Are you sure you want to change the base?
Conversation
2a85a7d to
85e2afd
Compare
|
This would be cool. I'd make it an option though so both view styles work. I am concerned about multi width graphemes though, it cannot use String.length or utf16 char offsets from js as the highlights operate on display width currently. That's a bit tricky. What could work to get content start and end for the highlight offsets would be to measure the string before the content starts with Bun.stringWidth, then the content that should be highlighted itself with Bun.stringWidth and add that to the former for the end offset. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds word-level highlighting to diff views, showing which specific words changed within modified lines (similar to GitHub's diff view). The implementation uses character-level similarity scoring to intelligently pair removed/added lines, then applies word-based diffing to compute highlight positions.
Key changes:
- New
computeLineSimilarityandcomputeInlineHighlightsfunctions for word-level diff analysis - Modified unified and split view rendering to integrate inline highlights
- Added
LineInlineHighlightinterface and rendering support inLineNumberRenderable
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| packages/core/src/renderables/Diff.ts | Implements word-level diff algorithm with similarity-based line pairing, adds new options (disableWordHighlights, addedWordBg, removedWordBg, lineSimilarityThreshold) and integrates highlights into both unified and split view rendering |
| packages/core/src/renderables/LineNumberRenderable.ts | Adds LineInlineHighlight interface and rendering logic to draw word-level highlight backgrounds, includes new public API methods for managing highlights |
| packages/core/src/renderables/Diff.test.ts | Comprehensive test coverage for similarity computation, inline highlight calculation (including CJK/emoji edge cases), and option handling |
| packages/react/examples/diff.tsx | Updates example diff content to include emoji and CJK characters for better visual testing of word-level highlighting |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Added scrollX offset to inline highlight positions in LineNumberRenderable - Added tests for word highlights with horizontal scrolling in unified and split views
When text wraps to multiple visual lines, highlights now only appear on the correct wrapped line(s) by using lineWraps offset to calculate visibility.
lineWraps contains wrap INDEX (0,1,2...), not column offset. Now properly computes column offset by summing widths of previous wrapped segments belonging to the same logical line.
…into word-highlights
bed1937 to
e74a3fa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
I now increase opacity, previously it was only increasing brightness |



Adds word-level highlighting to show which words changed within modified lines (like GitHub's diff view).
Algorithm:
diffWordsWithSpaceComplexity is O(L + B×P×C²) where L=lines, B=change blocks, P=pairs per block, C=chars per line. In practice C is small (~100) and only change blocks are processed.
Performance guards: skips word diff for dissimilar lines, skips entirely for large blocks (>50 lines).
New options:
disableWordHighlights- Turn off word-level highlighting (default:false)addedWordBg- Background color for added words (default: brighteraddedBg)removedWordBg- Background color for removed words (default: brighterremovedBg)lineSimilarityThreshold- Min similarity (0-1) to pair lines for word diff (default:0.4)Also includes:
wrapMode: "none"and horizontal scrolling is used