local function get_split_window_type() local id = vim.fn.win_getid() local width = vim.fn.winwidth(id) local height = vim.fn.winheight(id) * 2.1 if height > width then return "split" else return "vsplit" end end local function get_cursor_byte_offset() return vim.fn.line2byte(vim.fn.line('.')) + (vim.fn.col('.') - 2) end function go_test() local test_info = vim.fn.json_decode( vim.fn.system( string.format("go-test-name -pos %d -file %s", get_cursor_byte_offset(), vim.fn.expand("%")) ) ) for _, v in ipairs(vim.api.nvim_list_bufs()) do if vim.fn.bufname(v) == "vim-go-test-func" then vim.fn.execute(string.format("bwipe! %d", v)) end end local dir = vim.fn.expand("%:p:h") if #(test_info["sub_test_names"]) > 0 then -- TODO: Investigate why sub test name is nil when accessed by sub_test_name[0] local sub_test_name = "" for _, v in ipairs(test_info["sub_test_names"]) do sub_test_name = v end cmd = string.format( "go test -coverprofile='/tmp/go-coverage.out' -json -v -count=1 -v -race -run='^%s$'/'^%s$' $(go list %s) 2>&1 | tee /tmp/gotest.log | gotestfmt" , test_info.test_func_name, sub_test_name, dir ) else cmd = string.format( "go test -coverprofile='/tmp/go-coverage.out' -json -v -count=1 -v -race -run='^%s$' $(go list %s) 2>&1 | tee /tmp/gotest.log | gotestfmt" , test_info.test_func_name, dir ) end local st = get_split_window_type() vim.fn.execute(string.format("%s gotest", st)) if split == "split" then vim.cmd [[ execute(printf('resize %s', floor(&lines * 0.3))) ]] end vim.call("termopen", cmd) vim.cmd [[ setlocal bufhidden=delete setlocal noswapfile setlocal nobuflisted file vim-go-test-func wincmd p ]] end