Skip to content
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

Open
Mephistophiles opened this issue Aug 21, 2022 · 3 comments
Open

enhancement: add treesitter queries support for augend.user #30

Mephistophiles opened this issue Aug 21, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@Mephistophiles
Copy link

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().

@monaqa
Copy link
Owner

monaqa commented Aug 22, 2022

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.

@Mephistophiles
Copy link
Author

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(leftbufnr), vim.treesitter.get_node_text(rightbufnr)),
			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

@monaqa monaqa added the enhancement New feature or request label Aug 28, 2022
@monaqa
Copy link
Owner

monaqa commented Oct 17, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants