From c68a26870112792323b491a9b290511082251d51 Mon Sep 17 00:00:00 2001 From: Riri Date: Sun, 4 May 2025 01:38:08 +0800 Subject: [PATCH] Add ts/js property and type declaration support (#204) --- lua/neogen/configurations/javascript.lua | 42 +++++++++++++++++++++++- lua/neogen/configurations/typescript.lua | 38 ++++++++++++++++++++- lua/neogen/locators/typescript.lua | 7 ++++ lua/neogen/templates/jsdoc.lua | 4 +++ lua/neogen/templates/tsdoc.lua | 4 +++ 5 files changed, 93 insertions(+), 2 deletions(-) diff --git a/lua/neogen/configurations/javascript.lua b/lua/neogen/configurations/javascript.lua index 4a7ad25..46f717f 100644 --- a/lua/neogen/configurations/javascript.lua +++ b/lua/neogen/configurations/javascript.lua @@ -32,6 +32,10 @@ return { class = { "function_declaration", "expression_statement", "variable_declaration", "class_declaration" }, file = { "program" }, type = { "variable_declaration", "lexical_declaration" }, + property = { + "property_signature", + "property_identifier", + }, }, data = { @@ -101,6 +105,15 @@ return { }, }, }, + property = { + ["property_signature|property_identifier"] = { + ["0"] = { + extract = function() + return {} + end, + }, + }, + }, type = { ["variable_declaration|lexical_declaration"] = { ["0"] = { @@ -112,5 +125,32 @@ return { }, }, - template = template:add_default_annotation("jsdoc"), + locator = require("neogen.locators.typescript"), + + template = template + :config({ + append = { position = "after", child_name = "comment", fallback = "block", disabled = { "file" } }, + position = function(node, type) + if vim.tbl_contains({ "func", "class" }, type) then + local parent = node:parent() + + -- Verify if the parent is an export_statement (prevents offset of generated annotation) + if parent and parent:type() == "export_statement" then + local row, col = vim.treesitter.get_node_range(parent) + return row, col + end + return + end + if vim.tbl_contains({ "property" }, type) then + local parent = node:parent() + + if parent and vim.tbl_contains({ "public_field_definition" }, parent:type()) then + local row, col = vim.treesitter.get_node_range(parent) + return row, col + end + return + end + end, + }) + :add_default_annotation("jsdoc"), } diff --git a/lua/neogen/configurations/typescript.lua b/lua/neogen/configurations/typescript.lua index 95124f0..1cd62d8 100644 --- a/lua/neogen/configurations/typescript.lua +++ b/lua/neogen/configurations/typescript.lua @@ -85,6 +85,15 @@ return { }, type = { "variable_declaration", "lexical_declaration" }, file = { "program" }, + property = { + "property_signature", + "property_identifier", + }, + declaration = { + "type_alias_declaration", + "interface_declaration", + "enum_declaration", + }, }, data = { @@ -173,6 +182,24 @@ return { }, }, }, + property = { + ["property_signature|property_identifier"] = { + ["0"] = { + extract = function() + return {} + end, + }, + }, + }, + declaration = { + ["type_alias_declaration|interface_declaration|enum_declaration"] = { + ["0"] = { + extract = function() + return {} + end, + }, + }, + }, }, locator = require("neogen.locators.typescript"), @@ -181,7 +208,7 @@ return { :config({ append = { position = "after", child_name = "comment", fallback = "block", disabled = { "file" } }, position = function(node, type) - if vim.tbl_contains({ "func", "class" }, type) then + if vim.tbl_contains({ "func", "class", "declaration" }, type) then local parent = node:parent() -- Verify if the parent is an export_statement (prevents offset of generated annotation) @@ -191,6 +218,15 @@ return { end return end + if vim.tbl_contains({ "property" }, type) then + local parent = node:parent() + + if parent and vim.tbl_contains({ "public_field_definition" }, parent:type()) then + local row, col = vim.treesitter.get_node_range(parent) + return row, col + end + return + end end, }) :add_default_annotation("jsdoc") diff --git a/lua/neogen/locators/typescript.lua b/lua/neogen/locators/typescript.lua index a9d1078..ff8d015 100644 --- a/lua/neogen/locators/typescript.lua +++ b/lua/neogen/locators/typescript.lua @@ -13,5 +13,12 @@ return function(node_info, nodes_to_match) end end + if found_node and vim.tbl_contains({ "property_identifier" }, found_node:type()) then + local parent = found_node:parent() + if parent and parent:type() == "method_definition" then + return parent + end + end + return found_node end diff --git a/lua/neogen/templates/jsdoc.lua b/lua/neogen/templates/jsdoc.lua index 7882a96..f5b828e 100644 --- a/lua/neogen/templates/jsdoc.lua +++ b/lua/neogen/templates/jsdoc.lua @@ -18,4 +18,8 @@ return { }, { i.Return, " * @returns {$1} - $1", { type = { "func" } } }, { nil, " */", { type = { "class", "func" } } }, + + { nil, "/**", { no_results = true, type = { "property" } } }, + { nil, " * $1", { no_results = true, type = { "property" } } }, + { nil, " */", { no_results = true, type = { "property" } } }, } diff --git a/lua/neogen/templates/tsdoc.lua b/lua/neogen/templates/tsdoc.lua index db1e977..b9ad71f 100644 --- a/lua/neogen/templates/tsdoc.lua +++ b/lua/neogen/templates/tsdoc.lua @@ -19,4 +19,8 @@ return { }, { i.Return, " * @returns $1", { type = { "func" } } }, { nil, " */", { type = { "class", "func" } } }, + + { nil, "/**", { no_results = true, type = { "property", "declaration" } } }, + { nil, " * $1", { no_results = true, type = { "property", "declaration" } } }, + { nil, " */", { no_results = true, type = { "property", "declaration" } } }, }