[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(iferr): add message argument (#133)

authored by

Jaren Glenn and committed by
GitHub
d5d4e214 eeb4850d

+61 -12
+24 -3
doc/gopher.nvim.txt
··· 272 272 ------------------------------------------------------------------------------ 273 273 *gopher.nvim-iferr* 274 274 275 - `iferr` provides a way to way to automatically insert `if err != nil` check. 276 - If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| 275 + `iferr` provides a way to automatically insert `if err != nil` check. 276 + To configure a default `-message` option for the `iferr` tool, see |gopher.nvim-config| 277 277 278 278 Usage ~ 279 - Execute `:GoIfErr` near any `err` variable to insert the check 279 + 280 + 1. Insert error check: 281 + - Place your cursor near any `err` variable 282 + - Run `:GoIfErr` 283 + 284 + 2. Insert error check with custom `-message`: 285 + - Place your cursor near any`err` variable 286 + - Run `:GoIfErr fmt.Errorf("failed to %w", err)` 287 + 288 + Example: 289 + >go 290 + func test() error { 291 + err := doSomething() 292 + // ^ put your cursor here 293 + // run `:GoIfErr` or `:GoIfErr log.Printf("error: %v", err)` 294 + } 295 + 296 + ------------------------------------------------------------------------------ 297 + *iferr.iferr()* 298 + `iferr.iferr`({message}) 299 + Parameters ~ 300 + {message} `(optional)` `(string)` Optional custom error message to use instead of config default 280 301 281 302 282 303 ==============================================================================
+24 -6
lua/gopher/iferr.lua
··· 3 3 ---@toc_entry Iferr 4 4 ---@tag gopher.nvim-iferr 5 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| 6 + --- `iferr` provides a way to automatically insert `if err != nil` check. 7 + --- To configure a default `-message` option for the `iferr` tool, see |gopher.nvim-config| 8 + --- 9 + ---@usage 10 + --- 1. Insert error check: 11 + --- - Place your cursor near any `err` variable 12 + --- - Run `:GoIfErr` 13 + --- 14 + --- 2. Insert error check with custom `-message`: 15 + --- - Place your cursor near any`err` variable 16 + --- - Run `:GoIfErr fmt.Errorf("failed to %w", err)` 8 17 --- 9 - ---@usage Execute `:GoIfErr` near any `err` variable to insert the check 18 + --- Example: 19 + --- >go 20 + --- func test() error { 21 + --- err := doSomething() 22 + --- // ^ put your cursor here 23 + --- // run `:GoIfErr` or `:GoIfErr log.Printf("error: %v", err)` 24 + --- } 25 + --- 10 26 11 27 local c = require "gopher.config" 12 28 local u = require "gopher._utils" ··· 14 30 local log = require "gopher._utils.log" 15 31 local iferr = {} 16 32 17 - function iferr.iferr() 33 + ---@param message? string Optional custom error message to use instead of config default 34 + function iferr.iferr(message) 18 35 local curb = vim.fn.wordcount().cursor_bytes 19 36 local pos = vim.fn.getcurpos()[2] 20 37 local fpath = vim.fn.expand "%" 21 38 22 39 local cmd = { c.commands.iferr, "-pos", curb } 23 - if c.iferr.message ~= nil and type(c.iferr.message) == "string" then 40 + local msg = message or c.iferr.message 41 + if msg ~= nil and type(msg) == "string" then 24 42 table.insert(cmd, "-message") 25 - table.insert(cmd, c.iferr.message) 43 + table.insert(cmd, msg) 26 44 end 27 45 28 46 local rs = r.sync(cmd, {
+4 -3
plugin/gopher.lua
··· 24 24 vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile()) 25 25 end) 26 26 27 - cmd("GoIfErr", function() 28 - require("gopher").iferr() 29 - end) 27 + cmd("GoIfErr", function(opts) 28 + local msg = ((opts.args ~= "" and opts.args) or nil) 29 + require("gopher").iferr(msg) 30 + end, "*") 30 31 31 32 cmd("GoCmt", function() 32 33 require("gopher").comment()
+9
spec/integration/iferr_test.lua
··· 25 25 t.cleanup(rs) 26 26 end 27 27 28 + iferr["should add if err with message argument"] = function() 29 + local rs = t.setup_test("iferr/message", child, { 6, 2 }) 30 + child.cmd [[GoIfErr fmt.Errorf("failed to %w", err)]] 31 + child.cmd "write" 32 + 33 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 34 + t.cleanup(rs) 35 + end 36 + 28 37 return T