feat: add impl support

docs: add impl
This commit is contained in:
Smirnov Olexander 2022-06-15 12:44:04 +03:00
parent e4323581be
commit 6fd413c4c2
5 changed files with 104 additions and 7 deletions

View file

@ -23,25 +23,27 @@ Also, run `TSInstall go` if install the `go` parser if not installed yet.
## Features ## Features
1. Install requires go tools: 1. Install requires go tools:
```vim
:GoInstallDeps
```
This will install next tools: This will install next tools:
- [gomodifytags](https://github.com/fatih/gomodifytags) - [gomodifytags](https://github.com/fatih/gomodifytags)
- [impl](https://github.com/josharian/impl)
```viml
:GoInstallDeps
```
2. Modify struct tags: 2. Modify struct tags:
By default be added/removed `json` tag, if not set. By default be added/removed `json` tag, if not set.
```viml ```vim
:GoTagAdd json " For add json tag :GoTagAdd json " For add json tag
:GoTagRm yaml " For remove yaml tag :GoTagRm yaml " For remove yaml tag
``` ```
3. Run `go mod` command 3. Run `go mod` command
```viml ```vim
:GoMod tidy " Runs `go mod tidy` :GoMod tidy " Runs `go mod tidy`
:GoMod init asdf " Runs `go mod init asdf` :GoMod init asdf " Runs `go mod init asdf`
``` ```
@ -52,10 +54,28 @@ Link can has a `http` or `https` prefix.
You can provide more that one package url. You can provide more that one package url.
```viml ```vim
:GoGet github.com/gorilla/mux :GoGet github.com/gorilla/mux
``` ```
5. Interface implementation
Command syntax:
```vim
:GoImpl [receiver] [interface]
" Also you can put cursor on the struct and run:
:GoImpl [interface]
```
Example of usage:
```vim
" Example
:GoImpl r Read io.Reader
" or simply put your cursor in the struct and run:
:GoImpl io.Reader
```
## Thanks ## Thanks
- [go.nvim](https://github.com/ray-x/go.nvim) - [go.nvim](https://github.com/ray-x/go.nvim)

74
lua/gopher/impl.lua Normal file
View file

@ -0,0 +1,74 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local function get_struct()
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then
print "put cursor on 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
return function(...)
local args = { ... }
local iface, recv_name = "", ""
local recv = get_struct()
if #args == 0 then
iface = vim.fn.input "impl: generating method stubs for interface: "
vim.cmd "redeaw!"
if iface == "" then
print "usage: GoImpl f *File io.Reader"
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
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)
end
-- stylua: ignore
local cmd_args = {
"-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), ---@diagnostic disable-line: missing-parameter
recv,
iface
}
local res_data
Job
:new({
command = "impl",
args = cmd_args,
on_exit = function(data, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
return
end
res_data = data:result()
end,
})
:sync()
local pos = vim.fn.getcurpos()[2]
table.insert(res_data, 1, "")
vim.fn.append(pos, res_data)
-- table.insert(res_data, 1, "")
-- vim.fn.append(vim.fn.getcurpos()[2], res_data)
end

View file

@ -6,5 +6,6 @@ gopher.tags_add = tags.add
gopher.tags_rm = tags.remove gopher.tags_rm = tags.remove
gopher.mod = require "gopher.gomod" gopher.mod = require "gopher.gomod"
gopher.get = require "gopher.goget" gopher.get = require "gopher.goget"
gopher.impl = require "gopher.impl"
return gopher return gopher

View file

@ -2,6 +2,7 @@ local Job = require "plenary.job"
local M = { local M = {
urls = { urls = {
gomodifytags = "github.com/fatih/gomodifytags", gomodifytags = "github.com/fatih/gomodifytags",
impl = "github.com/josharian/impl",
}, },
} }

View file

@ -2,4 +2,5 @@ command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>)
command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>) command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>)
command! -nargs=* GoMod :lua require"gopher".mod(<f-args>) command! -nargs=* GoMod :lua require"gopher".mod(<f-args>)
command! -nargs=* GoGet :lua require"gopher".get(<f-args>) command! -nargs=* GoGet :lua require"gopher".get(<f-args>)
command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>)
command! GoInstallDeps :lua require"gopher".install_deps() command! GoInstallDeps :lua require"gopher".install_deps()