Skip to content

text_view: Expose range highlighting and scroll-to-range APIs #3214

Description

@suxiaoshao

Description

Please expose generic range highlighting and scroll-to-range capabilities for rendered Markdown in TextView / TextViewState.

Applications can already decide which documents to search, compute matches, and manage next/previous navigation. However, they cannot apply those results to arbitrary text ranges in a rendered TextView or reveal the exact location inside a long paragraph through the public API.

This would also support citation navigation and annotations. Search queries, matching algorithms, result counts, and the active-result index should remain application responsibilities; this request is not for a built-in search panel or search engine.

Expected capabilities

  • Apply, update, and clear application-supplied range highlights. Applications should be able to give different ranges different styles, for example a stronger background for the currently active result.
  • Reveal a specified text range/location in the viewport, including when the TextView is inside an application-owned scrolling container. The component should handle its parsing/layout timing so the application does not need to guess pixel positions or add polling loops.
  • Preserve existing Markdown layout, wrapping, syntax colors, link behavior, text selection, and copying. Changing highlights should not require rewriting or reparsing the Markdown source; reuse existing text layout where possible.
  • Define the range coordinate space and its relationship to rendered text clearly. For example, an application may find hello world in the rendered output of hello **world**; it needs a reliable way to address that rendered span. UTF-8 boundaries, escaped characters, and repeated identical text should not require applications to guess offsets by searching the source again. The exact API and coordinate representation are open for discussion.

The library does not need to own a concept of “current search match.” The application can choose the styles and request scrolling for the selected range.

Current API and related work

Checked against main at ce9267130ae030db4fc3bdec263212a0ab16045c.

list_state() and bounds() expose list/overall geometry, but do not directly locate a specific substring inside a long rendered block. The code-block highlighter hook is also limited to code blocks and does not cover ordinary Markdown paragraphs.

Reference implementation

Zed's Markdown component keeps source-to-rendered mappings and paints range highlights using its existing text layout. It also exposes an autoscroll request for a source position:

These are implementation references, not a request to copy Zed's search-specific API names or matching policy.

Example validation

Highlight and navigate among multiple occurrences in a long Markdown document containing ordinary text, emphasis, links, code blocks, and tables. Check that wrapping and selection/copy remain unchanged, the correct repeated occurrence is revealed, and updates to streamed content do not apply stale ranges to unrelated text.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions