[PR #568] [MERGED] feat(core): Add highlighting callback for custom inline highlighting #1402

Closed
opened 2026-03-14 09:34:57 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/anomalyco/opentui/pull/568
Author: @jettptacek
Created: 1/21/2026
Status: Merged
Merged: 1/24/2026
Merged by: @kommander

Base: mainHead: main


📝 Commits (4)

  • 330f744 feat(core): Add highlighting callback for custom inline highlighting
  • ed41889 Merge branch 'main' into main
  • 118f36e test(core) add CodeRenderable OnHighlight tests
  • 80e8b9b Merge branch 'main' into main

📊 Changes

2 files changed (+269 additions, -3 deletions)

View changed files

📝 packages/core/src/renderables/Code.test.ts (+221 -0)
📝 packages/core/src/renderables/Code.ts (+48 -3)

📄 Description

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 https://github.com/anomalyco/opencode/pull/9016 to close https://github.com/anomalyco/opencode/issues/2586 but my pr monkey patches treeSitterClient.highlightOnce to get that feature working and this onHighlight callback change would allow a cleaner implementation of https://github.com/anomalyco/opencode/issues/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.

https://github.com/user-attachments/assets/6f41d59c-a5ea-4e9b-b398-43bc4b9914e8

https://github.com/user-attachments/assets/f0aeb59a-5b19-4fd2-948e-e1a2e80d7b1f


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/anomalyco/opentui/pull/568 **Author:** [@jettptacek](https://github.com/jettptacek) **Created:** 1/21/2026 **Status:** ✅ Merged **Merged:** 1/24/2026 **Merged by:** [@kommander](https://github.com/kommander) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (4) - [`330f744`](https://github.com/anomalyco/opentui/commit/330f744ae549c70c2c7af5270ce04c4f537367e1) feat(core): Add highlighting callback for custom inline highlighting - [`ed41889`](https://github.com/anomalyco/opentui/commit/ed41889a842044c668ccc23600412dc1d4b315b4) Merge branch 'main' into main - [`118f36e`](https://github.com/anomalyco/opentui/commit/118f36e222871baf740006484e0feb9aae4a9ff8) test(core) add CodeRenderable OnHighlight tests - [`80e8b9b`](https://github.com/anomalyco/opentui/commit/80e8b9bcfb46c1909405b173b382445e6986b54b) Merge branch 'main' into main ### 📊 Changes **2 files changed** (+269 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/core/src/renderables/Code.test.ts` (+221 -0) 📝 `packages/core/src/renderables/Code.ts` (+48 -3) </details> ### 📄 Description 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 ```typescript 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 https://github.com/anomalyco/opencode/pull/9016 to close https://github.com/anomalyco/opencode/issues/2586 but my pr monkey patches `treeSitterClient.highlightOnce` to get that feature working and this onHighlight callback change would allow a cleaner implementation of https://github.com/anomalyco/opencode/issues/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. https://github.com/user-attachments/assets/6f41d59c-a5ea-4e9b-b398-43bc4b9914e8 https://github.com/user-attachments/assets/f0aeb59a-5b19-4fd2-948e-e1a2e80d7b1f --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-14 09:34:57 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/opentui#1402
No description provided.