nvim-treesitter/runtime/queries/nix/injections.scm
Sam eb3911d052
fix(nix): match writers in nested attrpaths
The current Nix injections don't mark this as bash:

```nix
pkgs.writers.writeBash "" ''
  echo Hello, world!
''
```

but do mark this as bash:

```nix
writers.............................writeBash "" ''
  echo hi
''
```

(and similar for other languages). Note that in
order to solve this issue, I had to swap from
Lua patterns to Vim regexps, which I know is less
efficient. Unfortunately Lua patterns don't let you
use modifiers on groups, so I swapped.
2026-01-24 19:24:51 -07:00

299 lines
8.3 KiB
Scheme

((comment) @injection.content
(#set! injection.language "comment"))
((comment) @injection.language
. ; this is to make sure only adjacent comments are accounted for the injections
[
(string_expression
(string_fragment) @injection.content)
(indented_string_expression
(string_fragment) @injection.content)
]
(#gsub! @injection.language "/%*%s*([%w%p]+)%s*%*/" "%1")
(#set! injection.combined))
; #-style Comments
((comment) @injection.language
. ; this is to make sure only adjacent comments are accounted for the injections
[
(string_expression
(string_fragment) @injection.content)
(indented_string_expression
(string_fragment) @injection.content)
]
(#gsub! @injection.language "#%s*([%w%p]+)%s*" "%1")
(#set! injection.combined))
(apply_expression
function: (_) @_func
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "regex")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "regex")))
]
(#lua-match? @_func "^%a*%.*match$")
(#set! injection.combined))
(binding
attrpath: (attrpath
(identifier) @_path)
expression: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
]
(#lua-match? @_path "^%a+Phase$"))
(binding
attrpath: (attrpath
(identifier) @_path)
expression: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
]
(#lua-match? @_path "^pre%a+$"))
(binding
attrpath: (attrpath
(identifier) @_path)
expression: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
]
(#lua-match? @_path "^post%a+$"))
(binding
attrpath: (attrpath
(identifier) @_path)
expression: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
]
(#lua-match? @_path "^script$"))
(apply_expression
function: (_) @_func
argument: (_
(_)*
(_
(_)*
(binding
attrpath: (attrpath
(identifier) @_path)
expression: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
])))
(#lua-match? @_func "^%a*%.*writeShellApplication$")
(#lua-match? @_path "^text$")
(#set! injection.combined))
(apply_expression
function: (apply_expression
function: (apply_expression
function: (_) @_func))
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
]
(#lua-match? @_func "^%a*%.*runCommand%a*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (_) @_func)
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
])
(#match? @_func "^([a-zA-Z]+.)*writeBash[a-zA-Z]*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (_) @_func)
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
])
(#match? @_func "^([a-zA-Z]+.)*writeDash[a-zA-Z]*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (_) @_func)
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "bash")))
])
(#lua-match? @_func "^%a*%.*writeShellScript%a*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (_) @_func)
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "fish")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "fish")))
])
(#match? @_func "^([a-zA-Z]+.)*writeFish[a-zA-Z]*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (apply_expression
function: (_) @_func))
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "haskell")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "haskell")))
])
(#match? @_func "^([a-zA-Z]+.)*writeHaskell[a-zA-Z]*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (_) @_func)
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "javascript")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "javascript")))
])
(#match? @_func "^([a-zA-Z]+.)*writeJS[a-zA-Z]*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (_) @_func)
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "perl")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "perl")))
])
(#match? @_func "^([a-zA-Z]+.)*writePerl[a-zA-Z]*$")
(#set! injection.combined))
((apply_expression
function: (apply_expression
function: (_) @_func)
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "python")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "python")))
])
(#match? @_func "^([a-zA-Z]+.)*writePy[a-zA-Z]*[0-9]*[a-zA-Z]*$")
(#set! injection.combined))
((apply_expression
function: (_) @_func
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "rust")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "rust")))
])
(#match? @_func "^([a-zA-Z]+.)*writeRust[a-zA-Z]*$")
(#set! injection.combined))
; (runTest) testScript
(apply_expression
function: (_) @_func
argument: (_
(_)*
(_
(binding
attrpath: (attrpath) @_func_name
expression: (_
(string_fragment) @injection.content
(#set! injection.language "python")))
(#eq? @_func_name "testScript")
(#lua-match? @_func "^.*%.*runTest$")
(#set! injection.combined))))
; (nixosTest) testScript
(apply_expression
function: (_) @_func
argument: (_
(_)*
(_
(binding
attrpath: (attrpath) @_func_name
expression: (_
(string_fragment) @injection.content
(#set! injection.language "python")))
(#eq? @_func_name "testScript")
(#lua-match? @_func "^.*%.*nixosTest$")
(#set! injection.combined))))
; home-manager Neovim plugin config
(attrset_expression
(binding_set
(binding
attrpath: (attrpath) @_ty_attr
(_
(string_fragment) @_ty)
(#eq? @_ty_attr "type")
(#eq? @_ty "lua"))
(binding
attrpath: (attrpath) @_cfg_attr
(_
(string_fragment) @injection.content
(#set! injection.language "lua"))
(#eq? @_cfg_attr "config")))
(#set! injection.combined))