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

This commit is contained in:
ray-x 2022-05-19 14:12:14 +10:00
parent dbf0094f25
commit 52ccb2e690
4 changed files with 37 additions and 15 deletions

View File

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

View File

@ -280,7 +280,8 @@ COMMANDS *go-nvim-commands*
:GoImpl {options} *:GoImpl*
e.g. GoImpl {receiver} {interface}, will check if cursor is a valid
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
Closes the floating term.

View File

@ -162,7 +162,7 @@ function go.setup(cfg)
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=+ -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(

View File

@ -1,8 +1,7 @@
-- local ts_utils = require 'nvim-treesitter.ts_utils'
local utils = require("go.utils")
local impl = "impl"
-- GoImpl f *Foo io.Writer
local impl = "impl" -- GoImpl f *Foo io.Writer
-- use ts to get name
local function get_struct_name()
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")
end
utils.log(name)
name = name.name
return string.lower(name) .. " *" .. name
if name == nil then
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
local run = function(...)
@ -22,19 +30,30 @@ local run = function(...)
local iface = ""
local arg = { ... }
utils.log(#arg, arg)
if #arg == 0 then
recv = get_struct_name()
iface = vim.fn.input("Impl: generating method stubs for interface: ")
vim.cmd("redraw!")
-- print("Usage: GoImpl f *File io.Reader")
if iface == "" then
print("Impl: please input interface name e.g. io.Reader")
-- print("Usage: GoImpl f *File io.Reader")
end
elseif #arg == 1 then
-- " i.e: ':GoImpl io.Writer'
recv = get_struct_name()
vim.cmd("redraw!")
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
local l = #arg
iface = select(l, ...)
@ -45,24 +64,22 @@ local run = function(...)
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)
vim.cmd("normal! $%")
-- vim.cmd("normal! $%") -- do a bracket match. changed to treesitter
local data = vim.fn.systemlist(setup)
data = utils.handle_job_data(data)
if not data then
return
end
utils.log(data)
--
local pos = vim.fn.getcurpos()[2]
table.insert(data, 1, "")
vim.fn.append(pos, data)
-- vim.cmd("silent normal! j=2j")
vim.fn.setpos(".", pos)
vim.cmd("silent normal! 4j")
-- vim.fn.setpos(".", pos)
-- vim.cmd("silent normal! 4j")
--
end