From 6fd413c4c2053e718be69fb816a94ce8322f0f21 Mon Sep 17 00:00:00 2001 From: Smirnov Olexander Date: Wed, 15 Jun 2022 12:44:04 +0300 Subject: [PATCH] feat: add impl support docs: add impl --- README.md | 34 ++++++++++++++---- lua/gopher/impl.lua | 74 ++++++++++++++++++++++++++++++++++++++++ lua/gopher/init.lua | 1 + lua/gopher/installer.lua | 1 + plugin/gopher.vim | 1 + 5 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 lua/gopher/impl.lua diff --git a/README.md b/README.md index f02f3ea..c48dcfc 100644 --- a/README.md +++ b/README.md @@ -23,25 +23,27 @@ Also, run `TSInstall go` if install the `go` parser if not installed yet. ## Features 1. Install requires go tools: + +```vim +:GoInstallDeps +``` + This will install next tools: - [gomodifytags](https://github.com/fatih/gomodifytags) - -```viml -:GoInstallDeps -``` +- [impl](https://github.com/josharian/impl) 2. Modify struct tags: By default be added/removed `json` tag, if not set. -```viml +```vim :GoTagAdd json " For add json tag :GoTagRm yaml " For remove yaml tag ``` 3. Run `go mod` command -```viml +```vim :GoMod tidy " Runs `go mod tidy` :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. -```viml +```vim :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 - [go.nvim](https://github.com/ray-x/go.nvim) diff --git a/lua/gopher/impl.lua b/lua/gopher/impl.lua new file mode 100644 index 0000000..b19c84a --- /dev/null +++ b/lua/gopher/impl.lua @@ -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 diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index c01abc4..3ada8c4 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -6,5 +6,6 @@ gopher.tags_add = tags.add gopher.tags_rm = tags.remove gopher.mod = require "gopher.gomod" gopher.get = require "gopher.goget" +gopher.impl = require "gopher.impl" return gopher diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua index c1213c5..6e0bd87 100644 --- a/lua/gopher/installer.lua +++ b/lua/gopher/installer.lua @@ -2,6 +2,7 @@ local Job = require "plenary.job" local M = { urls = { gomodifytags = "github.com/fatih/gomodifytags", + impl = "github.com/josharian/impl", }, } diff --git a/plugin/gopher.vim b/plugin/gopher.vim index 1e68e0a..caeeb80 100644 --- a/plugin/gopher.vim +++ b/plugin/gopher.vim @@ -2,4 +2,5 @@ command! -nargs=* GoTagAdd :lua require"gopher".tags_add() command! -nargs=* GoTagRm :lua require"gopher".tags_rm() command! -nargs=* GoMod :lua require"gopher".mod() command! -nargs=* GoGet :lua require"gopher".get() +command! -nargs=* GoImpl :lua require"gopher".impl() command! GoInstallDeps :lua require"gopher".install_deps()