Journal 2022-09-04

Nvim tree-sitter

Its easy to capture nodes with tree-sitter queries, but when it comes to match the text inside the node its complicated doing it with just the queries.

Thankfully nvim has some built in predicates that can help with it. An example is how to extract the content of a rust line_comment and inject the markdown syntax. We need to ignore the first 3 characters that are not valid markdown:

((line_comment) @markdown (#offset! @markdown 0 3 0 0))
  1. Capture the line_comment
  2. Mark it as markdown code
  3. Use the predicate offset!event.FootnoteReference
<!-- Morning --> <!-- What do I want to do today? --> <!-- Evening --> <!-- What did I learn today? --> <!-- Things I learned --> <!-- Useful tools and libraries -->
1
You can find more about built in predicates here
`:h lua-treesitter-predicates`