Add support for multiple types, see Readme:Usage
This commit is contained in:
26
README.md
26
README.md
@@ -44,14 +44,34 @@ use {
|
||||
|
||||
## Usage
|
||||
|
||||
I exposed a command `:Neogen` to generate the annotations.
|
||||
I exposed a function to generate the annotations.
|
||||
|
||||
```lua
|
||||
require('neogen').generate()
|
||||
```
|
||||
|
||||
You can bind it to your keybind of choice, like so:
|
||||
|
||||
```lua
|
||||
vim.api.nvim_set_keymap("n", "<Leader>ng", ":Neogen<CR>", {})
|
||||
vim.api.nvim_set_keymap("n", "<Leader>ng", ":lua require('neogen').generate()<CR>", {})
|
||||
```
|
||||
|
||||
Calling the `generate` function without any parameters will try to generate annotations for the current function.
|
||||
|
||||
You can provide some options for the generate, like so:
|
||||
|
||||
```lua
|
||||
require('neogen').generate({
|
||||
type = "func" -- the annotation type to generate. Currently supported: func, class
|
||||
})
|
||||
```
|
||||
|
||||
For example, I can add an other keybind to generate class annotations:
|
||||
|
||||
```lua
|
||||
vim.api.nvim_set_keymap("n", "<Leader>nc", ":lua require('neogen').generate({ type = 'class' })<CR>", {})
|
||||
```
|
||||
|
||||
It'll generate the annotations provided by neogen.
|
||||
|
||||
## Configuration
|
||||
|
||||
|
||||
@@ -13,7 +13,10 @@ require("neogen.locators.default")
|
||||
require("neogen.granulators.default")
|
||||
require("neogen.generators.default")
|
||||
|
||||
neogen.auto_generate = function(custom_template)
|
||||
neogen.generate = function(opts)
|
||||
opts = opts or {
|
||||
type = "func",
|
||||
}
|
||||
vim.treesitter.get_parser(0):for_each_tree(function(tree, language_tree)
|
||||
local language = neogen.configuration.languages[language_tree:lang()]
|
||||
|
||||
@@ -22,26 +25,26 @@ neogen.auto_generate = function(custom_template)
|
||||
language.granulator = language.granulator or neogen.default_granulator
|
||||
language.generator = language.generator or neogen.default_generator
|
||||
|
||||
if not language.parent[opts.type] or not language.data[opts.type] then
|
||||
return
|
||||
end
|
||||
|
||||
-- Use the language locator to locate one of the required parent nodes above the cursor
|
||||
local located_parent_node = language.locator({
|
||||
root = tree:root(),
|
||||
current = ts_utils.get_node_at_cursor(0),
|
||||
}, language.parent)
|
||||
}, language.parent[opts.type])
|
||||
|
||||
if not located_parent_node then
|
||||
return
|
||||
end
|
||||
|
||||
-- Use the language granulator to get the required content inside the node found with the locator
|
||||
local data = language.granulator(located_parent_node, language.data)
|
||||
local data = language.granulator(located_parent_node, language.data[opts.type])
|
||||
|
||||
if data then
|
||||
-- Will try to generate the documentation from a template and the data found from the granulator
|
||||
local to_place, start_column, content = language.generator(
|
||||
located_parent_node,
|
||||
data,
|
||||
custom_template or language.template
|
||||
)
|
||||
local to_place, start_column, content = language.generator(located_parent_node, data, language.template)
|
||||
|
||||
-- Append the annotation in required place
|
||||
vim.fn.append(to_place, content)
|
||||
@@ -57,7 +60,7 @@ neogen.auto_generate = function(custom_template)
|
||||
end
|
||||
|
||||
function neogen.generate_command()
|
||||
vim.api.nvim_command('command! -range -bar Neogen lua require("neogen").auto_generate()')
|
||||
vim.api.nvim_command('command! -range -bar Neogen lua require("neogen").generate()')
|
||||
end
|
||||
|
||||
neogen.setup = function(opts)
|
||||
|
||||
@@ -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