Ir al contenido

Gramática tree-sitter

tree-sitter-httui es el parser tree-sitter para el formato de bloque .httui. Da poder al syntax highlighting en cualquier editor que hable tree-sitter — Neovim, Helix, Zed, GitHub, y la desktop app en sí.

Ventana de terminal
npm install tree-sitter-httui
:TSInstall httui

(Requiere el parser registrado en nvim-treesitter — ya upstream desde v0.x.)

Ventana de terminal
git clone https://github.com/httuicom/tree-sitter-httui
cd tree-sitter-httui
tree-sitter generate
tree-sitter test

La gramática produce estos tipos de nodo top-level:

NodoQué matchea
fileArchivo .httui entero — nodo root
blockUn bloque fenced (fence + body + expect opcional)
fence_lineLa fence de apertura — lenguaje + info tokens
languagehttp o db-<conn>
info_tokenUn par key=value en la línea de fence
info_key, info_valuepartes del anterior
bodyBody del bloque (entre fence y close / próxima fence)
request_lineLínea verb HTTP + URL
verbGET, POST, etc
urlLa URL (puede contener nodos reference)
headerLínea Name: value
header_name, header_valuepartes
message_bodyBytes del body (cualquier content type)
sql_statementUna sentencia SQL (bloques DB)
expect_sectionEl bloque entero # expect:
assertionUna línea de assertion
field, operator, literalpartes de una assertion
referenceExpresión {{...}}
alias_ref, env_ref, prev_reflos tres tipos de referencia
identidentifier (nombre de alias, key de env, etc)
commentLínea # ... (fuera de la sección expect)

El default queries/highlights.scm mapea tipos de nodo a grupos de highlight:

; Verbs y lenguaje
(verb) @keyword
(language) @keyword.modifier
; Identifiers en referencias
(alias_ref (ident) @function)
(env_ref (ident) @constant)
(prev_ref) @function.builtin
; Info de fence
(info_key) @attribute
(info_value) @string
; Headers
(header_name) @property
(header_value) @string
; Assertions
(field) @variable
(operator) @operator
(literal) @number
; Comentarios
(comment) @comment

Los editores pueden sobreescribir según su tema. El default shipea con tree-sitter-httui y es lo que consumen out of the box nvim-treesitter / Zed / Helix.

(block) @fold
(expect_section) @fold
(message_body) @fold

Te deja colapsar bloques enteros en editores que respetan folds de tree-sitter.

Cuando el body es identificable, inyecta el parser de lenguaje correcto:

; Sintaxis SQL dentro de bloques DB
((block
(fence_line (language) @lang)
(body) @injection.content)
(#match? @lang "^db-")
(#set! injection.language "sql"))
; Sintaxis JSON dentro de bodies HTTP cuando Content-Type matchea
((block
(header (header_name) @hname (header_value) @hval)
(message_body) @injection.content)
(#eq? @hname "Content-Type")
(#match? @hval "application/json")
(#set! injection.language "json"))

Efecto: dentro de un bloque db-pg-staging, el highlighting SQL arranca automáticamente. Dentro de un bloque HTTP cuyo Content-Type: application/json, highlighting JSON en el body. El parser correcto para cada región sin switches manuales de modo.

Para runbooks .md, tree-sitter-markdown maneja el archivo externo y inyecta tree-sitter-httui para regiones fenced con lenguajes http / db-*:

; En injections.scm de tree-sitter-markdown (httui shipea una extensión)
((fenced_code_block
(info_string (language) @lang)
(code_fence_content) @injection.content)
(#match? @lang "^(http|db-)")
(#set! injection.language "httui"))

Resultado: obtienes highlight httui-aware dentro de archivos .md sin cambiar tu setup de markdown — solo agrega el parser httui.

Linguist de GitHub reconoce los archivos .httui desde que se agregó la entrada del lenguaje. Los bloques fenced dentro de .md con http o db-* también obtienen syntax highlight en el viewer de GitHub — los mismos query files vendoreados en Linguist.

(Si tu repo no muestra highlight httui en GitHub todavía, asegúrate de que los mappings de Linguist de tu repo incluyan el lenguaje; levanta un issue en httuicom/tree-sitter-httui para ayuda.)

La gramática es incremental — tree-sitter re-parsea solo la región editada. Números prácticos en el editor de la desktop app:

  • Archivo .httui de 1k líneas: parse completo ~3 ms
  • Update incremental de single-keystroke: ~0.2 ms
  • .md de 10k líneas con 20 bloques fenced de httui: parse completo ~12 ms

El highlight y los folds derivan del parse tree — son efectivamente gratis por keystroke una vez que el tree actualiza.

tree-sitter-httui sigue la versión del lenguaje de httui-lang:

Versión del lenguajeVersión de la gramáticaCompatibilidad
0.x0.x.ybreaking changes posibles — pin
1.01.0.0estable; 1.x.y siguientes son backward-compatible

Pin en el plugin manager de tu editor (semver de npm / lock del plugin-manager de Neovim).

Source: httuicom/tree-sitter-httui.

Ventana de terminal
git clone https://github.com/httuicom/tree-sitter-httui
cd tree-sitter-httui
npm install
tree-sitter generate # rebuild parser
tree-sitter test # corre tests de snapshot
tree-sitter parse <file> # prueba en un archivo real

Los tests viven en test/corpus/ — un .txt por caso, con el parse tree esperado. Las features nuevas del lenguaje aterrizan acá primero.