6 files changed,
48 insertions(+),
1 deletions(-)
Author:
Smirnov Oleksandr
ss2316544@gmail.com
Committed by:
GitHub
noreply@github.com
Committed at:
2025-03-03 14:22:28 +0200
Parent:
bb31271
M
doc/gopher.nvim.txt
@@ -88,6 +88,11 @@
-- default tags to add to struct fields default_tag = "json", }, + iferr = { + -- choose a custom error message + ---@type string|nil + message = nil, + }, } < Class ~
M
lua/gopher/config.lua
@@ -66,6 +66,11 @@
-- default tags to add to struct fields default_tag = "json", }, + iferr = { + -- choose a custom error message + ---@type string|nil + message = nil, + }, } --minidoc_afterlines_end
M
lua/gopher/iferr.lua
@@ -15,7 +15,13 @@ local curb = vim.fn.wordcount().cursor_bytes
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), })
A
spec/fixtures/iferr/message_input.go
@@ -0,0 +1,7 @@
+package main + +func getErr() error { return nil } + +func test() error { + err := getErr() +}
A
spec/fixtures/iferr/message_output.go
@@ -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) + } +}
M
spec/integration/iferr_test.lua
@@ -23,4 +23,18 @@
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