Skip to content

feat(core): Add highlighting callback for custom inline highlighting#568

Merged
kommander merged 4 commits intoanomalyco:mainfrom
jettptacek:main
Jan 24, 2026
Merged

feat(core): Add highlighting callback for custom inline highlighting#568
kommander merged 4 commits intoanomalyco:mainfrom
jettptacek:main

Conversation

@jettptacek
Copy link
Contributor

@jettptacek jettptacek commented Jan 21, 2026

Adds an onHighlight callback to CodeRenderable that allows post-processing of syntax highlights before they're rendered. This enables custom inline highlighting

Changes

  • Added onHighlight option to CodeRenderable that receives tree-sitter highlights and can modify them
  • Added HighlightContext interface providing access to content, filetype, and syntaxStyle

Usage

const codeDisplay = new CodeRenderable(renderer, {
  content: code,
  filetype: "typescript",
  syntaxStyle,
  onHighlight: (highlights, ctx) => {
    highlights.push([startPos, endPos, "(registered style) i.e color.FF0000", {}])
    return highlights
  },
})

Solves

I have an open pr in opencode anomalyco/opencode#9016 to close anomalyco/opencode#2586 but my pr monkey patches treeSitterClient.highlightOnce to get that feature working and this onHighlight callback change would allow a cleaner implementation of anomalyco/opencode#2586 and/or other custom highlight features

Examples

I made 2 examples using this feature that I can add to the pull request as well. If it's wanted.

SearchHighlightingExample.mp4
ColorCodeExample.mp4

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 23, 2026

@opentui/core

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/core@568

@opentui/react

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/react@568

@opentui/solid

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/solid@568

@opentui/core-darwin-arm64

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/core-darwin-arm64@568

@opentui/core-darwin-x64

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/core-darwin-x64@568

@opentui/core-linux-arm64

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/core-linux-arm64@568

@opentui/core-linux-x64

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/core-linux-x64@568

@opentui/core-win32-arm64

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/core-win32-arm64@568

@opentui/core-win32-x64

npm i https://pkg.pr.new/anomalyco/opentui/@opentui/core-win32-x64@568

commit: 80e8b9b

@kommander
Copy link
Collaborator

Just as a tip, I'd try to avoid re-creating syntax styles in the callback itself where possible. It will not actually re-create the style or use more memory, but it has to do an FFI call with checks every time. Just something to keep in mind for repeated re-highlighting.

@kommander
Copy link
Collaborator

Oh and I'd try to get the basics test covered to catch regressions early.

@jettptacek jettptacek requested a review from simonklee as a code owner January 24, 2026 03:27
@jettptacek
Copy link
Contributor Author

jettptacek commented Jan 24, 2026

Ah yes thank you, there's no reason I should be registering styles inside the callback. I was doing that in my examples.

I did add some tests and please let know if I need to be more thorough. Thanks!

@kommander
Copy link
Collaborator

Github is not picking up the core build and test job somehow 🙄 Looks fine though. I'll try again in a bit.

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

Labels

core This relates to the core package enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: highlight hex codes with the color

3 participants