feat: Allow recursive extractions
This commit is contained in:
@@ -2,11 +2,11 @@ local ts_utils = require("nvim-treesitter.ts_utils")
|
||||
|
||||
neogen.utilities.extractors = {
|
||||
--- Extract the content from each node from data
|
||||
--- @param _ any self
|
||||
--- @param self any
|
||||
--- @param data table a list of k,v values where k is the node_type and v a table of nodes
|
||||
--- @param opts table|nil optional configurations (supported: type = true will get node's type instead of node's text)
|
||||
--- @return any result the same table as data but with node texts instead
|
||||
extract_from_matched = function(_, data, opts)
|
||||
extract_from_matched = function(self, data, opts)
|
||||
opts = opts or {}
|
||||
local result = {}
|
||||
for k, v in pairs(data) do
|
||||
@@ -16,10 +16,12 @@ neogen.utilities.extractors = {
|
||||
local get_text = function(node)
|
||||
return ts_utils.get_node_text(node)[1]
|
||||
end
|
||||
if opts.type == true then
|
||||
result[k] = vim.tbl_map(get_type, v)
|
||||
else
|
||||
result[k] = vim.tbl_map(get_text, v)
|
||||
|
||||
if type(v) == "table" then
|
||||
result[k] = self:extract_from_matched(v, opts)
|
||||
elseif type(v) == "userdata" then
|
||||
local cb = opts.type and get_type or get_text
|
||||
result[k] = cb(v)
|
||||
end
|
||||
end
|
||||
return result
|
||||
|
||||
@@ -91,7 +91,13 @@ neogen.utilities.nodes = {
|
||||
if result[name] == nil then
|
||||
result[name] = {}
|
||||
end
|
||||
table.insert(result[name], child)
|
||||
local res = child
|
||||
if subtree.subtree then
|
||||
res = { res }
|
||||
local nodes = self:matching_nodes_from(child, subtree.subtree)
|
||||
table.insert(res, nodes)
|
||||
end
|
||||
table.insert(result[name], res)
|
||||
else
|
||||
local nodes = self:matching_nodes_from(child, subtree.subtree, result)
|
||||
result = vim.tbl_deep_extend("keep", result, nodes)
|
||||
|
||||
Reference in New Issue
Block a user