Treesitter Injection with predicates?

I’m trying to get bash to inject a language for an eval statement. This is a relatively common use-case that I’d love to see highlighted but I don’t seem to be able to put any kind of predicates.

As an example, given the following command for filetype bash,

psql -U username -d mydatabase -c 'SELECT * FROM mytable'

and the following query,

(
 command 
  name:(command_name (word) @cmd)
  (#eq? @cmd "psql")
  argument: (word) @arg
  (#eq? @arg "-c")
  argument: (raw_string) @sql
  (#offset! @sql 0 1 0 -1)
) 

I would expect the sql statement to be pulled out and highlighted. It doesn’t do anything at all. If I set the query to JUST below, it works:

 command 
  argument: (raw_string) @sql
  (#offset! @sql 0 1 0 -1)
) 

But this doesn’t change the style based on the command being run, it will just highlight any raw string as sql.

Any way to get this working?

1 Like

Looks like you need to prefix the predicates with _ for it to work. eg:

(
 command 
  name: (command_name) @_cmd (#eq? @_cmd "psql")
  argument: (word) @_arg (#eq? @_arg "-c") 
  argument: (raw_string) @sql (#offset! @sql 0 1 0 -1)
)
1 Like