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