(lua) Fix nested functions sometimes not being found

This commit is contained in:
Daniel Mathiot
2021-09-12 17:45:36 +02:00
parent b77ac73661
commit 7b8631697d
3 changed files with 28 additions and 17 deletions

View File

@@ -95,7 +95,7 @@ neogen.generate = function(opts)
end
-- Creates extmark for the end of the content
neogen.utilities.cursor.create(to_place + #content+ 1, 0)
neogen.utilities.cursor.create(to_place + #content + 1, 0)
neogen.utilities.cursor.jump({ first_time = true })
end

View File

@@ -1,9 +1,9 @@
local common_function_extractor = function(node)
local function_extractor = function(node, type)
if not vim.tbl_contains({ "local", "function" }, type) then
error("Incorrect call for function_extractor")
end
local tree = {
{
retrieve = "first_recursive",
node_type = "function_definition",
subtree = {
{
retrieve = "first",
node_type = "parameters",
@@ -13,9 +13,17 @@ local common_function_extractor = function(node)
},
},
{ retrieve = "first", node_type = "return_statement", extract = true },
},
}
if type == "local" then
tree = {
{
retrieve = "first_recursive",
node_type = "function_definition",
subtree = tree,
},
}
end
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes)
@@ -59,14 +67,17 @@ return {
-- When the function is inside one of those
["local_variable_declaration|field|variable_declaration"] = {
["0"] = {
extract = common_function_extractor,
extract = function(node)
return function_extractor(node, "local")
end,
},
},
-- When the function is in the root tree
["function|local_function"] = {
["0"] = {
extract = common_function_extractor,
extract = function(node)
return function_extractor(node, "function")
end,
},
},
},