This commit is contained in:
Aleksei Malykh 2025-07-14 19:00:21 +07:00
parent d3d51c1ef8
commit 395441bac3
No known key found for this signature in database
GPG key ID: 00FB366088A4D4CA
8 changed files with 108 additions and 15 deletions

View file

@ -148,7 +148,7 @@ require("gopher").setup {
</summary> </summary>
```vim ```vim
" Open alternate test file (if _test file does not exist it will create it) " Open alternate test file
:GoAlt :GoAlt
" Open test file in horizontal split " Open test file in horizontal split

View file

@ -1,22 +1,27 @@
local M = {} local alt = {}
function M.is_test_file() function alt.is_test_file()
local file = vim.fn.expand "%" local file = vim.fn.expand "%"
if #file <= 1 then if #file <= 1 then
vim.notify("no buffer name", vim.log.levels.ERROR) vim.notify("no buffer name", vim.log.levels.ERROR)
return nil, false, false return nil, false, false
end end
local is_test = string.find(file, "_test%.go$") local is_test = string.find(file, "_test%.go$")
local is_source = string.find(file, "%.go$") local is_source = string.find(file, "%.go$")
return file, (not is_test and is_source), is_test return file, (not is_test and is_source), is_test
end end
function M.alternate() function alt.alternate()
local file, is_source, is_test = M.is_test_file() local file, is_source, is_test = alt.is_test_file()
if not file then if not file then
return nil return nil
end end
local alt_file = file local alt_file = file
if is_test then if is_test then
alt_file = string.gsub(file, "_test.go", ".go") alt_file = string.gsub(file, "_test.go", ".go")
elseif is_source then elseif is_source then
@ -24,15 +29,16 @@ function M.alternate()
else else
vim.notify("not a go file", vim.log.levels.ERROR) vim.notify("not a go file", vim.log.levels.ERROR)
end end
return alt_file return alt_file
end end
function M.switch(bang, cmd) function alt.switch(cmd)
local alt_file = M.alternate() cmd = cmd or ""
if not vim.fn.filereadable(alt_file) and not vim.fn.bufexists(alt_file) and not bang then
vim.notify("couldn't find " .. alt_file, vim.log.levels.ERROR) local alt_file = alt.alternate()
return
elseif #cmd <= 1 then if #cmd <= 1 then
local ocmd = "e " .. alt_file local ocmd = "e " .. alt_file
vim.cmd(ocmd) vim.cmd(ocmd)
else else
@ -41,4 +47,4 @@ function M.switch(bang, cmd)
end end
end end
return M return alt

View file

@ -88,13 +88,13 @@ end, "?")
-- :GoAlt -- :GoAlt
cmd("GoAlt", function() cmd("GoAlt", function()
require("gopher.alternate").switch(true, "") require("gopher.alternate").switch()
end) end)
cmd("GoAltV", function() cmd("GoAltV", function()
require("gopher.alternate").switch(true, "vsplit") require("gopher.alternate").switch "vsplit"
end) end)
cmd("GoAltS", function() cmd("GoAltS", function()
require("gopher.alternate").switch(true, "split") require("gopher.alternate").switch "split"
end) end)

View file

@ -0,0 +1 @@
package main

View file

@ -0,0 +1 @@
package main

1
spec/fixtures/alternate/test_input.go vendored Normal file
View file

@ -0,0 +1 @@
package main

View file

@ -0,0 +1 @@
package main

View file

@ -0,0 +1,83 @@
-- spec/integration/alternate_test.lua
--
-- Integration tests for the GoAlt family of commands after removing the
-- `bang` flag. The commands now *always* open or create the sibling file.
--
-- Conventions match comment/iferr/impl suites: use `t.setup_test()`,
-- run the Ex-command via `child.cmd`, then `write`, then assertions.
local t = require "spec.testutils"
local child, T = t.setup "alternate"
-- ── small helpers ────────────────────────────────────────────────────────
---foo.go ⇄ foo_test.go
local function sibling(path)
if path:find "_test%.go$" then
return path:gsub("_test%.go$", ".go")
else
return path:gsub("%.go$", "_test.go")
end
end
---Run <cmd> inside a tmp copy of fixture `name`.
---@param name string "source" or "test"
---@param cmd string "GoAlt", "GoAltV", "GoAltS"
---@return table rs from t.setup_test (for later cleanup)
---@return string absolute sibling path
local function do_the_test(name, cmd)
local rs = t.setup_test("alternate/" .. name, child, { 1, 1 })
local alt = sibling(rs.tmp)
os.remove(alt)
child.cmd(cmd)
child.cmd "write"
return rs, alt
end
T["GoAlt"] = MiniTest.new_set {}
T["GoAltV"] = MiniTest.new_set {}
T["GoAltS"] = MiniTest.new_set {}
T["GoAlt"]["creates/opens sibling from source"] = function()
local rs, alt = do_the_test("source", "GoAlt")
t.eq(child.fn.expand "%:p", alt)
t.eq(child.fn.filereadable(alt), 0)
t.cleanup(rs)
t.deletefile(alt)
end
T["GoAlt"]["toggles back when invoked on test file"] = function()
local rs, src = do_the_test("test", "GoAlt")
t.eq(child.fn.expand "%:p", src)
t.eq(child.fn.filereadable(src), 0)
t.cleanup(rs)
t.deletefile(src)
end
T["GoAltV"]["opens sibling in vertical split"] = function()
local rs, alt = do_the_test("source", "GoAltV")
t.eq(#child.api.nvim_list_wins(), 2)
t.eq(child.fn.expand "%:p", alt)
t.cleanup(rs)
t.deletefile(alt)
end
T["GoAltS"]["opens sibling in horizontal split"] = function()
local rs, alt = do_the_test("source", "GoAltS")
t.eq(#child.api.nvim_list_wins(), 2)
t.eq(child.fn.expand "%:p", alt)
t.cleanup(rs)
t.deletefile(alt)
end
return T