(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 end
-- Creates extmark for the end of the content -- 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 }) neogen.utilities.cursor.jump({ first_time = true })
end end

View File

@@ -1,22 +1,30 @@
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 = { local tree = {
{ {
retrieve = "first_recursive", retrieve = "first",
node_type = "function_definition", node_type = "parameters",
subtree = { subtree = {
{ { retrieve = "all", node_type = "identifier", extract = true },
retrieve = "first", { retrieve = "all", node_type = "spread", extract = true },
node_type = "parameters",
subtree = {
{ retrieve = "all", node_type = "identifier", extract = true },
{ retrieve = "all", node_type = "spread", extract = true },
},
},
{ retrieve = "first", node_type = "return_statement", extract = true },
}, },
}, },
{ 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 nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
local res = neogen.utilities.extractors:extract_from_matched(nodes) local res = neogen.utilities.extractors:extract_from_matched(nodes)
@@ -59,14 +67,17 @@ return {
-- When the function is inside one of those -- When the function is inside one of those
["local_variable_declaration|field|variable_declaration"] = { ["local_variable_declaration|field|variable_declaration"] = {
["0"] = { ["0"] = {
extract = common_function_extractor, extract = function(node)
return function_extractor(node, "local")
end,
}, },
}, },
-- When the function is in the root tree -- When the function is in the root tree
["function|local_function"] = { ["function|local_function"] = {
["0"] = { ["0"] = {
extract = function(node)
extract = common_function_extractor, return function_extractor(node, "function")
end,
}, },
}, },
}, },

View File

@@ -62,7 +62,7 @@ neogen.utilities.cursor.jumpable = function()
end end
local cursor = vim.api.nvim_win_get_cursor(0) local cursor = vim.api.nvim_win_get_cursor(0)
if cursor[1] > extm_list[#extm_list][2] or cursor[1] < extm_list[1][2] then if cursor[1] > extm_list[#extm_list][2] or cursor[1] < extm_list[1][2] then
return false return false
end end
if #extm_list > 2 then if #extm_list > 2 then
return true return true