From 4f083d94c628cb46a8c78e8566359d346e3012b2 Mon Sep 17 00:00:00 2001 From: danymat Date: Wed, 3 Nov 2021 22:39:58 +0100 Subject: [PATCH] ref: Simplify recursive node conditional --- lua/neogen/utilities/nodes.lua | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lua/neogen/utilities/nodes.lua b/lua/neogen/utilities/nodes.lua index b76e102..df1fdef 100644 --- a/lua/neogen/utilities/nodes.lua +++ b/lua/neogen/utilities/nodes.lua @@ -52,7 +52,7 @@ neogen.utilities.nodes = { --- Get all required nodes from tree --- @param parent userdata the parent node - --- @param tree table a nested table : { retrieve = "all|first", node_type = node_name, subtree = tree } + --- @param tree table a nested table : { retrieve = "all|first", node_type = node_name, subtree = tree, recursive = true } --- If you want to extract the node, do not specify the subtree and instead: extract = true --- Optional: you can specify position = number instead of retrieve, and it will fetch the child node at position number --- @param result table the table of results @@ -78,17 +78,9 @@ neogen.utilities.nodes = { end end - -- Only keep first matched child node - if subtree.retrieve == "first" and #matched ~= 0 then - if subtree.recursive == true then - matched = self:recursive_find(parent, subtree.node_type, { first = true }) - else - matched = { matched[1] } - end - end - - if subtree.retrieve == "all" and subtree.recursive then - matched = self:recursive_find(parent, subtree.node_type) + if subtree.recursive then + local first = subtree.retrieve == "first" + matched = self:recursive_find(parent, subtree.node_type, { first = first }) end for _, child in pairs(matched) do