I use nvim and nvim-treesitter
, and I want to apply syntax highlighting to SQL strings in elixir files. But only when the corresponding SQL function is imported, since I don't want to match any 'ol function that is named execute
. E.g.
defmodule Hello do
use Ecto.Migration
def up do
print "hello"
execute """
alter table foo
add constraint foo_pos check (foo > 0),
add constraint bar_neg check (bar < 0)
"""
end
end
Here, the query:
(call
target: (identifier) @fn-name (#eq? @fn-name "execute")
(arguments
(string
(quoted_content) @str)))
matches the correct portion of the string, and does not trigger print
for e.g
But this also matches:
defmodule Bar do
def execute(str) do
# This is my own function named execute
end
def do_bar do
execute "not a SQL string"
end
end
I wanted to write something like that:
(call
target: (identifier) @defmodule-fn (#eq? @defmodule-fn "defmodule")
(do_block
(call
target: (identifier) @use-fn (#eq? @use-fn "use")
(arguments
(alias) @alias (#eq? @alias "Ecto.Migration")))
(call
target: (identifier) @fn-name (#eq? @fn-name "execute")
(arguments
(string
(quoted_content) @str)))))
So it would only start matching execute
functions, only if use Ecto.Migration
was present in the module.
That code matches both, use Ecto.Migration
as @use-fn
and execute
arguments as @str
. Not what I wanted.
I expect some kind of "and" logical operator for this pattern matching problem
P.S. Also, if there a way of adding injections only when I start up nvim in the corresponding project directory, knowing that would be amazing. I can use local .nvimrc.lua
files to run code on project startup