Add documentation

Added docs in multiple places, refactored some functions and changed
places.
This commit is contained in:
Daniel Mathiot
2021-08-21 07:31:57 +02:00
parent c4f3725af7
commit f42e14f633
6 changed files with 50 additions and 40 deletions

View File

@@ -3,34 +3,32 @@ assert(ok, "neogen requires nvim-treesitter to operate :(")
neogen = { } neogen = { }
-- TODO: Move code here
neogen.generate = function(searcher, generator) end
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)
local searcher = neogen.configuration.languages[language_tree:lang()] local language = neogen.configuration.languages[language_tree:lang()]
if searcher then if language then
searcher.locator = searcher.locator or neogen.default_locator language.locator = language.locator or neogen.default_locator
searcher.granulator = searcher.granulator or neogen.default_granulator language.granulator = language.granulator or neogen.default_granulator
searcher.generator = searcher.generator or neogen.default_generator language.generator = language.generator or neogen.default_generator
local located_parent_node = searcher.locator({ -- Use the language locator to locate one the the required parent nodes above the cursor
local located_parent_node = language.locator({
root = tree:root(), root = tree:root(),
current = ts_utils.get_node_at_cursor(0), current = ts_utils.get_node_at_cursor(0),
}, searcher.parent) }, language.parent)
if not located_parent_node then if not located_parent_node then
return return
end end
local data = searcher.granulator(located_parent_node, searcher.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
local to_place, content = searcher.generator( local to_place, content = language.generator(
located_parent_node, located_parent_node,
data, data,
custom_template or searcher.template custom_template or language.template
) )
vim.fn.append(to_place, content) vim.fn.append(to_place, content)

View File

@@ -1,3 +1,5 @@
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"},
@@ -5,7 +7,9 @@ return {
-- Traverse down these nodes and extract the information as necessary -- Traverse down these nodes and extract the information as necessary
data = { data = {
["function|local_function"] = { ["function|local_function"] = {
-- Get second child from the parent node
["2"] = { ["2"] = {
-- It has to be of type "parameters"
match = "parameters", match = "parameters",
extract = function(node) extract = function(node)
@@ -24,13 +28,13 @@ return {
match = "function_definition", match = "function_definition",
extract = function(node) extract = function(node)
local regular_params = neogen.utility:extract_children_from("identifier", { local regular_params = neogen.utility:extract_children_from({
[1] = "extract", [1] = "extract",
})(node) }, "identifier")(node)
local varargs = neogen.utility:extract_children_from("spread", { local varargs = neogen.utility:extract_children_from({
[1] = "extract", [1] = "extract",
})(node) }, "spread")(node)
return { return {
parameters = regular_params, parameters = regular_params,
@@ -42,16 +46,7 @@ return {
}, },
-- Custom lua locator that escapes from comments -- Custom lua locator that escapes from comments
locator = function(node_info, nodes_to_match) locator = require('neogen.locators.lua'),
-- We're dealing with a lua comment and we need to escape its grasp
if node_info.current:type() == "source" then
local start_row, _, _, _ = ts_utils.get_node_range(node_info.current)
vim.api.nvim_win_set_cursor(0, { start_row, 0 })
node_info.current = ts_utils.get_node_at_cursor()
end
return neogen.default_locator(node_info, nodes_to_match)
end,
-- Use default granulator and generator -- Use default granulator and generator
granulator = nil, granulator = nil,

View File

@@ -5,10 +5,11 @@ neogen.default_granulator = function(parent_node, node_data)
for parent_type, child_data in pairs(node_data) do for parent_type, child_data in pairs(node_data) do
local matches = vim.split(parent_type, "|", true) local matches = vim.split(parent_type, "|", true)
if vim.tbl_contains(matches, parent_node:type()) then
for i, _ in pairs(node_data[parent_type]) do
local data = child_data[i]
-- Look if the parent node is one of the matches
if vim.tbl_contains(matches, parent_node:type()) then
-- For each child_data in the matched parent node
for i, data in pairs(child_data) do
local child_node = parent_node:named_child(tonumber(i) - 1) local child_node = parent_node:named_child(tonumber(i) - 1)
if not child_node then if not child_node then
@@ -19,8 +20,10 @@ neogen.default_granulator = function(parent_node, node_data)
local extract = {} local extract = {}
if data.extract then if data.extract then
-- Extract content from it { [type] = { data } }
extract = data.extract(child_node) extract = data.extract(child_node)
-- All extracted values are created added in result, like so: [data.type] = { extract }
if data.type then if data.type then
-- Extract information into a one-dimensional array -- Extract information into a one-dimensional array
local one_dimensional_arr = {} local one_dimensional_arr = {}
@@ -36,6 +39,7 @@ neogen.default_granulator = function(parent_node, node_data)
end end
end end
else else
-- if not extract function, get the text from the node (required: data.type)
extract = ts_utils.get_node_text(child_node) extract = ts_utils.get_node_text(child_node)
result[data.type] = extract result[data.type] = extract
end end

View File

@@ -1,7 +1,8 @@
---The default locator tries to find one of the nodes to match in the current node --- The default locator tries to find one of the nodes to match in the current node
--If it does not find one, will fetch the parents until he finds one --- If it does not find one, will fetch the parents until he finds one
---@param node_info table --- @param node_info table a node informations
---@param nodes_to_match table --- @param nodes_to_match table a list of parent nodes to match
--- @return node_info.current node one of the nodes to match directly above the given node
neogen.default_locator = function(node_info, nodes_to_match) neogen.default_locator = function(node_info, nodes_to_match)
-- If we find one of the wanted nodes in current one, return the current node -- If we find one of the wanted nodes in current one, return the current node
if vim.tbl_contains(nodes_to_match, node_info.current:type()) then if vim.tbl_contains(nodes_to_match, node_info.current:type()) then

View File

@@ -0,0 +1,12 @@
local ts_utils = require("nvim-treesitter.ts_utils")
return function(node_info, nodes_to_match)
-- We're dealing with a lua comment and we need to escape its grasp
if node_info.current:type() == "source" then
local start_row, _, _, _ = ts_utils.get_node_range(node_info.current)
vim.api.nvim_win_set_cursor(0, { start_row, 0 })
node_info.current = ts_utils.get_node_at_cursor()
end
return neogen.default_locator(node_info, nodes_to_match)
end

View File

@@ -21,26 +21,26 @@ neogen.utility = {
end, end,
--- Extract content from specified children from a set of nodes --- Extract content from specified children from a set of nodes
--- the nodes 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):
--- [1] = { --- [1] = {
--- [1] = "extract" --- [1] = "extract"
--- } --- }
--- @param tree table see description
--- @param name string the children we want to extract (if multiple children, separate each one with "|") --- @param name string the children we want to extract (if multiple children, separate each one with "|")
--- @param nodes table see description extract_children_from = function(self, tree, name)
extract_children_from = function(self, name, nodes)
return function(node) return function(node)
local result = {} local result = {}
for i, value in ipairs(nodes) do for i, subtree in ipairs(tree) do
local child_node = node:named_child(i - 1) local child_node = node:named_child(i - 1)
if value == "extract" then if subtree == "extract" then
return self:extract_children(name)(child_node) return self:extract_children(name)(child_node)
else else
return self:extract_children_from(name, value)(node) return self:extract_children_from(subtree, name)(node)
end end
end end