feat: shared wrap helpers — GPUI cleanup + SwiftUI SBS wrap rendering#54
Merged
hewigovens merged 3 commits intoMay 22, 2026
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces visual wrapping logic for unified and side-by-side diffs, including new UniFFI exports and a core wrap module with comprehensive tests. Feedback focuses on optimizing the UniFFI boundary by avoiding redundant data transfers for single lookups, improving lookup performance using binary search for sorted collections, and addressing the quadratic complexity in character-based span splitting.
dd17242 to
aa9b2c5
Compare
aa9b2c5 to
d9789fc
Compare
d9789fc to
91485f3
Compare
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.
Summary
Stacked on top of #53. Three commits land here, all built around the same core idea: move the diff-wrap algorithm into
jj-diffonce, expose it via uniffi, then have both shells consume it.1.
feat(core): expose wrap helpers via uniffiMoves the diff-wrap algorithm into
jj-diffand exposes it across uniffi.crates/jj-diff/src/wrap.rs(~330 LOC incl. tests):WrappedDiffLine,WrappedSbsRow(u32 fields for clean uniffi crossing).wrap_cols_for_width(width: f32, advance: f32) -> u32,wrap_diff_lines,wrap_sbs_rows,sbs_line_to_row,visual_index_for_line,visual_index_for_sbs_row.wrap_sbs_rowsproduces aligned visual rows for SBS: eachSideBySideRowexpands tomax(old_chunks, new_chunks)rows with empty padding on the shorter side.crates/jayjay-uniffi/src/types.rs:#[uniffi::remote(Record)]for the two new records.crates/jayjay-uniffi/src/diff.rs: 6#[uniffi::export]free functions; Swift bindings regenerate aswrapColsForWidth,wrapDiffLines,wrapSbsRows,sbsLineToRow,visualIndexForLine,visualIndexForSbsRow.wrap_cols_for_widthclamping/defaulting.2.
refactor(gpui): use shared wrap helpers from jj-diffDrops the GPUI shell's duplicate of the wrap algorithm —
shell/gpui/src/diff/wrap.rsis now a 70-line thin wrapper that re-exportsjj_diff::wrap::*plus the two GPUI-specific helpers that legitimately belong here:wrap_cols_from_bounds(Bounds<Pixels>, Pixels) -> u32— pulls the width out of a gpuiBoundsslot.selection_cols_in_fragment(Range<usize>, usize, usize) -> Option<Range<usize>>— UI helper that maps a logical selection column range onto a wrapped visual fragment. UsesRange<usize>because the GPUI selection module speaks in usize.The selection-fragment helper kept its
.then(|| v)fix from #53 (the crash-fix for eager subtraction overflow on continuation fragments). Two regression tests carry along.Consumers updated with surgical
as usizecasts at the boundary between the sharedu32-flavored wrap records and GPUI'susize-flavored selection geometry:sbs_body.rs,unified_body.rs,log/find.rs.attach_selection_handlersands.col_range_for(...)already takeusizeso the casts stay close to the wrap-record field reads.3.
feat(swiftui): wrap SBS diff with shared wrap_sbs_rowsDrops the "SBS wrap is disabled in AppKit" workaround that #53 had to ship.
SideBySideDiffCoordinatornow owns the rendering state (diff, font, theme) and the rebuild path:scrollView.contentSize.widthand the font'sMadvance, then asks Rust for the column count viawrapColsForWidth. CallswrapSbsRowsto flatten paired SBS rows into per-visual-row records and consumes.rowdirectly (line numbers already cleared on continuation segments).DiffTextContainerView.onContentLayoutChangedtorenderIfNeeded(force: false), which short-circuits when the cachedlastOldCols/lastNewColshaven't changed. SwiftUI state changes still callrenderIfNeeded(force: true)fromupdateNSView.appendTextLine,appendGutterLine,lineBg,tokenColor) move fromextension SideBySideRepresentableto internal free functions so the Coordinator can call them. No behavior change — they never usedself.DiffTextContainerView.wrapsText = falsestays — we pre-wrap in Rust so each visual row is shorter than the pane's column count; the NSTextContainer should not re-wrap that pre-wrapped row.Why SBS alignment now holds
The old code fed one logical SBS row to two independent NSTextViews; long lines wrapped per-side, so 3 visual lines on the left and 1 on the right left the panes out of vertical alignment. Now we ask Rust to pad the shorter side with blank continuation rows up front, so both
NSTextViews receive the same visual-row count and the gutters line up automatically.Test plan
cargo test -p jj-diff wrap::— 8 unit tests.cargo clippy -p jj-diff -p jayjay-core -p jayjay-uniffi -p jayjay-gpui --all-targets -- -D warnings.just build,just test-app,just test-gpui— all green; uniffi regenerates the Swift bindings.