Add support for attributes in python class

This commit is contained in:
Daniel Mathiot
2021-08-23 17:30:19 +02:00
parent ceeae74d0d
commit 3272811aa7
4 changed files with 39 additions and 11 deletions

View File

@@ -24,8 +24,34 @@ return {
match = "block",
extract = function(node)
return {}
end,
local results = {
attributes = {}
}
local init_function = neogen.utilities.nodes:matching_child_nodes(node, "function_definition")[1]
if init_function == nil then
return
end
local body = neogen.utilities.nodes:matching_child_nodes(init_function, "block")[1]
if body == nil then
return
end
local expressions = neogen.utilities.nodes:matching_child_nodes(body, "expression_statement")
for _,expression in pairs(expressions) do
local assignment = neogen.utilities.nodes:matching_child_nodes(expression, "assignment")[1]
if assignment ~= nil then
local left_side = assignment:field("left")[1]
local left_attribute = left_side:field("attribute")[1]
table.insert(results.attributes, ts_utils.get_node_text(left_attribute)[1])
end
end
return results
end
},
},
},
@@ -42,6 +68,7 @@ return {
google_docstrings = {
{ nil, '"""' },
{ "parameters", "\t%s: ", { before_first_item = "Args: " } },
{ "attributes", "\t%s: ", { before_first_item = "Attributes: " } },
{ nil, '"""' },
},
},

View File

@@ -9,7 +9,6 @@ local ts_utils = require("nvim-treesitter.ts_utils")
--- @return table { line, content }, with line being the line to append the content
neogen.default_generator = function(parent, data, template)
local start_row, start_column, end_row, end_column = ts_utils.get_node_range(parent)
P(ts_utils.get_node_range(parent))
local commentstring, generated_template = vim.trim(vim.api.nvim_buf_get_option(0, "commentstring"):format(""))
local row_to_place = start_row
@@ -18,7 +17,7 @@ neogen.default_generator = function(parent, data, template)
local append = template.append or {}
if append.position == "after" then
local child_node = neogen.utilities.nodes:first_child_node(parent, append.child_name)
local child_node = neogen.utilities.nodes:matching_child_nodes(parent, append.child_name)[1]
if child_node ~= nil then
row_to_place, col_to_place, _ , _ = child_node:range()
end
@@ -57,7 +56,7 @@ neogen.default_generator = function(parent, data, template)
local opts = values[3] or {}
-- Will append the item before all their nodes
if opts.before_first_item then
if opts.before_first_item and data[type] then
table.insert(result, prefix .. opts.before_first_item)
end

View File

@@ -10,7 +10,6 @@ neogen.utilities.extractors = {
local result = {}
local split = vim.split(name, "|", true)
for child in node:iter_children() do
if vim.tbl_contains(split, child:type()) then
table.insert(result, ts_utils.get_node_text(child)[1])

View File

@@ -1,14 +1,17 @@
neogen.utilities.nodes = {
--- Get first child node that match the provided node name
--- Get a list of child nodes that match the provided node name
--- @param _ any
--- @param parent userdata the parent's node
--- @param node_name string the node type to search for
--- @return userdata node the first encountered child node
first_child_node = function (_, parent, node_name)
--- @return table a table of nodes that matched the name
matching_child_nodes = function (_, parent, node_name)
local results = {}
for child in parent:iter_children() do
if child:type() == node_name then
return child
table.insert(results, child)
end
end
end
return results
end,
}