Add return statement for lua
At the moment, it only extracts return statements from direct children of the function_definition
This commit is contained in:
@@ -1,7 +0,0 @@
|
|||||||
(function (parameters) @params)
|
|
||||||
(function_definition (parameters) @params)
|
|
||||||
(local_function (parameters) @params)
|
|
||||||
|
|
||||||
(function (return_statement) @return)
|
|
||||||
(function_definition (if_statement (return_statement) @return))
|
|
||||||
(local_function (return_statement) @return)
|
|
||||||
@@ -1,7 +1,15 @@
|
|||||||
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
|
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
|
||||||
assert(ok, "neogen requires nvim-treesitter to operate :(")
|
assert(ok, "neogen requires nvim-treesitter to operate :(")
|
||||||
|
|
||||||
neogen = { }
|
neogen = {}
|
||||||
|
|
||||||
|
require("neogen.utility")
|
||||||
|
|
||||||
|
-- Require defaults
|
||||||
|
require("neogen.locators.default")
|
||||||
|
require("neogen.granulators.default")
|
||||||
|
require("neogen.generators.default")
|
||||||
|
|
||||||
|
|
||||||
neogen.auto_generate = function(custom_template)
|
neogen.auto_generate = function(custom_template)
|
||||||
vim.treesitter.get_parser(0):for_each_tree(function(tree, language_tree)
|
vim.treesitter.get_parser(0):for_each_tree(function(tree, language_tree)
|
||||||
@@ -26,7 +34,7 @@ neogen.auto_generate = function(custom_template)
|
|||||||
local data = language.granulator(located_parent_node, language.data)
|
local data = language.granulator(located_parent_node, language.data)
|
||||||
|
|
||||||
if data and not vim.tbl_isempty(data) then
|
if data and not vim.tbl_isempty(data) then
|
||||||
-- Will try to generate the documentation from a template and the data found from the granulator
|
-- Will try to generate the documentation from a template and the data found from the granulator
|
||||||
local to_place, content = language.generator(
|
local to_place, content = language.generator(
|
||||||
located_parent_node,
|
located_parent_node,
|
||||||
data,
|
data,
|
||||||
@@ -48,16 +56,11 @@ neogen.setup = function(opts)
|
|||||||
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, {
|
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, {
|
||||||
-- DEFAULT CONFIGURATION
|
-- DEFAULT CONFIGURATION
|
||||||
languages = {
|
languages = {
|
||||||
lua = require('neogen.configurations.lua'),
|
lua = require("neogen.configurations.lua"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
neogen.generate_command()
|
neogen.generate_command()
|
||||||
end
|
end
|
||||||
|
|
||||||
require('neogen.utility')
|
|
||||||
require('neogen.locators.default')
|
|
||||||
require('neogen.granulators.default')
|
|
||||||
require('neogen.generators.default')
|
|
||||||
|
|
||||||
return neogen
|
return neogen
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ local ts_utils = require("nvim-treesitter.ts_utils")
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
-- Search for these nodes
|
-- Search for these nodes
|
||||||
parent = { "function", "local_function", "local_variable_declaration", "field", "variable_declaration"},
|
parent = { "function", "local_function", "local_variable_declaration", "field", "variable_declaration" },
|
||||||
|
|
||||||
-- Traverse down these nodes and extract the information as necessary
|
-- Traverse down these nodes and extract the information as necessary
|
||||||
data = {
|
data = {
|
||||||
@@ -36,10 +36,12 @@ return {
|
|||||||
[1] = "extract",
|
[1] = "extract",
|
||||||
}, "spread")(node)
|
}, "spread")(node)
|
||||||
|
|
||||||
|
local return_statement = neogen.utility:extract_children("return_statement")(node)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
parameters = regular_params,
|
parameters = regular_params,
|
||||||
vararg = varargs,
|
vararg = varargs,
|
||||||
|
return_statement = return_statement
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
@@ -47,7 +49,7 @@ return {
|
|||||||
},
|
},
|
||||||
|
|
||||||
-- Custom lua locator that escapes from comments
|
-- Custom lua locator that escapes from comments
|
||||||
locator = require('neogen.locators.lua'),
|
locator = require("neogen.locators.lua"),
|
||||||
|
|
||||||
-- Use default granulator and generator
|
-- Use default granulator and generator
|
||||||
granulator = nil,
|
granulator = nil,
|
||||||
@@ -57,5 +59,6 @@ return {
|
|||||||
{ nil, "- " },
|
{ nil, "- " },
|
||||||
{ "parameters", "- @param %s any" },
|
{ "parameters", "- @param %s any" },
|
||||||
{ "vararg", "- @vararg any" },
|
{ "vararg", "- @vararg any" },
|
||||||
|
{ "return_statement", "- @return any" }
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
local ts_utils = require("nvim-treesitter.ts_utils")
|
local ts_utils = require("nvim-treesitter.ts_utils")
|
||||||
|
|
||||||
--- Tries to use the configuration to find all required content nodes from the parent node
|
--- Tries to use the configuration to find all required content nodes from the parent node
|
||||||
--- @param parent_node userdata the node found by the locator
|
--- @param parent_node userdata the node found by the locator
|
||||||
--- @param node_data table the data from configurations[lang].data
|
--- @param node_data table the data from configurations[lang].data
|
||||||
neogen.default_granulator = function(parent_node, node_data)
|
neogen.default_granulator = function(parent_node, node_data)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ neogen.utility = {
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
--- Extract content from specified children from a tree
|
--- Extract content from specified children from a tree
|
||||||
--- the tree parameter can be a nested { [key] = value} with key being the
|
--- the tree parameter can be a nested { [key] = value} with key being the
|
||||||
--- * key: is which children we want to extract the values from (e.g first children is 1)
|
--- * key: is which children we want to extract the values from (e.g first children is 1)
|
||||||
--- * value: "extract" or { [key] = value }. If value is "extract", it will extract the key child node
|
--- * value: "extract" or { [key] = value }. If value is "extract", it will extract the key child node
|
||||||
--- Example (extract the first child node from the first child node of the parent node):
|
--- Example (extract the first child node from the first child node of the parent node):
|
||||||
|
|||||||
Reference in New Issue
Block a user