tree-sitter.mdx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ---
  2. title: Tree-sitter
  3. description: Add parsers and syntax highlighting with Tree-sitter
  4. order: 2
  5. ---
  6. # Tree-sitter
  7. OpenTUI integrates Tree-sitter for fast, accurate syntax highlighting. You can register parsers globally or per client.
  8. ## Add parsers globally
  9. Use `addDefaultParsers()` before creating clients:
  10. ```typescript
  11. import { addDefaultParsers, getTreeSitterClient } from "@opentui/core"
  12. addDefaultParsers([
  13. {
  14. filetype: "python",
  15. wasm: "https://github.com/tree-sitter/tree-sitter-python/releases/download/v0.23.6/tree-sitter-python.wasm",
  16. queries: {
  17. highlights: ["https://raw.githubusercontent.com/tree-sitter/tree-sitter-python/master/queries/highlights.scm"],
  18. },
  19. },
  20. ])
  21. const client = getTreeSitterClient()
  22. await client.initialize()
  23. ```
  24. ## Add parsers per client
  25. ```typescript
  26. import { TreeSitterClient } from "@opentui/core"
  27. const client = new TreeSitterClient({ dataPath: "./cache" })
  28. await client.initialize()
  29. client.addFiletypeParser({
  30. filetype: "rust",
  31. wasm: "https://github.com/tree-sitter/tree-sitter-rust/releases/download/v0.23.2/tree-sitter-rust.wasm",
  32. queries: {
  33. highlights: ["https://raw.githubusercontent.com/tree-sitter/tree-sitter-rust/master/queries/highlights.scm"],
  34. },
  35. })
  36. ```
  37. ## Parser configuration
  38. ```typescript
  39. interface FiletypeParserOptions {
  40. filetype: string
  41. aliases?: string[]
  42. wasm: string
  43. queries: {
  44. highlights: string[]
  45. injections?: string[]
  46. }
  47. injectionMapping?: {
  48. nodeTypes?: Record<string, string>
  49. infoStringMap?: Record<string, string>
  50. }
  51. }
  52. ```
  53. `aliases` maps additional filetype ids to the same parser assets.
  54. ## Language injections
  55. Use `queries.injections` to highlight embedded languages.
  56. - `injectionMapping.nodeTypes` maps injected node types to filetype ids.
  57. - `injectionMapping.infoStringMap` maps code fence language labels to filetype ids.
  58. ```typescript
  59. client.addFiletypeParser({
  60. filetype: "markdown",
  61. wasm: "https://github.com/tree-sitter-grammars/tree-sitter-markdown/releases/download/v0.5.1/tree-sitter-markdown.wasm",
  62. queries: {
  63. highlights: ["./assets/markdown/highlights.scm"],
  64. injections: [
  65. "https://raw.githubusercontent.com/nvim-treesitter/nvim-treesitter/refs/heads/master/queries/markdown/injections.scm",
  66. ],
  67. },
  68. injectionMapping: {
  69. nodeTypes: {
  70. inline: "markdown_inline",
  71. pipe_table_cell: "markdown_inline",
  72. },
  73. infoStringMap: {
  74. js: "javascript",
  75. jsx: "javascriptreact",
  76. ts: "typescript",
  77. tsx: "typescriptreact",
  78. },
  79. },
  80. })
  81. ```
  82. If `infoStringMap` has no match, the fence language label is used as the filetype id.
  83. ## Use local files
  84. ```typescript
  85. import pythonWasm from "./parsers/tree-sitter-python.wasm" with { type: "file" }
  86. import pythonHighlights from "./queries/python/highlights.scm" with { type: "file" }
  87. addDefaultParsers([
  88. {
  89. filetype: "python",
  90. wasm: pythonWasm,
  91. queries: {
  92. highlights: [pythonHighlights],
  93. },
  94. },
  95. ])
  96. ```
  97. ## Automated asset management
  98. Use the `updateAssets` utility to download parsers and generate imports.
  99. ### CLI usage
  100. ```json
  101. {
  102. "scripts": {
  103. "prebuild": "bun node_modules/@opentui/core/lib/tree-sitter/update-assets.js --config ./parsers-config.json --assets ./src/parsers --output ./src/parsers.ts"
  104. }
  105. }
  106. ```
  107. ### Programmatic usage
  108. ```typescript
  109. import { updateAssets } from "@opentui/core/tree-sitter/update-assets"
  110. await updateAssets({
  111. configPath: "./parsers-config.json",
  112. assetsDir: "./src/parsers",
  113. outputPath: "./src/parsers.ts",
  114. })
  115. ```
  116. ## Using with CodeRenderable
  117. ```typescript
  118. import { CodeRenderable, RGBA, SyntaxStyle, getTreeSitterClient } from "@opentui/core"
  119. const client = getTreeSitterClient()
  120. await client.initialize()
  121. const syntaxStyle = SyntaxStyle.fromStyles({
  122. default: { fg: RGBA.fromHex("#E6EDF3") },
  123. })
  124. const code = new CodeRenderable(renderer, {
  125. id: "code",
  126. content: "const x = 1",
  127. filetype: "typescript",
  128. syntaxStyle,
  129. treeSitterClient: client,
  130. })
  131. ```
  132. ## Caching
  133. Parser and query files are cached in the client `dataPath`. Set a custom cache directory:
  134. ```typescript
  135. const client = new TreeSitterClient({
  136. dataPath: "./my-cache",
  137. })
  138. ```
  139. ## File type resolution
  140. ```typescript
  141. import { pathToFiletype, extToFiletype, infoStringToFiletype } from "@opentui/core"
  142. const ft1 = pathToFiletype("src/main.rs")
  143. const ft2 = extToFiletype("ts")
  144. const ft3 = infoStringToFiletype("TSX title=Button.tsx")
  145. ```
  146. `infoStringToFiletype()` is used for fenced markdown code blocks.
  147. You can extend or override mappings at runtime:
  148. ```typescript
  149. import { extensionToFiletype, basenameToFiletype } from "@opentui/core"
  150. extensionToFiletype.set("templ", "html")
  151. basenameToFiletype.set("mytoolrc", "yaml")
  152. ```