NeoVim 0.10: Language injection for SQL highlighting inside Rust code

167 views Asked by At

I try to define a Treesitter injection to syntax highlight SQL inside Rust code. I figured out that the API has changed a bit since this video, so my query now looks like this:

(
 (attribute_item
   (attribute
     (identifier) @ident (#any-of? @ident "select" "insert")
    )
   ) 
 .
 (function_item
   body: (block (string_literal) @injection.content)
 )
 (#set! injection.language "sql")
)

It is supposed to match strings in functions like this one:

#[select]
fn validate(token: &String) -> i32 {
    "select user_id from login_token where token = $1"
}

The query works fine in the built-in Treesitter playground of NeoVim 0.10 and matches the correct string. But if I copy the query into after/queries/rust/injections.scm it does not work.

I have a really hard time debugging what's going wrong. First I thought the file might just be in the wrong place, might be ignored, ... but then I realized that the double quotes might be the problem. My query matches the string_literal including the double quotes, so the SQL parser might still just see a string. I should probably just select the inner part of the string by using

(#offset! @injection.content 0 1 0 -1)

but I cannot get it to work, i.e. don't understand where to put it exactly. Can somebody tell me how to do that or correct me, in case I'm on the completely wrong path!?

0

There are 0 answers