Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Injections in Neovim Treesitter with Tagged Template Literals

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.

like image 319
Harrison Cramer Avatar asked Sep 17 '25 14:09

Harrison Cramer


1 Answers

to make tree sitter use injections in your after/ folder, your injections.scm file should contain ; extends as first line.

Took me many hours to figure it out, it's now documented right here

like image 119
Brotify Force Avatar answered Sep 19 '25 06:09

Brotify Force