all repos

gopher.nvim @ 27ba078f14dd39fcd5e8a57d7009cdd77450ebbb

Minimalistic plugin for Go development

gopher.nvim/lua/gopher/impl.lua(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---@toc_entry Auto implementation of interface methods
---@tag gopher.nvim-impl
---@text
--- Integration of `impl` tool to generate method stubs for interfaces.
---
---@usage
--- 1. Automatically implement an interface for a struct:
---  - Place your cursor on the struct where you want to implement the interface.
---  - Run `:GoImpl io.Reader`
---  - This will automatically determine the receiver and implement the `io.Reader` interface.
---
--- 2. Specify a custom receiver:
---  - Place your cursor on the struct
---  - Run `:GoImpl w io.Writer`, where:
---   - `w` is the receiver.
---   - `io.Writer` is the interface to implement.
---
--- 3. Explicitly specify the receiver, struct, and interface:
---  - No need to place the cursor on the struct if all arguments are provided.
---  - Run `:GoImpl r RequestReader io.Reader`, where:
---   - `r` is the receiver.
---   - `RequestReader` is the struct.
---   - `io.Reader` is the interface to implement.
---
--- Example:
--- >go
---    type BytesReader struct{}
---    //    ^ put your cursor here
---    // run `:GoImpl b io.Reader`
---
---    // this is what you will get
---    func (b *BytesReader) Read(p []byte) (n int, err error) {
---        panic("not implemented") // TODO: Implement
---    }
--- <

local c = require "gopher.config"
local r = require "gopher._utils.runner"
local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils"
local impl = {}

function impl.impl(...)
  local args = { ... }
  local iface, recv = "", ""
  local bufnr = vim.api.nvim_get_current_buf()

  local append_after = nil
  if #args == 0 then
    u.notify("arguments not provided. usage: :GoImpl f *File io.Reader", vim.log.levels.ERROR)
    return
  elseif #args == 1 then -- :GoImpl io.Reader
    local st = ts_utils.get_struct_under_cursor(bufnr)
    iface = args[1]
    recv = string.lower(st.name) .. " *" .. st.name
    append_after = st.end_
  elseif #args == 2 then -- :GoImpl w io.Writer
    local st = ts_utils.get_struct_under_cursor(bufnr)
    iface = args[2]
    recv = args[1] .. " *" .. st.name
    append_after = st.end_
  elseif #args == 3 then -- :GoImpl r Struct io.Reader
    recv = args[1] .. " *" .. args[2]
    iface = args[3]
  end

  assert(iface ~= "", "interface not provided")
  assert(recv ~= "", "receiver not provided")

  local dir = vim.fn.fnameescape(vim.fn.expand "%:p:h")
  local rs = r.sync { c.commands.impl, "-dir", dir, recv, iface }
  if rs.code ~= 0 then
    error("failed to implement interface: " .. rs.stderr)
  end

  local pos = append_after or vim.fn.getcurpos()[2]
  local output = u.remove_empty_lines(vim.split(rs.stdout, "\n"))

  table.insert(output, 1, "")
  vim.fn.append(pos, output)
end

return impl