feat: add impl support

docs: add impl
This commit is contained in:
Smirnov Olexander 2022-06-15 12:44:04 +03:00
parent e4323581be
commit 6fd413c4c2
5 changed files with 104 additions and 7 deletions

74
lua/gopher/impl.lua Normal file
View file

@ -0,0 +1,74 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local function get_struct()
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then
print "put cursor on struct or specify a receiver"
return ""
end
vim.api.nvim_win_set_cursor(0, {
ns.dim.e.r,
ns.dim.e.c,
})
return ns.name
end
return function(...)
local args = { ... }
local iface, recv_name = "", ""
local recv = get_struct()
if #args == 0 then
iface = vim.fn.input "impl: generating method stubs for interface: "
vim.cmd "redeaw!"
if iface == "" then
print "usage: GoImpl f *File io.Reader"
end
elseif #args == 1 then -- :GoImpl io.Reader
recv = string.lower(recv) .. " *" .. recv
vim.cmd "redraw!"
iface = select(1, ...)
elseif #args == 2 then -- :GoImpl w io.Writer
recv_name = select(1, ...)
recv = string.format("%s *%s", recv_name, recv)
iface = select(#args, ...)
elseif #args > 2 then
iface = select(#args, ...)
recv = select(#args - 1, ...)
recv_name = select(#args - 2, ...)
recv = string.format("%s %s", recv_name, recv)
end
-- stylua: ignore
local cmd_args = {
"-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), ---@diagnostic disable-line: missing-parameter
recv,
iface
}
local res_data
Job
:new({
command = "impl",
args = cmd_args,
on_exit = function(data, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
return
end
res_data = data:result()
end,
})
:sync()
local pos = vim.fn.getcurpos()[2]
table.insert(res_data, 1, "")
vim.fn.append(pos, res_data)
-- table.insert(res_data, 1, "")
-- vim.fn.append(vim.fn.getcurpos()[2], res_data)
end