Repository navigation
diff: Report the character under the pointer so a diff can host links - #4042
Conversation
|
💡 Needs the maintainer's call on the API shape: the capability is useful, but the public surface is larger than the use case needs. Reviewed at 7b1b60d. New public API (+516/-23):
Points to consider:
Code otherwise reads cleanly and the docs (en, zh-CN) are updated. |
|
💡 The direction is welcome; please narrow the public API before we accept it:
Once these are in, we'll re-review (reviewed head 7b1b60d). |
…eview Upstream review on longbridge#4042 asked to shrink the public surface: without_background and is_filled duplicated what a transparent with_background already expresses, is_underlined is only read by the renderer, and the text-click precedence over the line handler deserved stating on both doc comments. Excerpts gained the ordering debug_assert from the longbridge#4043 review. Signed-off-by: Sven Kanoldt <sven@d34dl0ck.me>
Diff reports the character under the pointer with the held modifiers (on_text_hover, also when a modifier changes with the pointer at rest), hands clicks on a character to on_text_click before the line handler, and takes a text cursor. Span highlights can be underlined and unfilled, so the hovered span reads as a link. DiffState::has_line says whether the patch carries a line, so the application can decide where a jump lands before scrolling. Signed-off-by: Sven Kanoldt <sven@d34dl0ck.me>
7b1b60d to
749dc8e
Compare
|
✅ API looks good at 749dc8e. All three points from the previous review are addressed:
The remaining public surface ( |
|
Review points addressed at 749dc8e (PR description updated to match):
On the red Test Docs check: the failure is |
…longbridge#4042) ## Purpose Closes longbridge#4040. `Diff` reports pointer interaction at line granularity, so an application cannot tell which character — and therefore which word — the pointer is over. That rules out the link idiom every code surface has: Cmd/Ctrl-hover marks a symbol, an issue reference or a file path, Cmd/Ctrl-click follows it. This PR adds character-level pointer events while keeping the component semantics-free: it reports positions, the application decides what they mean. https://github.com/user-attachments/assets/21acb6b0-6529-4b6b-a0df-7de6a2db43f7 ## What changed - `on_text_hover` reports the character under the pointer with the held modifiers, or `None` off the text, and only on change. While the diff has focus it also fires when a modifier goes down or up with the pointer at rest, so a link can light up on the key alone. - `on_text_click` hears a click on a character before `on_line_click`; returning true claims the click. `text_cursor` sets the pointer shown over source text. - `DiffTextPosition` carries the line position, the character's byte offset in the line's content (the unit span-highlight spans use) and the line's text, so the handler can read the word around the offset without a copy of the patch. - `DiffSpanHighlight` can underline in its text color (`with_underline`), and a fully transparent `with_background` leaves the line showing through, so the hovered span reads as a link rather than a search match. - `DiffState::has_line` says whether the patch carries a line, so the application knows whether a jump target is in the diff before scrolling. - Hit testing reuses the painted-run geometry the text selection already keeps; past a line's end is over no character, and a run clipped by horizontal scrolling is not hit under its clip. - Site docs (en, zh-CN) and five window tests: hover reporting and change-only delivery, modifier-at-rest delivery, claimed and unclaimed clicks, `has_line` over folded and absent lines. ## Public API ### gpui-component ```rust pub struct DiffTextPosition { /* private */ } impl DiffTextPosition { pub fn position(&self) -> &DiffLinePosition; // the line the character is on pub fn offset(&self) -> usize; // byte offset in the line's content pub fn text(&self) -> &SharedString; // the line's content } impl Diff { pub fn on_text_hover<F>(self, handler: F) -> Self where F: Fn(Option<&DiffTextPosition>, Modifiers, &mut Window, &mut App) + 'static; pub fn on_text_click<F>(self, handler: F) -> Self where F: Fn(&DiffTextPosition, &ClickEvent, &mut Window, &mut App) -> bool + 'static; pub fn text_cursor(self, cursor: CursorStyle) -> Self; } impl DiffSpanHighlight { pub fn with_underline(self) -> Self; // underline in the text color } impl DiffState { /// Whether the patch carries the source line, shown or folded. pub fn has_line(&self, position: &DiffLinePosition) -> bool; } ``` No existing item changes; no breaking changes. ## Validation `cargo test -p gpui-component` diff tests pass (69, five new), `cargo fmt --check` and `cargo clippy -p gpui-component` are clean on the touched files, `typos` clean. Exercised end to end in a GPUI Kit review application: Cmd-hover underlines a resolvable symbol, Cmd-click jumps to its definition, plain clicks still select text and lines. This change was written with AI assistance (implementation and tests), reviewed and exercised by a human-driven application. Signed-off-by: Sven Kanoldt <sven@d34dl0ck.me>
Purpose
Closes #4040.
Diffreports pointer interaction at line granularity, so an application cannot tell which character — and therefore which word — the pointer is over. That rules out the link idiom every code surface has: Cmd/Ctrl-hover marks a symbol, an issue reference or a file path, Cmd/Ctrl-click follows it. This PR adds character-level pointer events while keeping the component semantics-free: it reports positions, the application decides what they mean.diffie-symbol-navigation-v2.mov
What changed
on_text_hoverreports the character under the pointer with the held modifiers, orNoneoff the text, and only on change. While the diff has focus it also fires when a modifier goes down or up with the pointer at rest, so a link can light up on the key alone.on_text_clickhears a click on a character beforeon_line_click; returning true claims the click.text_cursorsets the pointer shown over source text.DiffTextPositioncarries the line position, the character's byte offset in the line's content (the unit span-highlight spans use) and the line's text, so the handler can read the word around the offset without a copy of the patch.DiffSpanHighlightcan underline in its text color (with_underline), and a fully transparentwith_backgroundleaves the line showing through, so the hovered span reads as a link rather than a search match.DiffState::has_linesays whether the patch carries a line, so the application knows whether a jump target is in the diff before scrolling.has_lineover folded and absent lines.Public API
gpui-component
No existing item changes; no breaking changes.
Validation
cargo test -p gpui-componentdiff tests pass (69, five new),cargo fmt --checkandcargo clippy -p gpui-componentare clean on the touched files,typosclean. Exercised end to end in a GPUI Kit review application: Cmd-hover underlines a resolvable symbol, Cmd-click jumps to its definition, plain clicks still select text and lines.This change was written with AI assistance (implementation and tests), reviewed and exercised by a human-driven application.