diff --git a/lua/neogen/configurations/cs.lua b/lua/neogen/configurations/cs.lua index 59709df5..34893a86 100644 --- a/lua/neogen/configurations/cs.lua +++ b/lua/neogen/configurations/cs.lua @@ -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 = { @@ -12,8 +56,8 @@ 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 = { @@ -21,42 +65,22 @@ return { ["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 @@ -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) @@ -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 {} @@ -127,9 +160,8 @@ return { subtree = { { retrieve = "all", - node_type = "return_statement", + node_type = "accessor_declaration", as = i.Return, - recursive = true, extract = true, }, }, @@ -137,6 +169,22 @@ return { } 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, }, diff --git a/lua/neogen/templates/xmldoc.lua b/lua/neogen/templates/xmldoc.lua index 41f45657..3fdd52f0 100644 --- a/lua/neogen/templates/xmldoc.lua +++ b/lua/neogen/templates/xmldoc.lua @@ -8,7 +8,7 @@ return { { nil, "/// ", {} }, { nil, "/// $1", {} }, { nil, "/// ", {} }, - { i.Parameter, '/// $1', { type = { "func", "type" } } }, { i.Tparam, '/// $1', { type = { "func", "class" } } }, + { i.Parameter, '/// $1', { type = { "func", "type", "class" } } }, { i.Return, "/// $1", { type = { "func", "type" } } }, } diff --git a/lua/neogen/utilities/nodes.lua b/lua/neogen/utilities/nodes.lua index 97dd7fc3..6118849a 100644 --- a/lua/neogen/utilities/nodes.lua +++ b/lua/neogen/utilities/nodes.lua @@ -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) @@ -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