Skip to content

Fix StyledText crash with dynamic custom_highlights#2170

Open
KlausUllrich wants to merge 1 commit into
longbridge:mainfrom
KlausUllrich:fix/styled-text-custom-highlights-crash
Open

Fix StyledText crash with dynamic custom_highlights#2170
KlausUllrich wants to merge 1 commit into
longbridge:mainfrom
KlausUllrich:fix/styled-text-custom-highlights-crash

Conversation

@KlausUllrich

Copy link
Copy Markdown
Contributor

Problem

StyledText::with_runs panics with "invalid text run" when custom_highlights are used with dynamically updating content via TextView::markdown() or TextView::html().

This affects any use case where:

  • Content updates frequently (log viewers, live data displays, streaming text)
  • with_highlights() is used to apply custom styling
  • The panic occurs at gpui/src/elements/text.rs:251

Root Cause

Two issues in text/node.rs:

1. NodeContext equality ignores custom_highlights

The PartialEq impl for NodeContext intentionally excludes custom_highlights from comparison. When highlights change but text content doesn't, GPUI's diffing skips re-rendering and reuses stale Inline elements with mismatched TextRun indices.

2. merge_custom_highlights missing bounds validation

merge_custom_highlights() clips highlight ranges to chunk boundaries but doesn't validate the result. When chunks from parsed markdown and highlights from the caller originate from different parse cycles, clipping can produce local_end > chunk_len, leading to the panic downstream.

Fix

  1. Include custom_highlights in NodeContext::PartialEq so highlight changes trigger re-rendering
  2. Add bounds validation in merge_custom_highlights() to skip highlights that exceed chunk bounds after clipping

Both changes are minimal and defensive. The equality fix ensures correctness; the bounds check adds safety for edge cases during content transitions.

Testing

Reproduced in a production log viewer with rapid content updates and custom syntax highlighting. Both fixes together eliminate the crash class.

Include custom_highlights in NodeContext::PartialEq so highlight changes
trigger re-rendering, and add bounds validation in merge_custom_highlights()
to skip highlights that exceed chunk bounds after clipping.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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