Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 95 additions & 47 deletions lua/neogen/configurations/cs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,50 @@ local nodes_utils = require("neogen.utilities.nodes")
local template = require("neogen.template")
local i = require("neogen.types.template").item

local is_void = function(return_statement)
if not return_statement then
return
end
for _, value in ipairs(return_statement) do
if value == "void" then
return true
end
end
return false
end

local get_parameters_tree = function()
return {
retrieve = "first",
node_type = "parameter_list",
subtree = {
{
retrieve = "all",
node_type = "parameter",
subtree = {
{ position = -1, extract = true, as = i.Parameter },
},
},
},
}
end

local get_type_parameters_tree = function()
return {
retrieve = "first",
node_type = "type_parameter_list",
subtree = {
{
retrieve = "all",
node_type = "type_parameter",
subtree = {
{ position = 1, extract = true, as = i.Tparam },
},
},
},
}
end

return {
parent = {
func = {
Expand All @@ -12,51 +56,31 @@ return {
"delegate_declaration",
"conversion_operator_declaration",
},
class = { "class_declaration", "interface_declaration" },
type = { "field_declaration", "property_declaration", "event_field_declaration", "indexer_declaration" },
class = { "interface_declaration", "class_declaration", "record_declaration", "struct_declaration", "enum_declaration" },
type = { "field_declaration", "property_declaration", "event_field_declaration", "indexer_declaration", "enum_member_declaration" },
},
data = {
func = {
["method_declaration|constructor_declaration|operator_declaration|delegate_declaration|conversion_operator_declaration"] = {
["0"] = {
extract = function(node)
local tree = {
get_type_parameters_tree(),
get_parameters_tree(),
{
retrieve = "first",
node_type = "parameter_list",
subtree = {
{
retrieve = "all",
node_type = "parameter",
subtree = {
{ position = 2, extract = true, as = i.Parameter },
},
},
},
},
{
retrieve = "first",
node_type = "type_parameter_list",
subtree = {
{
retrieve = "all",
node_type = "type_parameter",
subtree = {
{ position = 1, extract = true, as = i.Tparam },
},
},
},
},
{
position = 1,
retrieve = "all",
extract = true,
as = i.Return,
},
}
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
if res.return_statement[1] == "void" then

local has_no_return = node:type() == "constructor_declaration" or is_void(res.return_statement)
if has_no_return then
res.return_statement = nil
else
res.return_statement = { res.return_statement[1] }
end
res.identifier = res["_"]
return res
Expand All @@ -65,23 +89,11 @@ return {
},
},
class = {
["class_declaration|interface_declaration"] = {
["interface_declaration"] = {
["0"] = {
extract = function(node)
local tree = {
{
retrieve = "first",
node_type = "type_parameter_list",
subtree = {
{
retrieve = "all",
node_type = "type_parameter",
subtree = {
{ position = 1, extract = true, as = i.Tparam },
},
},
},
},
get_type_parameters_tree(),
}
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
Expand All @@ -90,9 +102,30 @@ return {
end,
},
},
["class_declaration|record_declaration|struct_declaration"] = {
["0"] = {
extract = function(node)
local tree = {
get_type_parameters_tree(),
get_parameters_tree(),
}
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)
res.identifier = res["_"]
return res
end,
},
},
["enum_declaration"] = {
["0"] = {
extract = function()
return {}
end,
}
},
},
type = {
["field_declaration|property_declaration|event_field_declaration"] = {
["field_declaration|property_declaration|event_field_declaration|enum_member_declaration"] = {
["0"] = {
extract = function()
return {}
Expand Down Expand Up @@ -127,16 +160,31 @@ return {
subtree = {
{
retrieve = "all",
node_type = "return_statement",
node_type = "accessor_declaration",
as = i.Return,
recursive = true,
extract = true,
},
},
},
}
local nodes = nodes_utils:matching_nodes_from(node, tree)
local res = extractors:extract_from_matched(nodes)

local has_getter = false
if res.return_statement then
for _, value in ipairs(res.return_statement) do
if vim.startswith(value, "get") then
has_getter = true
break
end
end
end
if has_getter then
res.return_statement = { "get" }
else
res.return_statement = nil
end

return res
end,
},
Expand Down
2 changes: 1 addition & 1 deletion lua/neogen/templates/xmldoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ return {
{ nil, "/// <summary>", {} },
{ nil, "/// $1", {} },
{ nil, "/// </summary>", {} },
{ i.Parameter, '/// <param name="%s">$1</param>', { type = { "func", "type" } } },
{ i.Tparam, '/// <typeparam name="%s">$1</typeparam>', { type = { "func", "class" } } },
{ i.Parameter, '/// <param name="%s">$1</param>', { type = { "func", "type", "class" } } },
{ i.Return, "/// <returns>$1</returns>", { type = { "func", "type" } } },
}
8 changes: 6 additions & 2 deletions lua/neogen/utilities/nodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ return {
--- @param parent TSNode the parent node
--- @param tree table a nested table : { retrieve = "all|first", node_type = node_name, subtree = tree, recursive = true }
--- If you want to extract the node, do not specify the subtree and instead: extract = true
--- Optional: you can specify position = number instead of retrieve, and it will fetch the child node at position number
--- Optional: you can specify position = number instead of retrieve, and it will fetch the child node at position number, if position == -1, then last child is taken
--- @param result? table the table of results
--- @return table result a table of k,v where k are node_types and v all matched nodes
matching_nodes_from = function(self, parent, tree, result)
Expand All @@ -74,7 +74,11 @@ return {
-- Only keep the node with custom position
if not subtree.retrieve then
assert(type(subtree.position) == "number", "please require position if retrieve is nil")
matched = { matched[subtree.position] }
local position = subtree.position
if position == -1 then
position = #matched
end
matched = { matched[position] }
end

if subtree.recursive then
Expand Down