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?