[mirror] Make your go dev experience better
github.com/olexsmir/gopher.nvim
neovim
golang
1---@toc_entry Auto implementation of interface methods
2---@tag gopher.nvim-impl
3---@text impl is utilizing the `impl` tool to generate method stubs for interfaces.
4---@usage
5--- 1. Automatically implement an interface for a struct:
6--- - Place your cursor on the struct where you want to implement the interface.
7--- - Run `:GoImpl io.Reader`
8--- - This will automatically determine the receiver and implement the `io.Reader` interface.
9---
10--- 2. Specify a custom receiver:
11--- - Place your cursor on the struct
12--- - Run `:GoImpl w io.Writer`, where:
13--- - `w` is the receiver.
14--- - `io.Writer` is the interface to implement.
15---
16--- 3. Explicitly specify the receiver, struct, and interface:
17--- - No need to place the cursor on the struct if all arguments are provided.
18--- - Run `:GoImpl r RequestReader io.Reader`, where:
19--- - `r` is the receiver.
20--- - `RequestReader` is the struct.
21--- - `io.Reader` is the interface to implement.
22---
23--- Example:
24--- >go
25--- type BytesReader struct{}
26--- // ^ put your cursor here
27--- // run `:GoImpl b io.Reader`
28---
29--- // this is what you will get
30--- func (b *BytesReader) Read(p []byte) (n int, err error) {
31--- panic("not implemented") // TODO: Implement
32--- }
33--- <
34
35local c = require("gopher.config").commands
36local r = require "gopher._utils.runner"
37local ts_utils = require "gopher._utils.ts"
38local u = require "gopher._utils"
39local impl = {}
40
41---@return string
42---@private
43local function get_struct()
44 local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
45 if ns == nil then
46 u.notify "put cursor on a struct or specify a receiver"
47 return ""
48 end
49
50 vim.api.nvim_win_set_cursor(0, {
51 ns.dim.e.r,
52 ns.dim.e.c,
53 })
54
55 return ns.name
56end
57
58function impl.impl(...)
59 local args = { ... }
60 local iface, recv_name = "", ""
61 local recv = get_struct()
62
63 if #args == 0 then
64 iface = vim.fn.input "impl: generating method stubs for interface: "
65 vim.cmd "redraw!"
66 if iface == "" then
67 u.deferred_notify("usage: GoImpl f *File io.Reader", vim.log.levels.INFO)
68 return
69 end
70 elseif #args == 1 then -- :GoImpl io.Reader
71 recv = string.lower(recv) .. " *" .. recv
72 vim.cmd "redraw!"
73 iface = select(1, ...)
74 elseif #args == 2 then -- :GoImpl w io.Writer
75 recv_name = select(1, ...)
76 recv = string.format("%s *%s", recv_name, recv)
77 iface = select(#args, ...)
78 elseif #args > 2 then
79 iface = select(#args, ...)
80 recv = select(#args - 1, ...)
81 recv_name = select(#args - 2, ...)
82 recv = string.format("%s %s", recv_name, recv)
83 end
84
85 local rs = r.sync { c.impl, "-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), recv, iface }
86 if rs.code ~= 0 then
87 error("failed to implement interface: " .. rs.stderr)
88 end
89
90 local pos = vim.fn.getcurpos()[2]
91 local output = u.remove_empty_lines(vim.split(rs.stdout, "\n"))
92
93 table.insert(output, 1, "")
94 vim.fn.append(pos, output)
95end
96
97return impl