go.nvim/lua/go/alternate.lua
Prescott Murphy 82d796a01f
Handle literal "go" in path for GoAlt command (#93)
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.
2022-03-14 17:18:31 +11:00

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