[mirror] Make your go dev experience better
github.com/olexsmir/gopher.nvim
neovim
golang
1local Job = require "plenary.job"
2local ts_utils = require "gopher._utils.ts"
3local c = require("gopher.config").config.commands
4local u = require "gopher._utils"
5
6---@return string
7local function get_struct()
8 local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
9 if ns == nil then
10 u.notify("put cursor on a struct or specify a receiver", "info")
11 return ""
12 end
13
14 vim.api.nvim_win_set_cursor(0, {
15 ns.dim.e.r,
16 ns.dim.e.c,
17 })
18
19 return ns.name
20end
21
22return function(...)
23 local args = { ... }
24 local iface, recv_name = "", ""
25 local recv = get_struct()
26
27 if #args == 0 then
28 iface = vim.fn.input "impl: generating method stubs for interface: "
29 vim.cmd "redeaw!"
30 if iface == "" then
31 u.notify("usage: GoImpl f *File io.Reader", "info")
32 return
33 end
34 elseif #args == 1 then -- :GoImpl io.Reader
35 recv = string.lower(recv) .. " *" .. recv
36 vim.cmd "redraw!"
37 iface = select(1, ...)
38 elseif #args == 2 then -- :GoImpl w io.Writer
39 recv_name = select(1, ...)
40 recv = string.format("%s *%s", recv_name, recv)
41 iface = select(#args, ...)
42 elseif #args > 2 then
43 iface = select(#args, ...)
44 recv = select(#args - 1, ...)
45 recv_name = select(#args - 2, ...)
46 recv = string.format("%s %s", recv_name, recv)
47 end
48
49 -- stylua: ignore
50 local cmd_args = {
51 "-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), ---@diagnostic disable-line: missing-parameter
52 recv,
53 iface
54 }
55
56 local res_data
57 Job
58 :new({
59 command = c.impl,
60 args = cmd_args,
61 on_exit = function(data, retval)
62 if retval ~= 0 then
63 u.notify("command 'impl " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
64 return
65 end
66
67 res_data = data:result()
68 end,
69 })
70 :sync()
71
72 local pos = vim.fn.getcurpos()[2]
73 table.insert(res_data, 1, "")
74 vim.fn.append(pos, res_data)
75end