diff --git a/lua/neogen/configurations/typescript.lua b/lua/neogen/configurations/typescript.lua index 0beca7f..4d613f7 100644 --- a/lua/neogen/configurations/typescript.lua +++ b/lua/neogen/configurations/typescript.lua @@ -45,19 +45,39 @@ local function_tree = { { retrieve = "first", node_type = "return_statement", extract = true, as = i.Return }, }, }, + { + retrieve = "first", + node_type = "type_annotation", + subtree = { + { retrieve = "all", node_type = "type_identifier", extract = true, as = i.Return }, + }, + }, } return { parent = { - func = { "function_declaration", "expression_statement", "variable_declaration", "lexical_declaration" }, - class = { "function_declaration", "expression_statement", "variable_declaration", "class_declaration" }, + func = { + "function_declaration", + "function_signature", + "expression_statement", + "variable_declaration", + "lexical_declaration", + "method_definition", + }, + class = { + "function_declaration", + "expression_statement", + "variable_declaration", + "class_declaration", + "export_statement", + }, type = { "variable_declaration", "lexical_declaration" }, file = { "program" }, }, data = { func = { - ["function_declaration"] = { + ["function_declaration|method_definition|function_signature"] = { ["0"] = { extract = function(node) @@ -93,7 +113,7 @@ return { }, }, class = { - ["function_declaration|class_declaration|expression_statement|variable_declaration"] = { + ["function_declaration|class_declaration|expression_statement|variable_declaration|export_statement"] = { ["0"] = { extract = function(_) @@ -134,5 +154,7 @@ return { }, }, - template = template:add_default_annotation("jsdoc"), + locator = require("neogen.locators.typescript"), + + template = template:add_default_annotation("jsdoc"):add_annotation("tsdoc"), } diff --git a/lua/neogen/locators/typescript.lua b/lua/neogen/locators/typescript.lua new file mode 100644 index 0000000..f846e88 --- /dev/null +++ b/lua/neogen/locators/typescript.lua @@ -0,0 +1,15 @@ +local ts_utils = require("nvim-treesitter.ts_utils") +local default_locator = require("neogen.locators.default") + +return function(node_info, nodes_to_match) + local found_node = default_locator(node_info, nodes_to_match) + + if found_node and found_node:type() == "class_declaration" then + local parent = found_node:parent() + if parent and parent:type() == "export_statement" then + return parent + end + end + + return found_node +end diff --git a/lua/neogen/templates/tsdoc.lua b/lua/neogen/templates/tsdoc.lua new file mode 100644 index 0000000..f50992e --- /dev/null +++ b/lua/neogen/templates/tsdoc.lua @@ -0,0 +1,21 @@ +local i = require("neogen.types.template").item + +return { + { nil, "/* $1 */", { no_results = true, type = { "func", "class" } } }, + { nil, "/* @type $1 */", { no_results = true, type = { "type" } } }, + + { nil, "/**", { no_results = true, type = { "file" } } }, + { nil, " * @module $1", { no_results = true, type = { "file" } } }, + { nil, " */", { no_results = true, type = { "file" } } }, + + { nil, "/**", { type = { "class", "func" } } }, + { i.ClassName, " * $1", { type = { "class" } } }, + { i.Parameter, " * @param %s - $1", { type = { "func" } } }, + { + { i.Parameter }, + " * @param %s - $1", + { required = i.Tparam, type = { "func" } }, + }, + { i.Return, " * @returns $1", { type = { "func" } } }, + { nil, " */", { type = { "class", "func" } } }, +}