feat(iferr): add *-message* support
This commit is contained in:
parent
bb31271311
commit
a76ec8dfdc
5 changed files with 43 additions and 1 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
7
spec/fixtures/iferr/message_input.go
vendored
Normal 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
10
spec/fixtures/iferr/message_output.go
vendored
Normal 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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue