-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhancement: add treesitter queries support for augend.user #30
Comments
Thanks for the suggestion. 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. |
Thanks for your great plugin! How about using treesitter for
find
function inaugend.user
(add a new helper)? treesitter can return textrange for found node via https://neovim.io/doc/user/treesitter.html#tsnode:range().The text was updated successfully, but these errors were encountered: