Add support for multiple types, see Readme:Usage
This commit is contained in:
@@ -15,38 +15,40 @@ local function_tree = {
|
||||
},
|
||||
}
|
||||
return {
|
||||
parent = { "function_declaration", "expression_statement", "variable_declaration" },
|
||||
parent = { func = { "function_declaration", "expression_statement", "variable_declaration" }, },
|
||||
|
||||
data = {
|
||||
["function_declaration"] = {
|
||||
["0"] = {
|
||||
func = {
|
||||
["function_declaration"] = {
|
||||
["0"] = {
|
||||
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
local tree = function_tree
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local res = neogen.utilities.extractors:extract_from_matched(nodes)
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
local tree = function_tree
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local res = neogen.utilities.extractors:extract_from_matched(nodes)
|
||||
|
||||
results.parameters = res.identifier
|
||||
results.return_statement = res.return_statement
|
||||
return results
|
||||
end,
|
||||
results.parameters = res.identifier
|
||||
results.return_statement = res.return_statement
|
||||
return results
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
["expression_statement|variable_declaration"] = {
|
||||
["1"] = {
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
local tree = { { retrieve = "all", node_type = "function", subtree = function_tree } }
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local res = neogen.utilities.extractors:extract_from_matched(nodes)
|
||||
["expression_statement|variable_declaration"] = {
|
||||
["1"] = {
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
local tree = { { retrieve = "all", node_type = "function", subtree = function_tree } }
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local res = neogen.utilities.extractors:extract_from_matched(nodes)
|
||||
|
||||
results.parameters = res.identifier
|
||||
results.return_statement = res.return_statement
|
||||
return results
|
||||
end,
|
||||
results.parameters = res.identifier
|
||||
results.return_statement = res.return_statement
|
||||
return results
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
template = {
|
||||
|
||||
@@ -23,22 +23,49 @@ end
|
||||
|
||||
return {
|
||||
-- Search for these nodes
|
||||
parent = { "function", "local_function", "local_variable_declaration", "field", "variable_declaration" },
|
||||
parent = {
|
||||
func = { "function", "local_function", "local_variable_declaration", "field", "variable_declaration" },
|
||||
class = { "local_variable_declaration" },
|
||||
},
|
||||
|
||||
data = {
|
||||
-- When the function is inside one of those
|
||||
["local_variable_declaration|field|variable_declaration"] = {
|
||||
["2"] = {
|
||||
match = "function_definition",
|
||||
func = {
|
||||
-- When the function is inside one of those
|
||||
["local_variable_declaration|field|variable_declaration"] = {
|
||||
["2"] = {
|
||||
match = "function_definition",
|
||||
|
||||
extract = common_function_extractor,
|
||||
extract = common_function_extractor,
|
||||
},
|
||||
},
|
||||
-- When the function is in the root tree
|
||||
["function_definition|function|local_function"] = {
|
||||
["0"] = {
|
||||
|
||||
extract = common_function_extractor,
|
||||
},
|
||||
},
|
||||
},
|
||||
-- When the function is in the root tree
|
||||
["function_definition|function|local_function"] = {
|
||||
["0"] = {
|
||||
|
||||
extract = common_function_extractor,
|
||||
class = {
|
||||
["local_variable_declaration"] = {
|
||||
["0"] = {
|
||||
extract = function(node)
|
||||
local tree = {
|
||||
{
|
||||
retrieve = "first",
|
||||
node_type = "variable_declarator",
|
||||
subtree = {
|
||||
{ retrieve = "first", node_type = "identifier", extract = true },
|
||||
},
|
||||
},
|
||||
}
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local res = neogen.utilities.extractors:extract_from_matched(nodes)
|
||||
return {
|
||||
class_name = res.identifier,
|
||||
}
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -59,6 +86,7 @@ return {
|
||||
{ "parameters", "- @param %s any" },
|
||||
{ "vararg", "- @vararg any" },
|
||||
{ "return_statement", "- @return any" },
|
||||
{ "class_name", "- @class any" },
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2,95 +2,99 @@ local ts_utils = require("nvim-treesitter.ts_utils")
|
||||
|
||||
return {
|
||||
-- Search for these nodes
|
||||
parent = { "function_definition", "class_definition" },
|
||||
parent = { func = { "function_definition" }, class = { "class_definition" } },
|
||||
|
||||
-- Traverse down these nodes and extract the information as necessary
|
||||
data = {
|
||||
["function_definition"] = {
|
||||
["0"] = {
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
func = {
|
||||
["function_definition"] = {
|
||||
["0"] = {
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
|
||||
local tree = {
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "parameters",
|
||||
subtree = {
|
||||
{ retrieve = "all", node_type = "identifier", extract = true },
|
||||
local tree = {
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "parameters",
|
||||
subtree = {
|
||||
{ retrieve = "all", node_type = "identifier", extract = true },
|
||||
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "default_parameter",
|
||||
subtree = { { retrieve = "all", node_type = "identifier", extract = true } },
|
||||
},
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "typed_parameter",
|
||||
subtree = { { retrieve = "all", node_type = "identifier", extract = true } },
|
||||
},
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "typed_default_parameter",
|
||||
subtree = { { retrieve = "all", node_type = "identifier", extract = true } },
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "default_parameter",
|
||||
subtree = { { retrieve = "all", node_type = "identifier", extract = true } },
|
||||
},
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "typed_parameter",
|
||||
subtree = { { retrieve = "all", node_type = "identifier", extract = true } },
|
||||
},
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "typed_default_parameter",
|
||||
subtree = { { retrieve = "all", node_type = "identifier", extract = true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
retrieve = "first",
|
||||
node_type = "block",
|
||||
subtree = {
|
||||
{ retrieve = "all", node_type = "return_statement", extract = true },
|
||||
{
|
||||
retrieve = "first",
|
||||
node_type = "block",
|
||||
subtree = {
|
||||
{ retrieve = "all", node_type = "return_statement", extract = true },
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local res = neogen.utilities.extractors:extract_from_matched(nodes)
|
||||
}
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local res = neogen.utilities.extractors:extract_from_matched(nodes)
|
||||
|
||||
results.parameters = res.identifier
|
||||
results.return_statement = res.return_statement
|
||||
return results
|
||||
end,
|
||||
results.parameters = res.identifier
|
||||
results.return_statement = res.return_statement
|
||||
return results
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
["class_definition"] = {
|
||||
["2"] = {
|
||||
match = "block",
|
||||
class = {
|
||||
["class_definition"] = {
|
||||
["2"] = {
|
||||
match = "block",
|
||||
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
local tree = {
|
||||
{
|
||||
retrieve = "first",
|
||||
node_type = "function_definition",
|
||||
subtree = {
|
||||
{
|
||||
retrieve = "first",
|
||||
node_type = "block",
|
||||
subtree = {
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "expression_statement",
|
||||
subtree = {
|
||||
{ retrieve = "first", node_type = "assignment", extract = true },
|
||||
extract = function(node)
|
||||
local results = {}
|
||||
local tree = {
|
||||
{
|
||||
retrieve = "first",
|
||||
node_type = "function_definition",
|
||||
subtree = {
|
||||
{
|
||||
retrieve = "first",
|
||||
node_type = "block",
|
||||
subtree = {
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "expression_statement",
|
||||
subtree = {
|
||||
{ retrieve = "first", node_type = "assignment", extract = true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
local nodes = neogen.utilities.nodes:matching_nodes_from(node, tree)
|
||||
|
||||
results.attributes = {}
|
||||
for _, assignment in pairs(nodes["assignment"]) do
|
||||
local left_side = assignment:field("left")[1]
|
||||
local left_attribute = left_side:field("attribute")[1]
|
||||
table.insert(results.attributes, ts_utils.get_node_text(left_attribute)[1])
|
||||
end
|
||||
results.attributes = {}
|
||||
for _, assignment in pairs(nodes["assignment"]) do
|
||||
local left_side = assignment:field("left")[1]
|
||||
local left_attribute = left_side:field("attribute")[1]
|
||||
table.insert(results.attributes, ts_utils.get_node_text(left_attribute)[1])
|
||||
end
|
||||
|
||||
return results
|
||||
end,
|
||||
return results
|
||||
end,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user