fix(impl): refactor some logic, use new api

This commit is contained in:
Oleksandr Smirnov 2025-03-18 22:50:19 +02:00
parent 3642c247e5
commit ca191dbf3b
No known key found for this signature in database
3 changed files with 14 additions and 40 deletions

View file

@ -38,48 +38,22 @@ local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils" local u = require "gopher._utils"
local impl = {} 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(...) function impl.impl(...)
local args = { ... } local args = { ... }
local iface, recv_name = "", "" local iface, recv = "", ""
local recv = get_struct() local bufnr = vim.api.nvim_get_current_buf()
if #args == 0 then if #args == 1 then -- :GoImpl io.Reader
iface = vim.fn.input "impl: generating method stubs for interface: " local st = ts_utils.get_struct_under_cursor(bufnr)
vim.cmd "redraw!" iface = args[1]
if iface == "" then recv = string.lower(st.name) .. " *" .. st.name
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, ...)
elseif #args == 2 then -- :GoImpl w io.Writer elseif #args == 2 then -- :GoImpl w io.Writer
recv_name = select(1, ...) local st = ts_utils.get_struct_under_cursor(bufnr)
recv = string.format("%s *%s", recv_name, recv) iface = args[2]
iface = select(#args, ...) recv = args[1] .. " *" .. st.name
elseif #args > 2 then elseif #args == 3 then -- :GoImpl r Struct io.Reader
iface = select(#args, ...) recv = args[1] .. " *" .. args[2]
recv = select(#args - 1, ...) iface = args[3]
recv_name = select(#args - 2, ...)
recv = string.format("%s %s", recv_name, recv)
end end
local rs = r.sync { c.impl, "-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), recv, iface } local rs = r.sync { c.impl, "-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), recv, iface }

View file

@ -1,6 +1,6 @@
package main 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 panic("not implemented") // TODO: Implement
} }

View file

@ -16,7 +16,7 @@ T["impl"]["works w io.Writer"] = function()
t.writefile(tmp, fixtures.input) t.writefile(tmp, fixtures.input)
child.cmd("silent edit " .. tmp) 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 "GoImpl w io.Writer"
child.cmd "write" child.cmd "write"