Skip to content
Talk to an engineerRequest access

Custom rendering

Turn a highlight result into TextSpans for your own widgets, with theme styles resolved and cached by CodeStyles.

A message reading “The webhook sent this body:” above a dark rounded box holding one line of highlighted JSON

When CodeView is more than you need — a line of code inside a chat message, a cell in a table, a widget of your own — build the spans yourself. These are the pieces CodeView and HighlightTextController are made of.

  1. A HighlightResult, from Highlighter, or from a CodeHighlighter for its cache and background isolates. A result is plain data: highlight once and keep it.
  2. A CodeStyles for a theme. It resolves each scope stack’s style once, with the inherited styles merged, and caches it. Make one per theme and keep it.
  3. Spans, from one of three functions:
Function Gives you
lineSpans(result, line, styles) The spans of one line, without its line break. For one widget per line.
documentSpans(result, styles) Every line, joined with \n. The \r of a \r\n line break is dropped, so it never renders.
exactSpans(result, styles) Spans that cover every character exactly, as a text field needs. It can underline the composing range, and colour only part of the text.

Plain text gets no style of its own, so it takes the style of the span around it. Use styles.rootStyle(base) for that: your base style with the theme’s text colour. The theme’s background is left out — paint it on the container, as the example does.

textStyleFromStyle converts a single Style from a theme into a TextStyle, leaving unset fields unset.

Text styles for the scope stacks of highlight results, resolved once per stack and cached.

Stack 0 (plain text) gets an empty style so it inherits the root span. Every other stack gets its full inherited style, so spans can be flat. The cache grows as a table gains stacks, which lets incremental results that share one table reuse it.

CodeStyles(ValTheme theme)

Creates a style cache for theme.

Field Type Default Description
theme ValTheme required The theme being applied.
Member Type Description
rootStyle(TextStyle base) TextStyle Root style: the theme’s base color (no background; containers paint that) merged over base.
of(ScopeTable table, int stack) TextStyle Style for stack in table.

Converts a val_highlight Style to a TextStyle. Unset fields stay unset, so the result inherits from its parent span.

TextStyle textStyleFromStyle(Style style)

Spans for line line of result, without the line break.

List<TextSpan> lineSpans(HighlightResult result, int line, CodeStyles styles)

Spans for every line of result, joined with \n. The \r of a \r\n line break is dropped, so it never renders.

List<TextSpan> documentSpans(HighlightResult result, CodeStyles styles)

Spans covering every character of result exactly, as a text field needs. When composing is a valid range, that range is underlined.

Only code within styled (all of it by default) is coloured; the rest is plain text, which keeps huge documents cheap to lay out. Adjacent pieces with the same style share one span.

List<TextSpan> exactSpans(
HighlightResult result,
CodeStyles styles, {
TextRange composing = TextRange.empty,
TextRange? styled,
})