Add ts/js property and type declaration support (#204)

This commit is contained in:
Riri
2025-05-04 01:38:08 +08:00
committed by GitHub
parent 05d754004d
commit c68a268701
5 changed files with 93 additions and 2 deletions

View File

@@ -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"),
}

View File

@@ -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")

View File

@@ -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

View File

@@ -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" } } },
}

View File

@@ -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" } } },
}