dial.nvim icon indicating copy to clipboard operation
dial.nvim copied to clipboard

enhancement: add treesitter queries support for augend.user

Open Mephistophiles opened this issue 2 years ago • 3 comments

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

Mephistophiles avatar Aug 21 '22 10:08 Mephistophiles

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.

monaqa avatar Aug 22 '22 15:08 monaqa

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

Mephistophiles avatar Aug 23 '22 14:08 Mephistophiles

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.

monaqa avatar Oct 17 '22 13:10 monaqa