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

feat(iferr): add `-message` support (#89)

* feat(iferr): add *-message* support

* generate docs

authored by olexsmir.xyz and committed by GitHub d1a21bff bb312713

Changed files
+48 -1
doc
lua
spec
+5
doc/gopher.nvim.txt
··· 88 88 -- default tags to add to struct fields 89 89 default_tag = "json", 90 90 }, 91 + iferr = { 92 + -- choose a custom error message 93 + ---@type string|nil 94 + message = nil, 95 + }, 91 96 } 92 97 < 93 98 Class ~
+5
lua/gopher/config.lua
··· 66 66 -- default tags to add to struct fields 67 67 default_tag = "json", 68 68 }, 69 + iferr = { 70 + -- choose a custom error message 71 + ---@type string|nil 72 + message = nil, 73 + }, 69 74 } 70 75 --minidoc_afterlines_end 71 76
+7 -1
lua/gopher/iferr.lua
··· 15 15 local pos = vim.fn.getcurpos()[2] 16 16 local fpath = vim.fn.expand "%" 17 17 18 - local rs = r.sync({ c.commands.iferr, "-pos", curb }, { 18 + local cmd = { c.commands.iferr, "-pos", curb } 19 + if c.iferr.message ~= nil and type(c.iferr.message) == "string" then 20 + table.insert(cmd, "-message") 21 + table.insert(cmd, c.iferr.message) 22 + end 23 + 24 + local rs = r.sync(cmd, { 19 25 stdin = u.readfile_joined(fpath), 20 26 }) 21 27
+7
spec/fixtures/iferr/message_input.go
··· 1 + package main 2 + 3 + func getErr() error { return nil } 4 + 5 + func test() error { 6 + err := getErr() 7 + }
+10
spec/fixtures/iferr/message_output.go
··· 1 + package main 2 + 3 + func getErr() error { return nil } 4 + 5 + func test() error { 6 + err := getErr() 7 + if err != nil { 8 + return fmt.Errorf("failed to %w", err) 9 + } 10 + }
+14
spec/integration/iferr_test.lua
··· 23 23 t.eq(t.readfile(tmp), fixtures.output) 24 24 end 25 25 26 + T["iferr"]["works with custom message"] = function() 27 + local tmp = t.tmpfile() 28 + local fixtures = t.get_fixtures "iferr/message" 29 + t.writefile(tmp, fixtures.input) 30 + 31 + child.lua [[ require("gopher").setup { iferr = { message = 'fmt.Errorf("failed to %w", err)' } } ]] 32 + child.cmd("silent edit " .. tmp) 33 + child.fn.setpos(".", { child.fn.bufnr "%", 6, 2, 0 }) 34 + child.cmd "GoIfErr" 35 + child.cmd "write" 36 + 37 + t.eq(t.readfile(tmp), fixtures.output) 38 + end 39 + 26 40 return T