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

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

* generate docs
This commit is contained in:
Smirnov Oleksandr 2025-03-03 14:22:28 +02:00 committed by GitHub
parent bb31271311
commit d1a21bffab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 48 additions and 1 deletions

View file

@ -88,6 +88,11 @@ You can look at default options |gopher.nvim-config-defaults|
-- default tags to add to struct fields
default_tag = "json",
},
iferr = {
-- choose a custom error message
---@type string|nil
message = nil,
},
}
<
Class ~

View file

@ -66,6 +66,11 @@ local default_config = {
-- default tags to add to struct fields
default_tag = "json",
},
iferr = {
-- choose a custom error message
---@type string|nil
message = nil,
},
}
--minidoc_afterlines_end

View file

@ -15,7 +15,13 @@ function iferr.iferr()
local pos = vim.fn.getcurpos()[2]
local fpath = vim.fn.expand "%"
local rs = r.sync({ c.commands.iferr, "-pos", curb }, {
local cmd = { c.commands.iferr, "-pos", curb }
if c.iferr.message ~= nil and type(c.iferr.message) == "string" then
table.insert(cmd, "-message")
table.insert(cmd, c.iferr.message)
end
local rs = r.sync(cmd, {
stdin = u.readfile_joined(fpath),
})

7
spec/fixtures/iferr/message_input.go vendored Normal file
View file

@ -0,0 +1,7 @@
package main
func getErr() error { return nil }
func test() error {
err := getErr()
}

10
spec/fixtures/iferr/message_output.go vendored Normal file
View file

@ -0,0 +1,10 @@
package main
func getErr() error { return nil }
func test() error {
err := getErr()
if err != nil {
return fmt.Errorf("failed to %w", err)
}
}

View file

@ -23,4 +23,18 @@ T["iferr"]["works"] = function()
t.eq(t.readfile(tmp), fixtures.output)
end
T["iferr"]["works with custom message"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "iferr/message"
t.writefile(tmp, fixtures.input)
child.lua [[ require("gopher").setup { iferr = { message = 'fmt.Errorf("failed to %w", err)' } } ]]
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 6, 2, 0 })
child.cmd "GoIfErr"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
end
return T