2021-07-14 15:35:38 +00:00
|
|
|
-- local ts_utils = require 'nvim-treesitter.ts_utils'
|
|
|
|
local utils = require("go.utils")
|
2022-06-01 11:29:13 +00:00
|
|
|
local vfn = vim.fn
|
2022-05-19 04:12:14 +00:00
|
|
|
local impl = "impl" -- GoImpl f *Foo io.Writer
|
2021-12-22 04:01:15 +00:00
|
|
|
-- use ts to get name
|
|
|
|
local function get_struct_name()
|
|
|
|
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
|
|
|
local name = require("go.ts.go").get_struct_node_at_pos(row, col)
|
|
|
|
if name == nil then
|
|
|
|
print("put cursor on struct or specify a receiver")
|
|
|
|
end
|
|
|
|
utils.log(name)
|
2022-05-19 04:12:14 +00:00
|
|
|
if name == nil then
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
local node_name = name.name
|
|
|
|
-- let move the cursor to end of line of struct name
|
|
|
|
local dim = name.dim.e
|
|
|
|
-- let move cursor
|
|
|
|
local r, c = dim.r, dim.c
|
|
|
|
utils.log("move cusror to ", r, c)
|
|
|
|
vim.api.nvim_win_set_cursor(0, { r, c })
|
2022-05-21 05:11:10 +00:00
|
|
|
return node_name
|
2021-12-22 04:01:15 +00:00
|
|
|
end
|
|
|
|
|
2021-07-14 15:35:38 +00:00
|
|
|
local run = function(...)
|
|
|
|
require("go.install").install(impl)
|
|
|
|
local setup = "impl"
|
2021-12-22 04:01:15 +00:00
|
|
|
local iface = ""
|
2022-05-21 05:11:10 +00:00
|
|
|
local recv_name = ""
|
2021-12-22 04:01:15 +00:00
|
|
|
local arg = { ... }
|
2022-05-19 04:12:14 +00:00
|
|
|
utils.log(#arg, arg)
|
2021-12-22 04:01:15 +00:00
|
|
|
|
2022-05-21 05:11:10 +00:00
|
|
|
local recv = get_struct_name()
|
|
|
|
if #arg == 0 then
|
2022-06-01 11:29:13 +00:00
|
|
|
iface = vfn.input("Impl: generating method stubs for interface: ")
|
2021-12-22 04:01:15 +00:00
|
|
|
vim.cmd("redraw!")
|
|
|
|
if iface == "" then
|
|
|
|
print("Impl: please input interface name e.g. io.Reader")
|
2022-05-19 04:12:14 +00:00
|
|
|
-- print("Usage: GoImpl f *File io.Reader")
|
2021-12-22 04:01:15 +00:00
|
|
|
end
|
|
|
|
elseif #arg == 1 then
|
|
|
|
-- " i.e: ':GoImpl io.Writer'
|
2022-05-21 05:11:10 +00:00
|
|
|
recv = string.lower(recv) .. " *" .. recv
|
|
|
|
utils.log(recv)
|
2022-05-19 04:12:14 +00:00
|
|
|
vim.cmd("redraw!")
|
2021-12-22 04:01:15 +00:00
|
|
|
iface = select(1, ...)
|
2022-05-19 04:12:14 +00:00
|
|
|
elseif #arg == 2 then
|
|
|
|
-- " i.e: ':GoImpl s io.Writer'
|
|
|
|
utils.log(recv)
|
2022-05-21 05:11:10 +00:00
|
|
|
recv_name = select(1, ...)
|
|
|
|
recv = string.format("%s *%s", recv_name, recv)
|
2022-05-19 04:12:14 +00:00
|
|
|
local l = #arg
|
|
|
|
iface = select(l, ...)
|
2021-12-22 04:01:15 +00:00
|
|
|
elseif #arg > 2 then
|
|
|
|
local l = #arg
|
|
|
|
iface = select(l, ...)
|
|
|
|
recv = select(l - 1, ...)
|
2022-05-21 05:11:10 +00:00
|
|
|
recv_name = select(l - 2, ...)
|
2021-12-22 04:01:15 +00:00
|
|
|
recv = string.format("%s %s", recv_name, recv)
|
2021-07-14 15:35:38 +00:00
|
|
|
end
|
|
|
|
|
2022-05-21 05:11:10 +00:00
|
|
|
utils.log(#arg, recv_name, recv, iface)
|
2022-06-01 11:29:13 +00:00
|
|
|
local dir = vfn.fnameescape(vfn.expand("%:p:h"))
|
2021-12-22 04:01:15 +00:00
|
|
|
|
2022-05-19 04:12:14 +00:00
|
|
|
setup = { setup, "-dir", dir, recv, iface }
|
2021-12-22 04:01:15 +00:00
|
|
|
utils.log(setup)
|
2022-05-19 04:12:14 +00:00
|
|
|
-- vim.cmd("normal! $%") -- do a bracket match. changed to treesitter
|
2022-06-01 11:29:13 +00:00
|
|
|
local data = vfn.systemlist(setup)
|
2021-07-14 15:35:38 +00:00
|
|
|
data = utils.handle_job_data(data)
|
|
|
|
if not data then
|
|
|
|
return
|
|
|
|
end
|
2022-05-19 04:12:14 +00:00
|
|
|
--
|
2022-06-01 11:29:13 +00:00
|
|
|
local pos = vfn.getcurpos()[2]
|
2022-05-19 04:12:14 +00:00
|
|
|
table.insert(data, 1, "")
|
2022-06-01 11:29:13 +00:00
|
|
|
vfn.append(pos, data)
|
2021-07-14 15:35:38 +00:00
|
|
|
|
2021-12-22 04:01:15 +00:00
|
|
|
-- vim.cmd("silent normal! j=2j")
|
2022-06-01 11:29:13 +00:00
|
|
|
-- vfn.setpos(".", pos)
|
2022-05-19 04:12:14 +00:00
|
|
|
-- vim.cmd("silent normal! 4j")
|
2021-07-14 15:35:38 +00:00
|
|
|
--
|
2021-12-22 04:01:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function match_iface_name(part)
|
|
|
|
local pkg, iface = string.match(part, "^(.*)%.(.*)$")
|
2021-07-14 15:35:38 +00:00
|
|
|
|
2021-12-22 04:01:15 +00:00
|
|
|
utils.log(pkg, iface)
|
|
|
|
local cmd = string.format("go doc %s", pkg)
|
2022-06-01 11:29:13 +00:00
|
|
|
local doc = vfn.systemlist(cmd)
|
2021-12-22 04:01:15 +00:00
|
|
|
if vim.v.shell_error ~= 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local ifaces = {}
|
|
|
|
local pat = string.format("^type (%s.*) interface", iface)
|
|
|
|
for _, line in ipairs(doc) do
|
|
|
|
local m = string.match(line, pat)
|
|
|
|
if m ~= nil then
|
|
|
|
table.insert(ifaces, string.format("%s.%s", pkg, m))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ifaces
|
|
|
|
end
|
|
|
|
|
2022-06-01 11:29:13 +00:00
|
|
|
-- function complete(arglead, cmdline, cursorpos)
|
|
|
|
local function complete(_, cmdline, _)
|
2021-12-22 04:01:15 +00:00
|
|
|
local words = vim.split(cmdline, [[%s+]])
|
|
|
|
local gopls = require("go.gopls")
|
|
|
|
local last = words[#words]
|
|
|
|
|
|
|
|
if string.match(last, "^.+%..*") ~= nil then
|
|
|
|
local part = match_iface_name(last)
|
|
|
|
if part ~= nil then
|
|
|
|
return part
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-01 11:29:13 +00:00
|
|
|
return vfn.uniq(vfn.sort(gopls.list_pkgs()))
|
2021-07-14 15:35:38 +00:00
|
|
|
end
|
2021-12-22 04:01:15 +00:00
|
|
|
|
|
|
|
return { run = run, complete = complete }
|