gopher.nvim/lua/gopher/iferr.lua(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
-- Thanks https://github.com/koron/iferr for vim implementation
---@toc_entry Iferr
---@tag gopher.nvim-iferr
---@text
--- `iferr` provides a way to automatically insert `if err != nil` check.
--- To configure a default `-message` option for the `iferr` tool, see |gopher.nvim-config|
---
---@usage
--- 1. Insert error check:
--- - Place your cursor near any `err` variable
--- - Run `:GoIfErr`
---
--- 2. Insert error check with custom `-message`:
--- - Place your cursor near any`err` variable
--- - Run `:GoIfErr fmt.Errorf("failed to %w", err)`
---
--- Example:
--- >go
--- func test() error {
--- err := doSomething()
--- // ^ put your cursor here
--- // run `:GoIfErr` or `:GoIfErr log.Printf("error: %v", err)`
--- }
---
local c = require "gopher.config"
local u = require "gopher._utils"
local r = require "gopher._utils.runner"
local log = require "gopher._utils.log"
local iferr = {}
---@param message? string Optional custom error message to use instead of config default
function iferr.iferr(message)
local curb = vim.fn.wordcount().cursor_bytes
local pos = vim.fn.getcurpos()[2]
local fpath = vim.fn.expand "%"
local cmd = { c.commands.iferr, "-pos", curb }
local msg = message or c.iferr.message
if msg ~= nil and type(msg) == "string" then
table.insert(cmd, "-message")
table.insert(cmd, msg)
end
local rs = r.sync(cmd, {
stdin = u.readfile_joined(fpath),
})
if rs.code ~= 0 then
if string.find(rs.stderr, "no functions at") then
u.notify("iferr: no function at " .. curb, vim.log.levels.ERROR)
log.warn("iferr: no function at " .. curb)
return
end
log.error("ferr: failed. output: " .. rs.stderr)
error("iferr failed: " .. rs.stderr)
end
vim.fn.append(pos, u.remove_empty_lines(vim.split(rs.stdout, "\n")))
vim.cmd [[silent normal! j=2j]]
vim.fn.setpos(".", pos --[[@as integer[] ]])
end
return iferr
|