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 u = require "gopher._utils"
|
||||||
|
local ts_utils = require "gopher._utils.ts.init"
|
||||||
|
|
||||||
return function (cmd_args)
|
return function(_)
|
||||||
local parser = vim.treesitter.get_parser()
|
|
||||||
if not parser then
|
|
||||||
u.notify("no parser not available", "error")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 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
|
|
||||||
|
|
||||||
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
|
-- 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 row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||||
local struct_type_node = current_node:child(1):child(1)
|
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
|
||||||
|
|
||||||
|
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 args = {}
|
local args = {}
|
||||||
local struct_initialization = {}
|
local fields = {}
|
||||||
for field in struct_type_node:iter_children() do
|
for i in ns.struct_properties.fields_node:iter_children() do
|
||||||
if field:type() == 'field_declaration' then
|
if i:type() == "field_declaration" then
|
||||||
local field_type_node = field:child(1)
|
table.insert(args, vim.treesitter.get_node_text(i, 1))
|
||||||
local field_name_node = field:child(0)
|
|
||||||
if field_type_node and field_name_node then
|
for j in i:iter_children() do
|
||||||
local field_type = vim.treesitter.get_node_text(field_type_node, 1)
|
if j:type() == "field_identifier" then
|
||||||
local field_name = vim.treesitter.get_node_text(field_name_node, 1)
|
local field_name = vim.treesitter.get_node_text(j, 1)
|
||||||
local field_name_lower = string.lower(field_name)
|
table.insert(fields, field_name .. ": " .. field_name)
|
||||||
table.insert(args, field_name_lower .. " " .. field_type)
|
|
||||||
table.insert(struct_initialization, field_name .. ": " .. field_name_lower)
|
|
||||||
end
|
end
|
||||||
end
|
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(
|
local constructor_code = string.format(
|
||||||
"\nfunc New%s(%s) *%s {\n\treturn &%s{%s}\n}\n",
|
"\nfunc New%s%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, ', ')
|
ns.name:gsub("^%l", string.upper),
|
||||||
|
parameters_section,
|
||||||
|
table.concat(args, ", "),
|
||||||
|
struct_type,
|
||||||
|
struct_type,
|
||||||
|
table.concat(fields, ", ")
|
||||||
)
|
)
|
||||||
|
|
||||||
vim.fn.append(current_node:end_() + 1, vim.split(constructor_code, '\n'))
|
vim.fn.append(ns.declaring_node:end_() + 1, vim.split(constructor_code, "\n"))
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue