Update GenConstructor function
This commit is contained in:
parent
abc15511a2
commit
eb35a87cdb
1 changed files with 52 additions and 42 deletions
|
|
@ -1,51 +1,61 @@
|
|||
local u = require "gopher._utils"
|
||||
local ts_utils = require "gopher._utils.ts.init"
|
||||
|
||||
return function (cmd_args)
|
||||
local parser = vim.treesitter.get_parser()
|
||||
if not parser then
|
||||
u.notify("no parser not available", "error")
|
||||
return
|
||||
end
|
||||
return function(_)
|
||||
-- TODO: allow to pass a funcName as an argument
|
||||
|
||||
-- When the command is runned on the 'struct name' the current node will be
|
||||
-- the node referring directly to the 'struct name', therefore, to get the
|
||||
-- node that refers to the entire struct we have to get the parent.
|
||||
local current_node = vim.treesitter.get_node():parent()
|
||||
if not current_node then
|
||||
u.notify("no node found at cursor", "error")
|
||||
return
|
||||
end
|
||||
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
local ns = ts_utils.get_struct_node_at_pos(row + 1, col + 1)
|
||||
if not ns then
|
||||
u.notify("not node found at cursor position", "error")
|
||||
return
|
||||
end
|
||||
|
||||
if current_node:type() ~= 'type_spec' then
|
||||
u.notify("not struct node under cursor","error")
|
||||
return
|
||||
end
|
||||
|
||||
-- TODO: allow to pass a funcName as an argument
|
||||
-- local struct_name = (fn_args.args ~= nil and fn_args.args ~= "") and fn_args.args:gsub('"', '') or vim.treesitter.get_node_text(current_node:child(0), 1)
|
||||
local struct_name = vim.treesitter.get_node_text(current_node:child(0), 1)
|
||||
local struct_type_node = current_node:child(1):child(1)
|
||||
|
||||
local args = {}
|
||||
local struct_initialization = {}
|
||||
for field in struct_type_node:iter_children() do
|
||||
if field:type() == 'field_declaration' then
|
||||
local field_type_node = field:child(1)
|
||||
local field_name_node = field:child(0)
|
||||
if field_type_node and field_name_node then
|
||||
local field_type = vim.treesitter.get_node_text(field_type_node, 1)
|
||||
local field_name = vim.treesitter.get_node_text(field_name_node, 1)
|
||||
local field_name_lower = string.lower(field_name)
|
||||
table.insert(args, field_name_lower .. " " .. field_type)
|
||||
table.insert(struct_initialization, field_name .. ": " .. field_name_lower)
|
||||
end
|
||||
local parameter_identifiers = {}
|
||||
if ns.struct_properties.parameters_node then
|
||||
for i in ns.struct_properties.parameters_node:iter_children() do
|
||||
if i:type() == "type_parameter_declaration" then
|
||||
for j in i:iter_children() do
|
||||
print("j: ", j:type())
|
||||
if j:type() == "identifier" then
|
||||
table.insert(parameter_identifiers, vim.treesitter.get_node_text(j, 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local constructor_code = string.format(
|
||||
"\nfunc New%s(%s) *%s {\n\treturn &%s{%s}\n}\n",
|
||||
struct_name:gsub("^%l", string.upper), table.concat(args, ', '), struct_name, struct_name, table.concat(struct_initialization, ', ')
|
||||
)
|
||||
local args = {}
|
||||
local fields = {}
|
||||
for i in ns.struct_properties.fields_node:iter_children() do
|
||||
if i:type() == "field_declaration" then
|
||||
table.insert(args, vim.treesitter.get_node_text(i, 1))
|
||||
|
||||
vim.fn.append(current_node:end_() + 1, vim.split(constructor_code, '\n'))
|
||||
for j in i:iter_children() do
|
||||
if j:type() == "field_identifier" then
|
||||
local field_name = vim.treesitter.get_node_text(j, 1)
|
||||
table.insert(fields, field_name .. ": " .. field_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local struct_type = ns.name
|
||||
local parameters_section = ""
|
||||
if #parameter_identifiers > 0 then
|
||||
struct_type = struct_type .. "[" .. table.concat(parameter_identifiers, ", ") .. "]"
|
||||
parameters_section = vim.treesitter.get_node_text(ns.struct_properties.parameters_node, 1)
|
||||
end
|
||||
|
||||
local constructor_code = string.format(
|
||||
"\nfunc New%s%s(%s) *%s {\n\treturn &%s{%s}\n}\n",
|
||||
ns.name:gsub("^%l", string.upper),
|
||||
parameters_section,
|
||||
table.concat(args, ", "),
|
||||
struct_type,
|
||||
struct_type,
|
||||
table.concat(fields, ", ")
|
||||
)
|
||||
|
||||
vim.fn.append(ns.declaring_node:end_() + 1, vim.split(constructor_code, "\n"))
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue