[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
at main 1.4 kB view raw
1-- Thanks https://github.com/koron/iferr for vim implementation 2 3---@toc_entry Iferr 4---@tag gopher.nvim-iferr 5---@text 6--- `iferr` provides a way to way to automatically insert `if err != nil` check. 7--- If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| 8--- 9---@usage Execute `:GoIfErr` near any `err` variable to insert the check 10 11local c = require "gopher.config" 12local u = require "gopher._utils" 13local r = require "gopher._utils.runner" 14local log = require "gopher._utils.log" 15local iferr = {} 16 17function iferr.iferr() 18 local curb = vim.fn.wordcount().cursor_bytes 19 local pos = vim.fn.getcurpos()[2] 20 local fpath = vim.fn.expand "%" 21 22 local cmd = { c.commands.iferr, "-pos", curb } 23 if c.iferr.message ~= nil and type(c.iferr.message) == "string" then 24 table.insert(cmd, "-message") 25 table.insert(cmd, c.iferr.message) 26 end 27 28 local rs = r.sync(cmd, { 29 stdin = u.readfile_joined(fpath), 30 }) 31 32 if rs.code ~= 0 then 33 if string.find(rs.stderr, "no functions at") then 34 u.notify("iferr: no function at " .. curb, vim.log.levels.ERROR) 35 log.warn("iferr: no function at " .. curb) 36 return 37 end 38 39 log.error("ferr: failed. output: " .. rs.stderr) 40 error("iferr failed: " .. rs.stderr) 41 end 42 43 vim.fn.append(pos, u.remove_empty_lines(vim.split(rs.stdout, "\n"))) 44 vim.cmd [[silent normal! j=2j]] 45 vim.fn.setpos(".", pos --[[@as integer[] ]]) 46end 47 48return iferr