示例:
const user = await ctx.pool.one(sql.type(userSchema)`
UPDATE users
SET avatar = NULL
WHERE id = ${currentUser.id}
RETURNING *
`);
我正在尝试使用 treesitter 注射 来做到这一点。
所以我写了以下查询:
(call_expression
function: (call_expression
function: (member_expression
object: (identifier) @_obj (#eq? @_obj "sql")))
(template_string) @injection.content
(#set! injection.language "sql"))
使用treesitter Playground检查了它(顺便说一句,如何在nvim中运行查询?似乎编辑器中不存在
:EditQuery
,但:InspectTree
存在),并将其放置在这里~/.config/nvim/after/queries/typescript/injections.scm
。
但是打字稿文件中的 SQL 代码仍然没有突出显示。我尝试使用
(#set! injection.include-children)
,但它对我也不起作用。
Treesitter 已正确安装 SQL 解析器。设置:
require'nvim-treesitter.configs'.setup {
-- A list of parser names, or "all" (the five listed parsers should always be installed)
ensure_installed = { "c", "lua", "vim", "vimdoc", "query", "javascript", "typescript", "sql" },
-- Install parsers synchronously (only applied to `ensure_installed`)
sync_install = false,
-- Automatically install missing parsers when entering buffer
-- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally
auto_install = true,
highlight = {
enable = true,
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = false,
},
}
我已经修好了。
首先,我必须添加
injection.include-children
:
(call_expression
function: (call_expression
function: (member_expression
object: (identifier) @_obj (#eq? @_obj "sql")))
(template_string) @injection.content
(#set! injection.language "sql")
(#set! injection.include-children))
其次,我把文件放错了地方。似乎
queries
目录应放置在以下路径之一:
:lua print(vim.inspect(vim.api.nvim_list_runtime_paths()))
就我而言,我只是将
queries
目录放入 ~/.config/nvim
中(之前位于 ~/config/nvim/after
中)。