From eb19ecdc05bdd5231a010a2452b7bde261b31f99 Mon Sep 17 00:00:00 2001 From: Smirnov Oleksandr Date: Thu, 7 Jul 2022 14:19:07 +0300 Subject: [PATCH] Add `iferr` integration (#15) * feat(iferr): add to installer & config * feat(iferr): add first implementation * docs(iferr): add --- README.md | 12 ++++++++++-- lua/gopher/config.lua | 1 + lua/gopher/iferr.lua | 21 +++++++++++++++++++++ lua/gopher/init.lua | 1 + lua/gopher/installer.lua | 1 + plugin/gopher.vim | 1 + 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 lua/gopher/iferr.lua diff --git a/README.md b/README.md index 36d1d8b..05e45f2 100644 --- a/README.md +++ b/README.md @@ -130,12 +130,20 @@ Generate tests only for exported functions/methods in current file: 8. Generate doc comment -First set a cursor on **public** package/function/interface/struct and execure: +First set a cursor on **public** package/function/interface/struct and execute: ```vim :GoCmt ``` -## Thanks: +9. Generate `if err` + +Set cursor on the line with **err** and execute: + +```vim +:GoIfErr +``` + +## Thanks - [go.nvim](https://github.com/ray-x/go.nvim) diff --git a/lua/gopher/config.lua b/lua/gopher/config.lua index c736d9d..46e7edc 100644 --- a/lua/gopher/config.lua +++ b/lua/gopher/config.lua @@ -6,6 +6,7 @@ local M = { gomodifytags = "gomodifytags", gotests = "gotests", impl = "impl", + iferr = "iferr", }, }, } diff --git a/lua/gopher/iferr.lua b/lua/gopher/iferr.lua new file mode 100644 index 0000000..24d4041 --- /dev/null +++ b/lua/gopher/iferr.lua @@ -0,0 +1,21 @@ +local c = require("gopher.config").config.commands +local u = require "gopher._utils" + +---Add iferr declaration +---That's Lua of vimscript implementation of: +---github.com/koron/iferr +return function() + local boff = vim.fn.wordcount().cursor_bytes + local cmd = (c.iferr .. " -pos " .. boff) + local data = vim.fn.systemlist(cmd, vim.fn.bufnr "%") + + if vim.v.shell_error ~= 0 then + u.notify("command " .. cmd .. " exited with code " .. vim.v.shell_error, "error") + return + end + + local pos = vim.fn.getcurpos()[2] + vim.fn.append(pos, data) + vim.cmd [[silent normal! j=2j]] + vim.fn.setpos(".", pos) +end diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 54ae79e..39f5ca9 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -9,6 +9,7 @@ gopher.mod = require "gopher.gomod" gopher.get = require "gopher.goget" gopher.impl = require "gopher.impl" gopher.generate = require "gopher.gogenerate" +gopher.iferr = require "gopher.iferr" gopher.comment = require "gopher.comment" gopher.test_add = gotests.func_test gopher.test_exported = gotests.all_exported_tests diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua index 3251d71..4df048b 100644 --- a/lua/gopher/installer.lua +++ b/lua/gopher/installer.lua @@ -4,6 +4,7 @@ local urls = { gomodifytags = "github.com/fatih/gomodifytags", impl = "github.com/josharian/impl", gotests = "github.com/cweill/gotests/...", + iferr = "github.com/koron/iferr", } ---@param pkg string diff --git a/plugin/gopher.vim b/plugin/gopher.vim index 942cc04..089c330 100644 --- a/plugin/gopher.vim +++ b/plugin/gopher.vim @@ -8,4 +8,5 @@ command! -nargs=* GoGet :lua require"gopher".get() command! -nargs=* GoImpl :lua require"gopher".impl() command! -nargs=* GoGenerate :lua require"gopher".generate() command! GoCmt :lua require"gopher".comment() +command! GoIfErr :lua require"gopher".iferr() command! GoInstallDeps :lua require"gopher".install_deps()