all repos

gopher.nvim @ d5d4e21

Minimalistic plugin for Go development
4 files changed, 61 insertions(+), 12 deletions(-)
feat(iferr): add message argument (#133)

Author: Jaren Glenn jarenglenn@gmail.com
Committed by: GitHub noreply@github.com
Committed at: 2026-02-23 23:59:04 +0200
Parent: eeb4850
M doc/gopher.nvim.txt

@@ -272,11 +272,32 @@ ==============================================================================

------------------------------------------------------------------------------ *gopher.nvim-iferr* -`iferr` provides a way to way to automatically insert `if err != nil` check. -If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| +`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 ~ -Execute `:GoIfErr` near any `err` variable to insert the check + +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)` + } + +------------------------------------------------------------------------------ + *iferr.iferr()* + `iferr.iferr`({message}) +Parameters ~ +{message} `(optional)` `(string)` Optional custom error message to use instead of config default ==============================================================================
M lua/gopher/iferr.lua

@@ -3,10 +3,26 @@

---@toc_entry Iferr ---@tag gopher.nvim-iferr ---@text ---- `iferr` provides a way to way to automatically insert `if err != nil` check. ---- If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| +--- `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)` --- ----@usage Execute `:GoIfErr` near any `err` variable to insert the check +--- 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"

@@ -14,15 +30,17 @@ local r = require "gopher._utils.runner"

local log = require "gopher._utils.log" local iferr = {} -function iferr.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 } - if c.iferr.message ~= nil and type(c.iferr.message) == "string" then + local msg = message or c.iferr.message + if msg ~= nil and type(msg) == "string" then table.insert(cmd, "-message") - table.insert(cmd, c.iferr.message) + table.insert(cmd, msg) end local rs = r.sync(cmd, {
M plugin/gopher.lua

@@ -24,9 +24,10 @@ cmd("GopherLog", function()

vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile()) end) -cmd("GoIfErr", function() - require("gopher").iferr() -end) +cmd("GoIfErr", function(opts) + local msg = ((opts.args ~= "" and opts.args) or nil) + require("gopher").iferr(msg) +end, "*") cmd("GoCmt", function() require("gopher").comment()
M spec/integration/iferr_test.lua

@@ -25,4 +25,13 @@ t.eq(t.readfile(rs.tmp), rs.fixtures.output)

t.cleanup(rs) end +iferr["should add if err with message argument"] = function() + local rs = t.setup_test("iferr/message", child, { 6, 2 }) + child.cmd [[GoIfErr fmt.Errorf("failed to %w", err)]] + child.cmd "write" + + t.eq(t.readfile(rs.tmp), rs.fixtures.output) + t.cleanup(rs) +end + return T