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,20 +355,33 @@ return {
return {} return {}
end end
-- Deliberately check inside the init function for assignments to "self" -- Initialize results
results[i.ClassAttribute] = {} results[i.ClassAttribute] = {}
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 -- Extracting left field for the ones already marked as attributes
if not vim.tbl_isempty(left) and not vim.tbl_isempty(left[1]:field("attribute")) then if nodes[i.ClassAttribute] then
--Adding it to the list for _, assignment in pairs(nodes[i.ClassAttribute]) do
local left_attribute = assignment:field("left")[1]:field("attribute")[1] local left = assignment:field("left")
left_attribute = helpers.get_node_text(left_attribute)[1] left = left and helpers.get_node_text(left[1])[1]
table.insert(results[i.ClassAttribute], left)
end
end
if not vim.startswith(left_attribute, "_") then -- For other attributes only marked as assignments, we will check if they are not private and add them to the list
table.insert(results[i.ClassAttribute], left_attribute) -- 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
local left_attribute = assignment:field("left")[1]:field("attribute")[1]
left_attribute = helpers.get_node_text(left_attribute)[1]
if not vim.startswith(left_attribute, "_") then
table.insert(results[i.ClassAttribute], left_attribute)
end
end end
end end
end 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) --- 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~ --- ## 2.16.0~
--- - Add support for `nvim` snippet engine (default nvim snippet engine) ! (see |neogen-snippet-integration|) --- - Add support for `nvim` snippet engine (default nvim snippet engine) ! (see |neogen-snippet-integration|)
--- ## 2.15.0~ --- ## 2.15.0~
@@ -301,7 +303,7 @@ end
--- with multiple annotation conventions. --- with multiple annotation conventions.
---@tag neogen-changelog ---@tag neogen-changelog
---@toc_entry Changes in neogen plugin ---@toc_entry Changes in neogen plugin
neogen.version = "2.16.1" neogen.version = "2.17.0"
--minidoc_afterlines_end --minidoc_afterlines_end
return neogen return neogen