I'm trying to write a custom injection for Neovim's Treesitter that will highlight tagged template literals as SQL code.
The code in question looks like this:
import sql from "postgres"
const query = sql` SELECT * FROM my_table`
I'd like to tell Treesitter to parse this tagged template literal as SQL code. This is possible using Treesitter's custom injections; in fact the library uses this same technique to inject syntax for GraphQL tagged template literals, which I believe happens in this file here.
I've written a configuration file at ~/.config/nvim/after/queries/ecma/injections.scm
that's quite similar to the GraphQL query, and although Treesitter is recognizing the file, the injection isn't working at all. Does anyone have a solution to this? Ideally, Treesitter would see the sql
tagged template literal and interpret the body of the function with the SQL parser.
Similar to what @brotify-force said, but my own SQL injection for Python was not working until I realized what the issue was.
~/.config/nvim/after/queries/python/injections.scm
My original query used @id, but it seems like if you have any captures not starting with an underscore besides the language capture, the Neovim Treesitter machinery gets confused.
Update: Treesitter / Neovim 12/2023 I recently noticed my SQL injections were broken. It looks like maybe there were changes to what symbols get parsed by treesitter as well as some special syntax to indicate the injected language, etc.
My updated file at ~/.config/nvim/after/queries/python/injections.scm: