2021-12-27 08:47:59 +00:00
|
|
|
local utils = require("go.utils")
|
2021-11-23 23:13:40 +00:00
|
|
|
local log = utils.log
|
2021-08-25 15:42:17 +00:00
|
|
|
local coverage = {}
|
|
|
|
local api = vim.api
|
2021-12-29 14:33:24 +00:00
|
|
|
local empty = utils.empty
|
2021-08-25 15:42:17 +00:00
|
|
|
local M = {}
|
|
|
|
local visable = false
|
|
|
|
_GO_NVIM_CFG = _GO_NVIM_CFG
|
|
|
|
local sign_define_cache = {}
|
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
M.sign_map = { covered = "goCoverageCovered", uncover = "goCoverageUncover" }
|
2021-08-25 15:42:17 +00:00
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
local ns = "gocoverage_ns"
|
2021-08-25 15:42:17 +00:00
|
|
|
|
|
|
|
local sign_covered = M.sign_map.covered
|
|
|
|
local sign_uncover = M.sign_map.uncover
|
|
|
|
|
|
|
|
local function sign_get(bufnr, name)
|
|
|
|
if sign_define_cache[bufnr] == nil then
|
|
|
|
sign_define_cache[bufnr] = {}
|
|
|
|
end
|
|
|
|
if not sign_define_cache[bufnr][name] then
|
|
|
|
local s = vim.fn.sign_getdefined(name)
|
|
|
|
if not vim.tbl_isempty(s) then
|
|
|
|
sign_define_cache[bufnr][name] = s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return sign_define_cache[bufnr][name]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- all windows and buffers
|
2021-12-29 14:33:24 +00:00
|
|
|
local function all_bufnr()
|
2021-08-25 15:42:17 +00:00
|
|
|
local bufnrl = {}
|
2021-12-29 14:33:24 +00:00
|
|
|
local buffers = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 })
|
|
|
|
|
|
|
|
for _, b in pairs(buffers) do
|
|
|
|
if not (vim.fn.empty(b.name) == 1 or b.hidden == 1) then
|
|
|
|
local name = b.name
|
|
|
|
|
|
|
|
local ext = string.sub(name, #name - 2)
|
|
|
|
if ext == ".go" then
|
|
|
|
table.insert(bufnrl, b.bufnr)
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-29 14:33:24 +00:00
|
|
|
return bufnrl
|
|
|
|
end -- log(bufnr, name, opts, redefine)
|
2021-08-25 15:42:17 +00:00
|
|
|
|
|
|
|
function M.define(bufnr, name, opts, redefine)
|
|
|
|
if sign_define_cache[bufnr] == nil then
|
|
|
|
sign_define_cache[bufnr] = {}
|
|
|
|
end
|
2021-12-22 03:34:10 +00:00
|
|
|
-- vim.notify(bufnr .. name .. opts .. redefine, vim.lsp.log_levels.DEBUG)
|
2021-08-25 15:42:17 +00:00
|
|
|
if redefine then
|
|
|
|
sign_define_cache[bufnr][name] = nil
|
|
|
|
vim.fn.sign_undefine(name)
|
|
|
|
vim.fn.sign_define(name, opts)
|
|
|
|
elseif not sign_get(name) then
|
|
|
|
-- log("define sign", name, vim.inspect(opts))
|
|
|
|
vim.fn.sign_define(name, opts)
|
|
|
|
end
|
|
|
|
-- vim.cmd([[sign list]])
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.remove(bufnr, lnum)
|
|
|
|
if bufnr == nil then
|
2021-12-27 08:47:59 +00:00
|
|
|
bufnr = vim.fn.bufnr("$")
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
2021-12-27 08:47:59 +00:00
|
|
|
vim.fn.sign_unplace(ns, { buffer = bufnr, id = lnum })
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function remove_all()
|
2021-12-29 14:33:24 +00:00
|
|
|
local bufnrs = all_bufnr()
|
2021-08-25 15:42:17 +00:00
|
|
|
for _, bid in pairs(bufnrs) do
|
|
|
|
M.remove(bid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.add(bufnr, signs)
|
|
|
|
local to_place = {}
|
|
|
|
for _, s in pairs(signs or {}) do
|
|
|
|
local count = s.cnt
|
|
|
|
local stype = "goCoverageCovered"
|
|
|
|
if count == 0 then
|
|
|
|
stype = "goCoverageUncover"
|
|
|
|
end
|
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
M.define(bufnr, stype, { text = _GO_NVIM_CFG.gocoverage_sign, texthl = stype })
|
|
|
|
for lnum = s.range.start.line, s.range["end"].line + 1 do
|
2021-12-29 14:33:24 +00:00
|
|
|
log(lnum, bufnr)
|
2021-08-25 15:42:17 +00:00
|
|
|
to_place[#to_place + 1] = {
|
|
|
|
id = lnum,
|
|
|
|
group = ns,
|
|
|
|
name = stype,
|
|
|
|
buffer = bufnr,
|
|
|
|
lnum = lnum,
|
2021-12-27 08:47:59 +00:00
|
|
|
priority = _GO_NVIM_CFG.gocoverage_sign_priority,
|
2021-08-25 15:42:17 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- log("placing", to_place)
|
|
|
|
vim.fn.sign_placelist(to_place)
|
|
|
|
return to_place -- for testing
|
|
|
|
end
|
|
|
|
|
|
|
|
M.highlight = function()
|
2021-12-27 08:47:59 +00:00
|
|
|
if vim.o.background == "dark" then
|
2021-08-25 15:42:17 +00:00
|
|
|
vim.cmd([[hi! goCoverageCovered guifg=#107040 ctermbg=28]])
|
|
|
|
vim.cmd([[hi! goCoverageUncover guifg=#A03040 ctermbg=52]])
|
|
|
|
else
|
|
|
|
vim.cmd([[hi! goCoverageCovered guifg=#70f0d0 ctermbg=120]])
|
|
|
|
vim.cmd([[hi! goCoverageUncover guifg=#f040d0 ctermbg=223]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function augroup()
|
|
|
|
vim.cmd([[ augroup gopher.vim-coverage ]])
|
|
|
|
vim.cmd([[ au! ]])
|
|
|
|
vim.cmd([[ au ColorScheme * lua require'go.coverage'.highlight() ]])
|
|
|
|
vim.cmd([[ au BufWinLeave *.go lua require'go.coverage'remove() ]])
|
|
|
|
vim.cmd([[ au BufWinEnter *.go lua require'go.coverage'enable_all() ]])
|
|
|
|
vim.cmd([[ augroup end ]])
|
|
|
|
end
|
|
|
|
|
|
|
|
local function enable_all()
|
2021-12-29 14:33:24 +00:00
|
|
|
local bufnrs = all_bufnr()
|
2021-08-25 15:42:17 +00:00
|
|
|
for _, bufnr in pairs(bufnrs) do
|
|
|
|
-- enable
|
|
|
|
-- local bufnr = vim.fn.winbufnr(id)
|
|
|
|
local fn = vim.fn.bufname(bufnr)
|
|
|
|
|
|
|
|
local filename = vim.fn.fnamemodify(fn, ":t")
|
|
|
|
if coverage[fn] ~= nil then
|
|
|
|
M.add(bufnr, coverage[fn])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
M.toggle = function(show)
|
|
|
|
if (show == nil and visable == true) or show == false then
|
|
|
|
-- hide
|
|
|
|
visable = false
|
|
|
|
remove_all()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
visable = true
|
|
|
|
enable_all()
|
|
|
|
-- end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function parse_line(line)
|
|
|
|
local m = vim.fn.matchlist(line, [[\v([^:]+):(\d+)\.(\d+),(\d+)\.(\d+) (\d+) (\d+)]])
|
|
|
|
|
2021-12-29 14:33:24 +00:00
|
|
|
if empty(m) then
|
2021-08-25 15:42:17 +00:00
|
|
|
return {}
|
|
|
|
end
|
|
|
|
local path = m[2]
|
|
|
|
local filename = vim.fn.fnamemodify(m[2], ":t")
|
|
|
|
return {
|
|
|
|
file = path,
|
|
|
|
filename = filename,
|
|
|
|
range = {
|
2021-12-27 08:47:59 +00:00
|
|
|
start = { line = tonumber(m[3]), character = tonumber(m[4]) },
|
|
|
|
["end"] = { line = tonumber(m[5]), character = tonumber(m[6]) },
|
2021-08-25 15:42:17 +00:00
|
|
|
},
|
|
|
|
num = tonumber(m[7]),
|
2021-12-27 08:47:59 +00:00
|
|
|
cnt = tonumber(m[8]),
|
2021-08-25 15:42:17 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if vim.tbl_isempty(vim.fn.sign_getdefined(sign_covered)) then
|
2021-11-23 23:13:40 +00:00
|
|
|
vim.fn.sign_define(sign_covered, {
|
|
|
|
text = _GO_NVIM_CFG.gocoverage_sign,
|
2021-12-27 08:47:59 +00:00
|
|
|
texthl = "goCoverageCovered",
|
2021-11-23 23:13:40 +00:00
|
|
|
})
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if vim.tbl_isempty(vim.fn.sign_getdefined(sign_uncover)) then
|
2021-11-23 23:13:40 +00:00
|
|
|
vim.fn.sign_define(sign_uncover, {
|
|
|
|
text = _GO_NVIM_CFG.gocoverage_sign,
|
2021-12-27 08:47:59 +00:00
|
|
|
texthl = "goCoverageUncover",
|
2021-11-23 23:13:40 +00:00
|
|
|
})
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.read_cov = function(covfn)
|
2021-12-29 14:33:24 +00:00
|
|
|
if vim.fn.filereadable(covfn) == 0 then
|
|
|
|
vim.notify(string.format("cov file not exist: %s", covfn), vim.lsp.log_levels.WARN)
|
|
|
|
return
|
|
|
|
end
|
2021-08-25 15:42:17 +00:00
|
|
|
local cov = vim.fn.readfile(covfn)
|
|
|
|
-- log(vim.inspect(cov))
|
|
|
|
for _, line in pairs(cov) do
|
|
|
|
local cl = parse_line(line)
|
2021-12-27 08:47:59 +00:00
|
|
|
if cl.filename ~= nil or cl.range ~= nil then
|
|
|
|
-- log("cl", vim.inspect(cl))
|
|
|
|
if coverage[cl.filename] == nil then
|
|
|
|
coverage[cl.filename] = {}
|
|
|
|
end
|
|
|
|
table.insert(coverage[cl.filename], cl)
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-29 14:33:24 +00:00
|
|
|
local bufnrs = all_bufnr()
|
2021-08-25 15:42:17 +00:00
|
|
|
log("buffers", bufnrs)
|
2021-12-29 14:33:24 +00:00
|
|
|
local added = {}
|
2021-08-25 15:42:17 +00:00
|
|
|
for _, bid in pairs(bufnrs) do
|
2021-12-29 14:33:24 +00:00
|
|
|
-- if added[bid] == nil then
|
|
|
|
local fn = vim.fn.bufname(bid)
|
2021-08-25 15:42:17 +00:00
|
|
|
fn = vim.fn.fnamemodify(fn, ":t")
|
2021-12-29 14:33:24 +00:00
|
|
|
log(bid, fn)
|
2021-08-25 15:42:17 +00:00
|
|
|
M.add(bid, coverage[fn])
|
|
|
|
visable = true
|
2021-12-29 14:33:24 +00:00
|
|
|
added[bid] = true
|
|
|
|
-- end
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
|
|
|
return coverage
|
|
|
|
end
|
|
|
|
|
2021-11-23 23:13:40 +00:00
|
|
|
M.run = function(...)
|
2021-12-27 08:47:59 +00:00
|
|
|
local get_build_tags = require("go.gotest").get_build_tags
|
2021-08-25 15:42:17 +00:00
|
|
|
-- local cov = vim.fn.tempname()
|
2021-12-29 14:33:24 +00:00
|
|
|
local pwd = vim.fn.getcwd()
|
|
|
|
local cov = pwd .. utils.sep() .. "cover.cov"
|
2021-08-25 15:42:17 +00:00
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
local args = { ... }
|
2021-11-23 23:13:40 +00:00
|
|
|
log(args)
|
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
local test_runner = "go"
|
|
|
|
if _GO_NVIM_CFG.test_runner ~= "go" then
|
2021-11-23 23:13:40 +00:00
|
|
|
test_runner = _GO_NVIM_CFG.test_runner
|
|
|
|
require("go.install").install(test_runner)
|
|
|
|
end
|
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
local cmd = { test_runner, "test", "-coverprofile", cov }
|
|
|
|
local tags = ""
|
2021-11-23 23:13:40 +00:00
|
|
|
local args2 = {}
|
2021-12-29 14:33:24 +00:00
|
|
|
if not empty(args) then
|
2021-11-23 23:13:40 +00:00
|
|
|
tags, args2 = get_build_tags(args)
|
2021-12-27 08:47:59 +00:00
|
|
|
if tags ~= {} then
|
|
|
|
vim.list_extend(cmd, tags)
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
2021-12-29 14:33:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not empty(args2) then
|
|
|
|
log(args2)
|
|
|
|
cmd = vim.list_extend(cmd, args2)
|
|
|
|
else
|
|
|
|
argsstr = "." .. utils.sep() .. "..."
|
|
|
|
table.insert(cmd, argsstr)
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
local lines = { "" }
|
2021-08-25 15:42:17 +00:00
|
|
|
coverage = {}
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2021-08-25 15:42:17 +00:00
|
|
|
log("run coverage", cmd)
|
2021-11-23 23:13:40 +00:00
|
|
|
|
2021-12-27 08:47:59 +00:00
|
|
|
local argsstr = ""
|
2021-11-23 23:13:40 +00:00
|
|
|
if _GO_NVIM_CFG.run_in_floaterm then
|
|
|
|
cmd = table.concat(cmd, " ")
|
2021-12-29 14:33:24 +00:00
|
|
|
if empty(args2) then
|
2021-12-27 08:47:59 +00:00
|
|
|
cmd = cmd .. "." .. utils.sep() .. "..."
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
|
|
|
utils.log(cmd)
|
2021-12-27 08:47:59 +00:00
|
|
|
local term = require("go.term").run
|
|
|
|
term({ cmd = cmd, autoclose = false })
|
2021-11-23 23:13:40 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-08-25 15:42:17 +00:00
|
|
|
local j = vim.fn.jobstart(cmd, {
|
|
|
|
on_stdout = function(jobid, data, event)
|
|
|
|
log("go coverage " .. vim.inspect(data))
|
|
|
|
vim.list_extend(lines, data)
|
|
|
|
end,
|
|
|
|
on_stderr = function(job_id, data, event)
|
2021-12-27 08:47:59 +00:00
|
|
|
data = utils.handle_job_data(data)
|
|
|
|
if data == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
vim.notify(
|
|
|
|
"go coverage finished with message: "
|
|
|
|
.. vim.inspect(cmd)
|
|
|
|
.. "error: "
|
|
|
|
.. vim.inspect(data)
|
2021-12-29 14:33:24 +00:00
|
|
|
.. "job "
|
2021-12-27 08:47:59 +00:00
|
|
|
.. tostring(job_id)
|
2021-12-29 14:33:24 +00:00
|
|
|
.. "ev "
|
2021-12-27 08:47:59 +00:00
|
|
|
.. event,
|
|
|
|
vim.lsp.log_levels.ERROR
|
|
|
|
)
|
2021-08-25 15:42:17 +00:00
|
|
|
end,
|
|
|
|
on_exit = function(job_id, data, event)
|
|
|
|
if event ~= "exit" then
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify(string.format("%s %s %s", job_id, event, vim.inspect(data)), vim.lsp.log_levels.ERROR)
|
2021-08-25 15:42:17 +00:00
|
|
|
end
|
|
|
|
|
2021-12-29 14:33:24 +00:00
|
|
|
local lp = table.concat(lines, "\n")
|
|
|
|
vim.notify(string.format("test finished %s", vim.inspect(lp)), vim.lsp.log_levels.INFO)
|
|
|
|
coverage = M.read_cov(cov)
|
2021-08-25 15:42:17 +00:00
|
|
|
vim.fn.setqflist({}, " ", {
|
|
|
|
title = cmd,
|
2021-12-27 08:47:59 +00:00
|
|
|
lines = lines,
|
2021-12-29 14:33:24 +00:00
|
|
|
efm = vim.o.efm .. [[,]] .. require("go.gotest").efm(),
|
2021-08-25 15:42:17 +00:00
|
|
|
})
|
|
|
|
vim.api.nvim_command("doautocmd QuickFixCmdPost")
|
2021-12-14 16:59:14 +00:00
|
|
|
vim.cmd([[botright copen]])
|
2021-12-29 14:33:24 +00:00
|
|
|
-- vim.fn.delete(cov)
|
2021-12-27 08:47:59 +00:00
|
|
|
end,
|
2021-08-25 15:42:17 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|