text_view: Add TextViewState::reveal_range - #3216
Conversation
|
Thank you for implementing this and the companion highlight PR! Together they look very close to what we hoped for in #3214. The separation between application-owned search and the component's highlighting/reveal capabilities fits the request well. One small question about the API contract: if Thanks again for the work and the test coverage! |
|
@suxiaoshao Thanks! It's intentionally best effort. I've made that explicit in the |
110f3f1 to
f0a9881
Compare
Scroll the line a range of the rendered text starts on into view, in the view itself, an enclosing list, or through `TextView::on_reveal`. Refs longbridge#3214
f0a9881 to
f5fccfd
Compare
A reveal starting on a line break or an empty line in an inline flow paragraph fell between fragments, so no fragment carried it out and it was dropped after its timeout. It now goes to the next fragment of its text, or the last when none follows. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
huacnlee
left a comment
There was a problem hiding this comment.
Thanks! I pushed 19a13f6 to fix a reveal that starts on a line break (or an empty line) inside an inline flow paragraph: no fragment contained that offset, so the reveal was never carried out and was dropped after its timeout. It now goes to the next fragment of its text, with a test covering the case.
Closes #3214
Follows #3215, which added the range highlights this builds on.
Description
Adds
reveal_range, which scrolls the line where a range starts into view, for example the current search result, or a line deep inside a long paragraph.It works in a scrollable
TextViewand inside an app'sgpui::list, such as a chat. Other scroll containers, like adivwithoverflow_y_scroll, can use the newTextView::on_revealcallback. It gets the line's position, so the app can scroll its own container.It doesn't scroll when the line is already visible, and it follows the content the same way highlights do. It also gives up after a second if the line can't be shown, so it never jumps late.
The markdown example gets previous and next buttons. Enter and Shift+Enter in the find field do the same.
Not covered: text scrolled sideways inside a table, and a scrollable
TextViewinside an app list, which only scrolls itself.Also fixes a
RangeHighlightdoc comment that was cut off mid-sentence in #3215.Public API
gpui-base
Also available on the
gpui_component::text::TextViewwrapper.TextViewState::reveal_range(&mut self, range: Range<usize>, cx: &mut Context<Self>) -> Result<(), RangeHighlightError>: scrolls the line whererangestarts into view. Likeset_range_highlights,rangepoints into the currentrendered_text(). It's best effort:Ok(())means the request was taken, not that the view has scrolled.TextView::on_reveal(self, handler: impl Fn(Bounds<Pixels>, &mut Window, &mut App) + 'static) -> Self: lets a container that ignores scroll requests follow a reveal, using the line's bounds in window coordinates.How to Test
cargo test -p gpui-base --lib text::cargo run -p example-markdown, search in the "Find in preview" field, then step through the matches with the arrows or Enter / Shift+Enter.Checklist
cargo runfor story tests related to the changes.