Skip to content

text: Add code_block_highlighter to the component TextView - #3323

Closed
madcodelife wants to merge 1 commit into
mainfrom
text-view-code-highlighter
Closed

madcodelife wants to merge 1 commit into
mainfrom
text-view-code-highlighter

Conversation

@madcodelife

@madcodelife madcodelife commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

The Base TextView takes a per-view code block highlighter, but the component TextView (gpui_kit::component::text::TextView) only highlights through the application default that component initialization installs from the theme. A view could not show its code blocks as plain text, or highlight them differently, while other views keep the default.

This adds the same builder to the component TextView. It takes precedence over the application default and over the highlight_theme of a style passed to .style(...). Returning no ranges renders that view's code blocks unhighlighted:

TextView::markdown("notes", source).code_block_highlighter(|_| Vec::new())

Views without the builder are unchanged.

Public API

gpui-component

  • TextView::code_block_highlighter<F>(self, highlighter: F) -> Self where F: Fn(&CodeBlock) -> Vec<(Range<usize>, HighlightStyle)> + Send + Sync + 'static — highlights this view's fenced code blocks with highlighter instead of the application default or the style's highlight theme; same signature and range contract as gpui_base::TextView::code_block_highlighter.

How to test

  • cargo test -p gpui-component --features tree-sitter --lib text:: — code_block_highlighter_replaces_the_default_for_one_view renders two views side by side with a recording default highlighter: the view without an override is highlighted by the default only, the overridden view (which also sets a custom highlight theme) by its override only. The test fails if the override is not applied.
  • Docs: new "Syntax Highlighting" section on the component TextView page (en and zh-CN).

Checklist

  • Reviewed the changes and confirmed they are accurate.
  • cargo clippy -p gpui-component --all-targets -- --deny warnings, with and without tree-sitter.
  • Passed cargo run for story tests related to the changes (no story change).

The Base `TextView` accepts a per-view code block highlighter, but the
component `TextView` only highlighted through the application default
installed from the theme, so one view could not render its code blocks
plainly (or differently) while the rest kept syntax highlighting.

The new builder mirrors the Base one and takes precedence over both the
application default and the highlight theme of `.style(...)`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@madcodelife
madcodelife deleted the text-view-code-highlighter branch September 30, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant