GoImpl updates, allow no arguments and let treesitter do everything for you

pull/130/head
ray-x 2 years ago
parent dbf0094f25
commit 52ccb2e690

@ -324,6 +324,10 @@ e.g:
``` ```
:GoImpl f *File io.Reader :GoImpl f *File io.Reader
``` ```
or simply put your cursor in a struct and do
```
:GoImpl io.Reader
```
## Debug ## Debug

@ -280,7 +280,8 @@ COMMANDS *go-nvim-commands*
:GoImpl {options} *:GoImpl* :GoImpl {options} *:GoImpl*
e.g. GoImpl {receiver} {interface}, will check if cursor is a valid e.g. GoImpl {receiver} {interface}, will check if cursor is a valid
receiver, if you park cursor on struct name, receiver can be omitted. receiver, if you park cursor on struct name, receiver can be omitted.
e.g ":GoImpl io.Reader", or "GoImpl f *File io.Reader" e.g ":GoImpl io.Reader", or "GoImpl f *File io.Reader" or "GoImpl
f io.Reader", you can use tab to complete the interface name.
:GoTermClose :GoTermClose
Closes the floating term. Closes the floating term.

@ -162,7 +162,7 @@ function go.setup(cfg)
vim.cmd([[command! -nargs=* GoAddTag lua require("go.tags").add(<f-args>)]]) vim.cmd([[command! -nargs=* GoAddTag lua require("go.tags").add(<f-args>)]])
vim.cmd([[command! -nargs=* GoRmTag lua require("go.tags").rm(<f-args>)]]) vim.cmd([[command! -nargs=* GoRmTag lua require("go.tags").rm(<f-args>)]])
vim.cmd( vim.cmd(
[[command! -nargs=+ -complete=custom,v:lua.package.loaded.go.impl_complete GoImpl lua require("go.impl").run(<f-args>)]] [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.impl_complete GoImpl lua require("go.impl").run(<f-args>)]]
) )
vim.cmd( vim.cmd(

@ -1,8 +1,7 @@
-- local ts_utils = require 'nvim-treesitter.ts_utils' -- local ts_utils = require 'nvim-treesitter.ts_utils'
local utils = require("go.utils") local utils = require("go.utils")
local impl = "impl" local impl = "impl" -- GoImpl f *Foo io.Writer
-- GoImpl f *Foo io.Writer
-- use ts to get name -- use ts to get name
local function get_struct_name() local function get_struct_name()
local row, col = unpack(vim.api.nvim_win_get_cursor(0)) local row, col = unpack(vim.api.nvim_win_get_cursor(0))
@ -11,8 +10,17 @@ local function get_struct_name()
print("put cursor on struct or specify a receiver") print("put cursor on struct or specify a receiver")
end end
utils.log(name) utils.log(name)
name = name.name if name == nil then
return string.lower(name) .. " *" .. name return ""
end
local node_name = name.name
-- let move the cursor to end of line of struct name
local dim = name.dim.e
-- let move cursor
local r, c = dim.r, dim.c
utils.log("move cusror to ", r, c)
vim.api.nvim_win_set_cursor(0, { r, c })
return string.lower(node_name) .. " *" .. node_name
end end
local run = function(...) local run = function(...)
@ -22,19 +30,30 @@ local run = function(...)
local iface = "" local iface = ""
local arg = { ... } local arg = { ... }
utils.log(#arg, arg)
if #arg == 0 then if #arg == 0 then
recv = get_struct_name() recv = get_struct_name()
iface = vim.fn.input("Impl: generating method stubs for interface: ") iface = vim.fn.input("Impl: generating method stubs for interface: ")
vim.cmd("redraw!") vim.cmd("redraw!")
-- print("Usage: GoImpl f *File io.Reader")
if iface == "" then if iface == "" then
print("Impl: please input interface name e.g. io.Reader") print("Impl: please input interface name e.g. io.Reader")
-- print("Usage: GoImpl f *File io.Reader")
end end
elseif #arg == 1 then elseif #arg == 1 then
-- " i.e: ':GoImpl io.Writer' -- " i.e: ':GoImpl io.Writer'
recv = get_struct_name() recv = get_struct_name()
vim.cmd("redraw!")
iface = select(1, ...) iface = select(1, ...)
elseif #arg == 2 then
-- " i.e: ':GoImpl s io.Writer'
recv = get_struct_name()
local recv_name = select(1, ...)
recv = string.format("%s %s", recv_name, recv)
utils.log(recv)
local l = #arg
iface = select(l, ...)
recv = select(l - 1, ...)
elseif #arg > 2 then elseif #arg > 2 then
local l = #arg local l = #arg
iface = select(l, ...) iface = select(l, ...)
@ -45,24 +64,22 @@ local run = function(...)
local dir = vim.fn.fnameescape(vim.fn.expand("%:p:h")) local dir = vim.fn.fnameescape(vim.fn.expand("%:p:h"))
setup = string.format("%s -dir %s '%s' %s ", setup, dir, recv, iface) setup = { setup, "-dir", dir, recv, iface }
utils.log(setup) utils.log(setup)
vim.cmd("normal! $%") -- vim.cmd("normal! $%") -- do a bracket match. changed to treesitter
local data = vim.fn.systemlist(setup) local data = vim.fn.systemlist(setup)
data = utils.handle_job_data(data) data = utils.handle_job_data(data)
if not data then if not data then
return return
end end
--
utils.log(data)
local pos = vim.fn.getcurpos()[2] local pos = vim.fn.getcurpos()[2]
table.insert(data, 1, "")
vim.fn.append(pos, data) vim.fn.append(pos, data)
-- vim.cmd("silent normal! j=2j") -- vim.cmd("silent normal! j=2j")
vim.fn.setpos(".", pos) -- vim.fn.setpos(".", pos)
vim.cmd("silent normal! 4j") -- vim.cmd("silent normal! 4j")
-- --
end end

Loading…
Cancel
Save