82d796a01f
The original logic fails if you have go in your path, like: `/Code/mygoproject/myfile.go` because the split will have more than two elements.
32 lines
838 B
Lua
32 lines
838 B
Lua
local M = {}
|
|
function M.switch(bang, cmd)
|
|
local file = vim.fn.expand('%')
|
|
local alt_file = ""
|
|
if #file <= 1 then
|
|
vim.notify("no buffer name", vim.lsp.log_levels.ERROR)
|
|
return
|
|
end
|
|
local s, e = string.find(file, "_test%.go$")
|
|
local s2, e2 = string.find(file, "%.go$")
|
|
if s ~= nil then
|
|
alt_file = string.gsub(file, "_test.go", ".go")
|
|
elseif s2 ~= nil then
|
|
alt_file = vim.fn.expand('%:r') .. "_test.go"
|
|
else
|
|
vim.notify('not a go file', vim.lsp.log_levels.ERROR)
|
|
end
|
|
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.lsp.log_levels.ERROR)
|
|
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
|