Unable to create working tree-sitter injections

I write a lot of shell scripts, and the scripts often include calling to other languages (such as Awk), so it would be nice if I was able to get syntax highlighting for these other languages in string literals:

#!/bin/sh

do stuff | awk '
/pattern/ { action }  # This string should be highlighted!
/pattern/ { action }
'

For a good while I have solved this by using a tree-sitter injection as follows:

(~/.config/nvim/after/queries/sh/injections.scm)

;; extends

(command
  name: (command_name) @injection.language
  argument: (raw_string) @injection.content)

((command
   name: (command_name) @_name
   argument: (raw_string) @injection.content)
 (#eq? @_name "gawk")
 (#set! injection.language "awk"))

This worked for a good while, but for whatever reason it stopped working a good while back. I never bothered to look into it because I was busy, but now I’m not. I see no issue with these injections, and inspected the AST via vim.treesitter.inspect_tree() shows me that the tree-sitter grammar hasn’t changed either. I am at a total loss as to why this isn’t working. Does anyone have any ideas?

There doesn’t seem to be a parser for sh because :TSInstall sh throws. (assuming you use nvim-treesitter plugin). I got your example to highlight properly by moving the scm file to ~/.config/nvim/after/queries/bash/injections.scm and doing :TSInstall bash, :TSInstall awk (even though I kept the same shebang and :echo &ft shows sh).

By the way, since you just joined, this forum is unofficial, see:

Ah thanks! That seems to have solved my issues. After renaming queries/sh to queries/bash and adding the following to my configuration, everything worked!

" after/ftdetect/bash.vim
autocmd BufRead,BufNewFile *.sh set ft=bash
1 Like

I checked in $MYVIMRC but somehow I don’t need this :person_shrugging: I also don’t have any custom ftdetect patch for *.sh as far as I can see. But if it make it work then that’s good.