feat(py): Add support for @dataclass attributes

Closes #126
This commit is contained in:
danymat
2024-03-03 23:00:36 +01:00
parent 63ca9609be
commit e5460f2ad8
2 changed files with 39 additions and 12 deletions

View File

@@ -333,6 +333,18 @@ return {
},
},
},
{
retrieve = "all",
node_type = "expression_statement",
subtree = {
{
retrieve = "first",
node_type = "assignment",
extract = true,
as = i.ClassAttribute,
},
},
},
},
},
}
@@ -343,12 +355,24 @@ return {
return {}
end
-- Deliberately check inside the init function for assignments to "self"
-- Initialize results
results[i.ClassAttribute] = {}
-- Extracting left field for the ones already marked as attributes
if nodes[i.ClassAttribute] then
for _, assignment in pairs(nodes[i.ClassAttribute]) do
local left = assignment:field("left")
left = left and helpers.get_node_text(left[1])[1]
table.insert(results[i.ClassAttribute], left)
end
end
-- For other attributes only marked as assignments, we will check if they are not private and add them to the list
-- Deliberately check inside the init function for assignments to "self"
if nodes["assignment"] then
for _, assignment in pairs(nodes["assignment"]) do
-- Getting left side in assignment
local left = assignment:field("left")
-- Checking if left side is an "attribute", which means assigning to an attribute to the function
if not vim.tbl_isempty(left) and not vim.tbl_isempty(left[1]:field("attribute")) then
--Adding it to the list
@@ -360,6 +384,7 @@ return {
end
end
end
end
if vim.tbl_isempty(results[i.ClassAttribute]) then
results[i.ClassAttribute] = nil
end

View File

@@ -241,6 +241,8 @@ end
---
--- Note: We will only document `major` and `minor` versions, not `patch` ones. (only X and Y in X.Y.z)
---
--- ## 2.17.0~
--- - Python now supports dataclass attributes (#126)
--- ## 2.16.0~
--- - Add support for `nvim` snippet engine (default nvim snippet engine) ! (see |neogen-snippet-integration|)
--- ## 2.15.0~
@@ -301,7 +303,7 @@ end
--- with multiple annotation conventions.
---@tag neogen-changelog
---@toc_entry Changes in neogen plugin
neogen.version = "2.16.1"
neogen.version = "2.17.0"
--minidoc_afterlines_end
return neogen