Refactor
More documentation, changed function signature
This commit is contained in:
@@ -12,7 +12,7 @@ neogen.auto_generate = function(custom_template)
|
||||
language.granulator = language.granulator or neogen.default_granulator
|
||||
language.generator = language.generator or neogen.default_generator
|
||||
|
||||
-- Use the language locator to locate one the the required parent nodes above the cursor
|
||||
-- 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),
|
||||
@@ -22,15 +22,18 @@ neogen.auto_generate = function(custom_template)
|
||||
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)
|
||||
|
||||
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
|
||||
local to_place, content = language.generator(
|
||||
located_parent_node,
|
||||
data,
|
||||
custom_template or language.template
|
||||
)
|
||||
|
||||
-- Append the annotation in required place
|
||||
vim.fn.append(to_place, content)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -36,6 +36,7 @@ return {
|
||||
[1] = "extract",
|
||||
}, "spread")(node)
|
||||
|
||||
|
||||
return {
|
||||
parameters = regular_params,
|
||||
vararg = varargs,
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
local ts_utils = require("nvim-treesitter.ts_utils")
|
||||
|
||||
---Default Generator:
|
||||
---Uses the provided template to format the annotations with data found by the granulator
|
||||
--- @param parent userdata the node used to generate the annotations
|
||||
--- @param data table the data from the granulator, which is a set of [type] = results
|
||||
--- @param template table a template from the configuration
|
||||
--- @return table { line, content }, with line being the line to append the content
|
||||
neogen.default_generator = function(parent, data, template)
|
||||
local start_row, start_column, _, _ = ts_utils.get_node_range(parent)
|
||||
local commentstring, generated_template = vim.trim(vim.api.nvim_buf_get_option(0, "commentstring"):format(""))
|
||||
|
||||
if not template then
|
||||
-- Default template
|
||||
generated_template = {
|
||||
{ nil, "" },
|
||||
{ "name", " @Summary " },
|
||||
@@ -12,6 +19,7 @@ neogen.default_generator = function(parent, data, template)
|
||||
{ "return", " @Return " },
|
||||
}
|
||||
elseif type(template) == "function" then
|
||||
-- You can also pass a function as a template
|
||||
generated_template = template(parent, commentstring, data)
|
||||
else
|
||||
generated_template = template
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
local ts_utils = require("nvim-treesitter.ts_utils")
|
||||
|
||||
--- 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)
|
||||
local result = {}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ neogen.utility = {
|
||||
end
|
||||
end,
|
||||
|
||||
--- Extract content from specified children from a set of nodes
|
||||
--- Extract content from specified children from a tree
|
||||
--- 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
|
||||
|
||||
Reference in New Issue
Block a user