Files
neogen/lua/neogen/granulator.lua
Amaan Qureshi a9641d131c Remove usage of nvim-treesitter utilities (#152)
`vim.treesitter` is now shipped, therefore nvim-treesitter plugin is not needed anymore as a requirement.
References to nvim-treesitter was deleted in the code and the README points to `:h treesitter-parsers`

---------

Co-authored-by: danymat <d.danymat@gmail.com>
2024-03-03 21:42:36 +01:00

44 lines
1.5 KiB
Lua

local helpers = require("neogen.utilities.helpers")
--- Tries to use the configuration to find all required content nodes from the parent node
---@param parent_node TSNode the node found by the locator
---@param node_data table the data from configurations[lang].data
return function(parent_node, node_data)
local result = {}
if not parent_node then
return result
end
for parent_type, child_data in pairs(node_data) do
local matches = helpers.split(parent_type, "|", true)
-- Look if the parent node is one of the matches
if vim.tbl_contains(matches, parent_node:type()) then
-- For each child_data in the matched parent node
for i, data in pairs(child_data) do
local index = tonumber(i)
assert(index, "Need a valid index")
local child_node = index == 0 and parent_node or parent_node:named_child(index - 1)
if not child_node then
return
end
if not data.match or child_node:type() == data.match then
if not type(data.extract) == "function" then
return
end
-- Extract content from it { [type] = { data } }
for type, extracted_data in pairs(data.extract(child_node)) do
result[type] = extracted_data
end
end
end
end
end
return result
end