code.mdx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ---
  2. title: Code
  3. description: Syntax-highlighted code display component with Tree-sitter support
  4. order: 10
  5. ---
  6. # Code
  7. Displays syntax-highlighted code using Tree-sitter. Supports many languages with accurate, fast highlighting.
  8. ## Basic usage
  9. ### Renderable API
  10. ```typescript
  11. import { CodeRenderable, createCliRenderer, SyntaxStyle, RGBA } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const syntaxStyle = SyntaxStyle.fromStyles({
  14. keyword: { fg: RGBA.fromHex("#FF7B72"), bold: true },
  15. string: { fg: RGBA.fromHex("#A5D6FF") },
  16. comment: { fg: RGBA.fromHex("#8B949E"), italic: true },
  17. number: { fg: RGBA.fromHex("#79C0FF") },
  18. function: { fg: RGBA.fromHex("#D2A8FF") },
  19. default: { fg: RGBA.fromHex("#E6EDF3") },
  20. })
  21. const code = new CodeRenderable(renderer, {
  22. id: "code",
  23. content: `function hello() {
  24. // This is a comment
  25. const message = "Hello, world!"
  26. return message
  27. }`,
  28. filetype: "javascript",
  29. syntaxStyle,
  30. width: 50,
  31. height: 10,
  32. })
  33. renderer.root.add(code)
  34. ```
  35. ### Construct API
  36. ```typescript
  37. import { Code, Box, createCliRenderer, SyntaxStyle, RGBA } from "@opentui/core"
  38. const renderer = await createCliRenderer()
  39. const syntaxStyle = SyntaxStyle.fromStyles({
  40. keyword: { fg: RGBA.fromHex("#FF7B72"), bold: true },
  41. string: { fg: RGBA.fromHex("#A5D6FF") },
  42. default: { fg: RGBA.fromHex("#E6EDF3") },
  43. })
  44. renderer.root.add(
  45. Box(
  46. { border: true, width: 50, height: 10 },
  47. Code({
  48. content: 'const x = "hello"',
  49. filetype: "javascript",
  50. syntaxStyle,
  51. }),
  52. ),
  53. )
  54. ```
  55. ## Creating syntax styles
  56. Use `SyntaxStyle.fromStyles()` to define colors and attributes for syntax tokens:
  57. ```typescript
  58. import { SyntaxStyle, RGBA, parseColor } from "@opentui/core"
  59. const syntaxStyle = SyntaxStyle.fromStyles({
  60. // Basic tokens
  61. keyword: { fg: RGBA.fromHex("#FF7B72"), bold: true },
  62. "keyword.import": { fg: RGBA.fromHex("#FF7B72"), bold: true },
  63. "keyword.operator": { fg: RGBA.fromHex("#FF7B72") },
  64. string: { fg: RGBA.fromHex("#A5D6FF") },
  65. comment: { fg: RGBA.fromHex("#8B949E"), italic: true },
  66. number: { fg: RGBA.fromHex("#79C0FF") },
  67. boolean: { fg: RGBA.fromHex("#79C0FF") },
  68. constant: { fg: RGBA.fromHex("#79C0FF") },
  69. // Functions and types
  70. function: { fg: RGBA.fromHex("#D2A8FF") },
  71. "function.call": { fg: RGBA.fromHex("#D2A8FF") },
  72. "function.method.call": { fg: RGBA.fromHex("#D2A8FF") },
  73. type: { fg: RGBA.fromHex("#FFA657") },
  74. constructor: { fg: RGBA.fromHex("#FFA657") },
  75. // Variables and properties
  76. variable: { fg: RGBA.fromHex("#E6EDF3") },
  77. "variable.member": { fg: RGBA.fromHex("#79C0FF") },
  78. property: { fg: RGBA.fromHex("#79C0FF") },
  79. // Operators and punctuation
  80. operator: { fg: RGBA.fromHex("#FF7B72") },
  81. punctuation: { fg: RGBA.fromHex("#F0F6FC") },
  82. "punctuation.bracket": { fg: RGBA.fromHex("#F0F6FC") },
  83. "punctuation.delimiter": { fg: RGBA.fromHex("#C9D1D9") },
  84. // Default fallback
  85. default: { fg: RGBA.fromHex("#E6EDF3") },
  86. })
  87. ```
  88. ### Style properties
  89. Each style definition can include:
  90. | Property | Type | Description |
  91. | ----------- | --------- | ----------------------- |
  92. | `fg` | `RGBA` | Foreground (text) color |
  93. | `bg` | `RGBA` | Background color |
  94. | `bold` | `boolean` | Bold text |
  95. | `italic` | `boolean` | Italic text |
  96. | `underline` | `boolean` | Underlined text |
  97. | `dim` | `boolean` | Dimmed text |
  98. ## Supported languages
  99. Code uses Tree-sitter for parsing. Tree-sitter supports these languages:
  100. - TypeScript / JavaScript
  101. - Markdown
  102. - Zig
  103. - And any language with a Tree-sitter grammar
  104. ## Streaming mode
  105. Enable streaming mode when content arrives incrementally, like LLM output:
  106. ```typescript
  107. const code = new CodeRenderable(renderer, {
  108. id: "streaming-code",
  109. content: "",
  110. filetype: "typescript",
  111. syntaxStyle,
  112. streaming: true, // Enable streaming mode
  113. })
  114. // Later, append content
  115. code.content += "const x = 1\n"
  116. code.content += "const y = 2\n"
  117. ```
  118. Streaming mode optimizes highlighting for incremental updates.
  119. ## Text selection
  120. Enable text selection for copy operations:
  121. ```typescript
  122. const code = new CodeRenderable(renderer, {
  123. id: "code",
  124. content: sourceCode,
  125. filetype: "typescript",
  126. syntaxStyle,
  127. selectable: true,
  128. selectionBg: "#264F78",
  129. selectionFg: "#FFFFFF",
  130. })
  131. ```
  132. ## Concealment
  133. The `conceal` option controls whether certain syntax elements (like markdown formatting characters) are hidden:
  134. ```typescript
  135. const code = new CodeRenderable(renderer, {
  136. id: "markdown",
  137. content: "# Heading\n**bold** text",
  138. filetype: "markdown",
  139. syntaxStyle,
  140. conceal: true, // Hide formatting characters
  141. })
  142. ```
  143. ## With line numbers
  144. Use `LineNumberRenderable` to add line numbers:
  145. ```typescript
  146. import { CodeRenderable, LineNumberRenderable, RGBA, ScrollBoxRenderable, SyntaxStyle } from "@opentui/core"
  147. const sourceCode = "const answer = 42\nconsole.log(answer)\n"
  148. const syntaxStyle = SyntaxStyle.fromStyles({
  149. default: { fg: RGBA.fromHex("#E6EDF3") },
  150. })
  151. const code = new CodeRenderable(renderer, {
  152. id: "code",
  153. content: sourceCode,
  154. filetype: "typescript",
  155. syntaxStyle,
  156. width: "100%",
  157. })
  158. const lineNumbers = new LineNumberRenderable(renderer, {
  159. id: "code-with-lines",
  160. target: code,
  161. minWidth: 3,
  162. paddingRight: 1,
  163. fg: "#6b7280",
  164. bg: "#161b22",
  165. width: "100%",
  166. })
  167. // Wrap in ScrollBox for scrolling
  168. const scrollbox = new ScrollBoxRenderable(renderer, {
  169. id: "scrollbox",
  170. width: 60,
  171. height: 20,
  172. })
  173. scrollbox.add(lineNumbers)
  174. renderer.root.add(scrollbox)
  175. ```
  176. ## Properties
  177. | Property | Type | Default | Description |
  178. | ------------------ | ------------------ | -------- | ---------------------------------------- |
  179. | `content` | `string` | `""` | Source code to display |
  180. | `filetype` | `string` | - | Language for syntax highlighting |
  181. | `syntaxStyle` | `SyntaxStyle` | required | Syntax highlighting theme |
  182. | `streaming` | `boolean` | `false` | Optimize for incremental content updates |
  183. | `conceal` | `boolean` | `true` | Hide concealed syntax elements |
  184. | `drawUnstyledText` | `boolean` | `true` | Show text before highlighting completes |
  185. | `treeSitterClient` | `TreeSitterClient` | - | Custom Tree-sitter client instance |
  186. ### Inherited from TextBufferRenderable
  187. | Property | Type | Default | Description |
  188. | -------------- | ------------------ | -------- | ------------------------------------------- |
  189. | `fg` | `string \| RGBA` | - | Default foreground color |
  190. | `bg` | `string \| RGBA` | - | Background color |
  191. | `selectable` | `boolean` | `false` | Enable text selection |
  192. | `selectionBg` | `string \| RGBA` | - | Selection background color |
  193. | `selectionFg` | `string \| RGBA` | - | Selection foreground color |
  194. | `wrapMode` | `string` | `"word"` | Text wrapping: `"none"`, `"char"`, `"word"` |
  195. | `tabIndicator` | `string \| number` | - | Tab display character or width |
  196. ## Additional properties
  197. | Property | Type | Description |
  198. | ---------------- | --------- | -------------------------------------------- |
  199. | `lineCount` | `number` | Number of lines in content |
  200. | `scrollY` | `number` | Current vertical scroll position (get/set) |
  201. | `scrollX` | `number` | Current horizontal scroll position (get/set) |
  202. | `scrollWidth` | `number` | Total scrollable width (read-only) |
  203. | `scrollHeight` | `number` | Total scrollable height (read-only) |
  204. | `isHighlighting` | `boolean` | Whether highlighting is in progress |
  205. | `plainText` | `string` | Raw text content |
  206. ## Markdown styles
  207. For markdown highlighting, use markup-prefixed style names:
  208. ```typescript
  209. const markdownStyle = SyntaxStyle.fromStyles({
  210. "markup.heading": { fg: RGBA.fromHex("#58A6FF"), bold: true },
  211. "markup.heading.1": { fg: RGBA.fromHex("#00FF88"), bold: true, underline: true },
  212. "markup.heading.2": { fg: RGBA.fromHex("#00D7FF"), bold: true },
  213. "markup.bold": { fg: RGBA.fromHex("#F0F6FC"), bold: true },
  214. "markup.strong": { fg: RGBA.fromHex("#F0F6FC"), bold: true },
  215. "markup.italic": { fg: RGBA.fromHex("#F0F6FC"), italic: true },
  216. "markup.list": { fg: RGBA.fromHex("#FF7B72") },
  217. "markup.quote": { fg: RGBA.fromHex("#8B949E"), italic: true },
  218. "markup.raw": { fg: RGBA.fromHex("#A5D6FF") },
  219. "markup.raw.block": { fg: RGBA.fromHex("#A5D6FF") },
  220. "markup.link": { fg: RGBA.fromHex("#58A6FF"), underline: true },
  221. "markup.link.url": { fg: RGBA.fromHex("#58A6FF"), underline: true },
  222. default: { fg: RGBA.fromHex("#E6EDF3") },
  223. })
  224. ```