ref!: Extract template codes into template dir
I possibly introduced some bugs in the process, please open an issue if so. - This is an attempt to refactor the code and provide re-use of templates (for example, doxygen or jsdoc) - I standardized the exposed returned nodes for a template (you can view it by going to lua/neogen/types/template.lua)
This commit is contained in:
@@ -2,6 +2,8 @@ local ts_utils = require("nvim-treesitter.ts_utils")
|
||||
local nodes_utils = require("neogen.utilities.nodes")
|
||||
local extractors = require("neogen.utilities.extractors")
|
||||
local locator = require("neogen.locators.default")
|
||||
local template = require("neogen.utilities.template")
|
||||
local i = require("neogen.types.template").item
|
||||
|
||||
local parent = {
|
||||
func = { "function_definition" },
|
||||
@@ -75,7 +77,7 @@ return {
|
||||
{
|
||||
retrieve = "all",
|
||||
node_type = "type",
|
||||
as = "return_type",
|
||||
as = i.ReturnTypeHint,
|
||||
extract = true,
|
||||
},
|
||||
}
|
||||
@@ -84,8 +86,8 @@ return {
|
||||
results["typed_parameters"] = {}
|
||||
for _, n in pairs(nodes["typed_parameter"]) do
|
||||
local type_subtree = {
|
||||
{ retrieve = "all", node_type = "identifier", extract = true },
|
||||
{ retrieve = "all", node_type = "type", extract = true },
|
||||
{ retrieve = "all", node_type = "identifier", extract = true, as = i.Parameter },
|
||||
{ retrieve = "all", node_type = "type", extract = true, as = i.Type },
|
||||
}
|
||||
local typed_parameters = nodes_utils:matching_nodes_from(n, type_subtree)
|
||||
typed_parameters = extractors:extract_from_matched(typed_parameters)
|
||||
@@ -100,11 +102,11 @@ return {
|
||||
end
|
||||
|
||||
-- Return type hints takes precedence over all other types for generating template
|
||||
if res["return_type"] then
|
||||
if res[i.ReturnTypeHint] then
|
||||
res["return_statement"] = nil
|
||||
res["anonymous_return"] = nil
|
||||
if res["return_type"][1] == "None" then
|
||||
res["return_type"] = nil
|
||||
if res[i.ReturnTypeHint][1] == "None" then
|
||||
res[i.ReturnTypeHint] = nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -128,13 +130,13 @@ return {
|
||||
end
|
||||
end
|
||||
|
||||
results.has_identifier = (res.typed_parameter or res.identifier) and { true } or nil
|
||||
results.type = res.type
|
||||
results.parameters = res.identifier
|
||||
results.anonymous_return = res.anonymous_return
|
||||
results.return_statement = res.return_statement
|
||||
results.return_type = res.return_type
|
||||
results.has_return = (res.return_statement or res.anonymous_return or res.return_type)
|
||||
results[i.HasParameter] = (res.typed_parameter or res.identifier) and { true } or nil
|
||||
results[i.Type] = res.type
|
||||
results[i.Parameter] = res.identifier
|
||||
results[i.ReturnAnonym] = res.anonymous_return
|
||||
results[i.Return] = res.return_statement
|
||||
results[i.ReturnTypeHint] = res[i.ReturnTypeHint]
|
||||
results[i.HasReturn] = (res.return_statement or res.anonymous_return or res[i.ReturnTypeHint])
|
||||
and { true }
|
||||
or nil
|
||||
return results
|
||||
@@ -185,17 +187,17 @@ return {
|
||||
return {}
|
||||
end
|
||||
|
||||
results.attributes = {}
|
||||
results[i.ClassAttribute] = {}
|
||||
for _, assignment in pairs(nodes["assignment"]) do
|
||||
local left_side = assignment:field("left")[1]
|
||||
local left_attribute = left_side:field("attribute")[1]
|
||||
left_attribute = ts_utils.get_node_text(left_attribute)[1]
|
||||
if left_attribute and not vim.startswith(left_attribute, "_") then
|
||||
table.insert(results.attributes, left_attribute)
|
||||
table.insert(results[i.ClassAttribute], left_attribute)
|
||||
end
|
||||
end
|
||||
if vim.tbl_isempty(results.attributes) then
|
||||
results.attributes = nil
|
||||
if vim.tbl_isempty(results[i.ClassAttribute]) then
|
||||
results[i.ClassAttribute] = nil
|
||||
end
|
||||
|
||||
return results
|
||||
@@ -226,124 +228,28 @@ return {
|
||||
granulator = nil,
|
||||
generator = nil,
|
||||
|
||||
template = {
|
||||
annotation_convention = "google_docstrings", -- required: Which annotation convention to use (default_generator)
|
||||
append = { position = "after", child_name = "comment", fallback = "block" }, -- optional: where to append the text (default_generator)
|
||||
use_default_comment = false, -- If you want to prefix the template with the default comment for the language, e.g for python: # (default_generator)
|
||||
position = function(node, type)
|
||||
if type == "file" then
|
||||
-- Checks if the file starts with #!, that means it's a shebang line
|
||||
-- We will then write file annotations just after it
|
||||
for child in node:iter_children() do
|
||||
if child:type() == "comment" then
|
||||
local start_row = child:start()
|
||||
if start_row == 0 then
|
||||
if vim.startswith(ts_utils.get_node_text(node, 0)[1], "#!") then
|
||||
return 1, 0
|
||||
template = template
|
||||
:config({
|
||||
append = { position = "after", child_name = "comment", fallback = "block" }, -- optional: where to append the text (default_generator)
|
||||
position = function(node, type)
|
||||
if type == "file" then
|
||||
-- Checks if the file starts with #!, that means it's a shebang line
|
||||
-- We will then write file annotations just after it
|
||||
for child in node:iter_children() do
|
||||
if child:type() == "comment" then
|
||||
local start_row = child:start()
|
||||
if start_row == 0 then
|
||||
if vim.startswith(ts_utils.get_node_text(node, 0)[1], "#!") then
|
||||
return 1, 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return 0, 0
|
||||
end
|
||||
return 0, 0
|
||||
end
|
||||
end,
|
||||
|
||||
google_docstrings = {
|
||||
{ nil, '""" $1 """', { no_results = true, type = { "class", "func" } } },
|
||||
{ nil, '"""$1', { no_results = true, type = { "file" } } },
|
||||
{ nil, "", { no_results = true, type = { "file" } } },
|
||||
{ nil, "$1", { no_results = true, type = { "file" } } },
|
||||
{ nil, '"""', { no_results = true, type = { "file" } } },
|
||||
{ nil, "", { no_results = true, type = { "file" } } },
|
||||
|
||||
{ nil, "# $1", { no_results = true, type = { "type" } } },
|
||||
|
||||
{ nil, '"""$1' },
|
||||
{ "has_identifier", "", { type = { "func" } } },
|
||||
{ "has_identifier", "Args:", { type = { "func" } } },
|
||||
{ "parameters", " %s ($1): $1", { type = { "func" } } },
|
||||
{ { "identifier", "type" }, " %s (%s): $1", { required = "typed_parameters", type = { "func" } } },
|
||||
{ "attributes", " %s: $1", { before_first_item = { "", "Attributes: " } } },
|
||||
{ "has_return", "", { type = { "func" } } },
|
||||
{ "has_return", "Returns:", { type = { "func" } } },
|
||||
{ "has_return", " $1", { type = { "func" } } },
|
||||
{ nil, '"""' },
|
||||
},
|
||||
numpydoc = {
|
||||
{ nil, '""" $1 """', { no_results = true, type = { "class", "func" } } },
|
||||
{ nil, '"""$1', { no_results = true, type = { "file" } } },
|
||||
{ nil, "", { no_results = true, type = { "file" } } },
|
||||
{ nil, "$1", { no_results = true, type = { "file" } } },
|
||||
{ nil, '"""', { no_results = true, type = { "file" } } },
|
||||
{ nil, "", { no_results = true, type = { "file" } } },
|
||||
|
||||
{ nil, "# $1", { no_results = true, type = { "type" } } },
|
||||
|
||||
{ nil, '"""$1' },
|
||||
{ "has_identifier", "", { type = { "func" } } },
|
||||
{ "has_identifier", "Parameters", { type = { "func" } } },
|
||||
{ "has_identifier", "----------", { type = { "func" } } },
|
||||
{
|
||||
"parameters",
|
||||
"%s : $1",
|
||||
{ after_each = " $1", type = { "func" } },
|
||||
},
|
||||
{
|
||||
{ "identifier", "type" },
|
||||
"%s : %s",
|
||||
{ after_each = " $1", required = "typed_parameters", type = { "func" } },
|
||||
},
|
||||
{ "attributes", "%s : $1", { before_first_item = { "", "Attributes", "----------" } } },
|
||||
{ "has_return", "", { type = { "func" } } },
|
||||
{ "has_return", "Returns", { type = { "func" } } },
|
||||
{ "has_return", "-------", { type = { "func" } } },
|
||||
{
|
||||
"return_type",
|
||||
"%s",
|
||||
{ after_each = " $1" },
|
||||
},
|
||||
{
|
||||
"return_statement",
|
||||
"%s : $1",
|
||||
{ after_each = " $1" },
|
||||
},
|
||||
{
|
||||
"anonymous_return",
|
||||
"%s",
|
||||
{ after_each = " $1" },
|
||||
},
|
||||
{ nil, '"""' },
|
||||
},
|
||||
reST = {
|
||||
{ nil, '""" $1 """', { no_results = true, type = { "class", "func" } } },
|
||||
{ nil, '"""$1', { no_results = true, type = { "file" } } },
|
||||
{ nil, "", { no_results = true, type = { "file" } } },
|
||||
{ nil, "$1", { no_results = true, type = { "file" } } },
|
||||
{ nil, '"""', { no_results = true, type = { "file" } } },
|
||||
{ nil, "", { no_results = true, type = { "file" } } },
|
||||
|
||||
{ nil, "# $1", { no_results = true, type = { "type" } } },
|
||||
|
||||
{ nil, '"""$1' },
|
||||
{ nil, "" },
|
||||
{
|
||||
"parameters",
|
||||
":param %s: $1",
|
||||
{ after_each = ":type %s: $1", type = { "func" } },
|
||||
},
|
||||
{
|
||||
{ "identifier", "type" },
|
||||
":param %s: $1",
|
||||
{
|
||||
after_each = ":type %s: %s $1",
|
||||
required = "typed_parameters",
|
||||
type = { "func" },
|
||||
},
|
||||
},
|
||||
{ "attributes", ":param %s: $1" },
|
||||
{ "has_return", ":return: $1", { type = { "func" } } },
|
||||
{ "has_return", ":rtype: $1", { type = { "func" } } },
|
||||
{ nil, '"""' },
|
||||
},
|
||||
},
|
||||
end,
|
||||
})
|
||||
:add_default_template("google_docstrings")
|
||||
:add_template("numpydoc")
|
||||
:add_template("reST"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user