dial.nvim
dial.nvim copied to clipboard
enhancement: add treesitter queries support for augend.user
Thanks for your great plugin! How about using treesitter for find
function in augend.user
(add a new helper)? treesitter can return textrange for found node via https://neovim.io/doc/user/treesitter.html#tsnode:range().
Thanks for the suggestion. The integration with nvim-treesitter is very appealing, but it would require a fundamental rethinking of the current dial.nvim design to make it work. Even if we were to start implementing it, it would take a long time to support.
If you have a concrete example of a use case, it might be helpful in designing it.
Yep, my case is too complicated (perhaps this issue should be closed). While developing this example, I realized that it would not be very convenient:
-- example for replacement
if vim.g.option1 and vim.g.option2 then
print("hello")
end
local require = require('augend')
local replacement = augend.user.new {
find = require('dial.augend.common').ts_query("(if_statement (binary_expression left: (_) @left right: (_) @right) @cond) @replacement"), -- https://tree-sitter.github.io/tree-sitter/playground for visualize
add = function(text, _, cursor)
local cond = node:child(1)
local left = cond:field("left")[1]
local right = cond:field("right")[1]
print(vim.treesitter.get_node_text(left, bufnr))
print(vim.treesitter.get_node_text(right, bufnr))
print(text)
return {
text = string.format("%s or %s", vim.treesitter.get_node_text(left,·bufnr), vim.treesitter.get_node_text(right,·bufnr)),
cursor = cursor,
}
end,
}
local bufnr = 1
local query = vim.treesitter.parse_query(
"lua",
"(if_statement (binary_expression left: (_) @op right: (_) @op) @cond) @replacement"
)
local parser = vim.treesitter.get_parser(bufnr, "lua", {})
local tree = parser:parse()[1]
for id, node, metadata in query:iter_captures(tree:root(), bufnr, 0, -1) do
local name = query.captures[id]
local text = vim.treesitter.get_node_text(node, bufnr)
if name == "replacement" then -- user cb
local cond = node:child(1)
local left = cond:field("left")[1]
local right = cond:field("right")[1]
print(vim.treesitter.get_node_text(left, bufnr))
print(vim.treesitter.get_node_text(right, bufnr))
print(text)
end
end
Sorry for the late reply. Sounds interesting. I thought about keeping the current dial.nvim structure as much as possible, but I think it is necessary to reconsider from the fundamental framework because the current structure is still too restrictive. Please give me some time.