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:
Daniel Mathiot
2021-08-21 18:05:03 +02:00
parent ebeb5aa329
commit ec09985e9d
5 changed files with 18 additions and 19 deletions

View File

@@ -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)

View File

@@ -1,7 +1,15 @@
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
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)
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)
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(
located_parent_node,
data,
@@ -48,16 +56,11 @@ neogen.setup = function(opts)
neogen.configuration = vim.tbl_deep_extend("keep", opts or {}, {
-- DEFAULT CONFIGURATION
languages = {
lua = require('neogen.configurations.lua'),
lua = require("neogen.configurations.lua"),
},
})
neogen.generate_command()
end
require('neogen.utility')
require('neogen.locators.default')
require('neogen.granulators.default')
require('neogen.generators.default')
return neogen

View File

@@ -2,7 +2,7 @@ local ts_utils = require("nvim-treesitter.ts_utils")
return {
-- 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
data = {
@@ -36,10 +36,12 @@ return {
[1] = "extract",
}, "spread")(node)
local return_statement = neogen.utility:extract_children("return_statement")(node)
return {
parameters = regular_params,
vararg = varargs,
return_statement = return_statement
}
end,
},
@@ -47,7 +49,7 @@ return {
},
-- Custom lua locator that escapes from comments
locator = require('neogen.locators.lua'),
locator = require("neogen.locators.lua"),
-- Use default granulator and generator
granulator = nil,
@@ -57,5 +59,6 @@ return {
{ nil, "- " },
{ "parameters", "- @param %s any" },
{ "vararg", "- @vararg any" },
{ "return_statement", "- @return any" }
},
}

View File

@@ -1,6 +1,6 @@
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 node_data table the data from configurations[lang].data
neogen.default_granulator = function(parent_node, node_data)

View File

@@ -21,7 +21,7 @@ neogen.utility = {
end,
--- 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)
--- * 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):