fix(impl): refactor some logic, use new api
This commit is contained in:
parent
3642c247e5
commit
ca191dbf3b
3 changed files with 14 additions and 40 deletions
|
|
@ -38,48 +38,22 @@ local ts_utils = require "gopher._utils.ts"
|
|||
local u = require "gopher._utils"
|
||||
local impl = {}
|
||||
|
||||
---@return string
|
||||
---@private
|
||||
local function get_struct()
|
||||
local ns = ts_utils.get_struct_under_cursor(unpack(vim.api.nvim_win_get_cursor(0)))
|
||||
if ns == nil then
|
||||
u.notify "put cursor on a 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
|
||||
|
||||
function impl.impl(...)
|
||||
local args = { ... }
|
||||
local iface, recv_name = "", ""
|
||||
local recv = get_struct()
|
||||
local iface, recv = "", ""
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
|
||||
if #args == 0 then
|
||||
iface = vim.fn.input "impl: generating method stubs for interface: "
|
||||
vim.cmd "redraw!"
|
||||
if iface == "" then
|
||||
u.deferred_notify("usage: GoImpl f *File io.Reader", vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
elseif #args == 1 then -- :GoImpl io.Reader
|
||||
recv = string.lower(recv) .. " *" .. recv
|
||||
vim.cmd "redraw!"
|
||||
iface = select(1, ...)
|
||||
if #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
|
||||
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)
|
||||
local st = ts_utils.get_struct_under_cursor(bufnr)
|
||||
iface = args[2]
|
||||
recv = args[1] .. " *" .. st.name
|
||||
elseif #args == 3 then -- :GoImpl r Struct io.Reader
|
||||
recv = args[1] .. " *" .. args[2]
|
||||
iface = args[3]
|
||||
end
|
||||
|
||||
local rs = r.sync { c.impl, "-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), recv, iface }
|
||||
|
|
|
|||
2
spec/fixtures/impl/reader_output.go
vendored
2
spec/fixtures/impl/reader_output.go
vendored
|
|
@ -1,6 +1,6 @@
|
|||
package main
|
||||
|
||||
func (r Read2) Read(p []byte) (n int, err error) {
|
||||
func (r *Read2) Read(p []byte) (n int, err error) {
|
||||
panic("not implemented") // TODO: Implement
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ T["impl"]["works w io.Writer"] = function()
|
|||
t.writefile(tmp, fixtures.input)
|
||||
|
||||
child.cmd("silent edit " .. tmp)
|
||||
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6 })
|
||||
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 0 })
|
||||
child.cmd "GoImpl w io.Writer"
|
||||
child.cmd "write"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue