2021-07-13 22:39:27 +00:00
|
|
|
local M = {}
|
2022-05-29 13:07:39 +00:00
|
|
|
function M.alternate()
|
2021-07-13 22:39:27 +00:00
|
|
|
local file = vim.fn.expand('%')
|
|
|
|
local alt_file = ""
|
|
|
|
if #file <= 1 then
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify("no buffer name", vim.lsp.log_levels.ERROR)
|
2021-07-13 22:39:27 +00:00
|
|
|
return
|
|
|
|
end
|
2022-06-01 11:29:13 +00:00
|
|
|
local s = string.find(file, "_test%.go$")
|
|
|
|
local s2 = string.find(file, "%.go$")
|
2021-07-13 22:39:27 +00:00
|
|
|
if s ~= nil then
|
2022-03-14 06:18:31 +00:00
|
|
|
alt_file = string.gsub(file, "_test.go", ".go")
|
2021-07-13 22:39:27 +00:00
|
|
|
elseif s2 ~= nil then
|
2022-03-14 06:18:31 +00:00
|
|
|
alt_file = vim.fn.expand('%:r') .. "_test.go"
|
2021-07-13 22:39:27 +00:00
|
|
|
else
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify('not a go file', vim.lsp.log_levels.ERROR)
|
2021-07-13 22:39:27 +00:00
|
|
|
end
|
2022-05-29 13:07:39 +00:00
|
|
|
return alt_file
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.switch(bang, cmd)
|
|
|
|
local alt_file = M.alternate()
|
2021-07-13 22:39:27 +00:00
|
|
|
if not vim.fn.filereadable(alt_file) and not vim.fn.bufexists(alt_file) and not bang then
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify("couldn't find " .. alt_file, vim.lsp.log_levels.ERROR)
|
2021-07-13 22:39:27 +00:00
|
|
|
return
|
|
|
|
elseif #cmd <= 1 then
|
|
|
|
local ocmd = "e " .. alt_file
|
|
|
|
vim.cmd(ocmd)
|
|
|
|
else
|
|
|
|
local ocmd = cmd .. " " .. alt_file
|
|
|
|
vim.cmd(ocmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|