2021-04-19 06:10:06 +00:00
|
|
|
-- https://phelipetls.github.io/posts/async-make-in-nvim-with-lua/
|
|
|
|
local M = {}
|
2021-12-11 03:44:15 +00:00
|
|
|
local util = require("go.utils")
|
|
|
|
local log = util.log
|
2021-07-16 14:37:41 +00:00
|
|
|
function M.make(...)
|
2021-12-10 00:43:59 +00:00
|
|
|
local args = { ... }
|
|
|
|
local lines = {}
|
2021-12-15 08:02:22 +00:00
|
|
|
local errorlines = {}
|
2021-04-19 06:10:06 +00:00
|
|
|
local winnr = vim.fn.win_getid()
|
|
|
|
local bufnr = vim.api.nvim_win_get_buf(winnr)
|
|
|
|
local makeprg = vim.api.nvim_buf_get_option(bufnr, "makeprg")
|
2021-12-10 00:43:59 +00:00
|
|
|
|
2021-12-16 06:46:52 +00:00
|
|
|
log(makeprg, args)
|
2021-12-10 00:43:59 +00:00
|
|
|
local indent = "%\\%( %\\)"
|
2021-07-16 14:37:41 +00:00
|
|
|
if not makeprg then
|
2021-12-15 08:02:22 +00:00
|
|
|
log("makeprog not setup")
|
2021-07-16 14:37:41 +00:00
|
|
|
return
|
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
|
2021-12-15 08:02:22 +00:00
|
|
|
local efm = [[%-G#\ %.%#]]
|
2021-12-16 06:46:52 +00:00
|
|
|
if makeprg:find("go build") then
|
2021-12-10 00:43:59 +00:00
|
|
|
vim.cmd([[setl errorformat=%-G#\ %.%#]])
|
2021-12-15 08:02:22 +00:00
|
|
|
-- if makeprg:find("go build") then
|
|
|
|
efm = efm .. [[,%-G%.%#panic:\ %m]]
|
|
|
|
efm = efm .. [[,%Ecan\'t\ load\ package:\ %m]]
|
|
|
|
efm = efm .. [[,%A%\\%%\(%[%^:]%\\+:\ %\\)%\\?%f:%l:%c:\ %m]]
|
|
|
|
efm = efm .. [[,%A%\\%%\(%[%^:]%\\+:\ %\\)%\\?%f:%l:\ %m]]
|
|
|
|
efm = efm .. [[,%C%*\\s%m]]
|
|
|
|
efm = efm .. [[,%-G%.%#]]
|
2021-07-16 14:37:41 +00:00
|
|
|
end
|
2021-12-15 08:02:22 +00:00
|
|
|
-- end
|
2021-12-10 00:43:59 +00:00
|
|
|
|
2022-01-06 23:29:40 +00:00
|
|
|
local runner = "golangci-lint"
|
2021-12-10 00:43:59 +00:00
|
|
|
if makeprg:find("golangci%-lint") then
|
2021-04-19 06:10:06 +00:00
|
|
|
-- lint
|
2021-12-15 08:02:22 +00:00
|
|
|
efm = efm .. [[,%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:%c:\ %m]]
|
|
|
|
efm = efm .. [[,%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:\ %m]]
|
2021-12-11 03:44:15 +00:00
|
|
|
|
2021-12-16 07:15:21 +00:00
|
|
|
local pwd = util.work_path()
|
2021-12-15 08:02:22 +00:00
|
|
|
local cfg = pwd .. ".golangci.yml"
|
2021-12-11 03:44:15 +00:00
|
|
|
|
|
|
|
if util.file_exists(cfg) then
|
|
|
|
makeprg = makeprg .. [[\ -c\ ]] .. cfg
|
2021-12-16 06:46:52 +00:00
|
|
|
-- vim.api.nvim_buf_set_option(bufnr, "makeprg", makeprg)
|
2021-12-11 03:44:15 +00:00
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
end
|
2021-12-15 08:02:22 +00:00
|
|
|
if makeprg:find("go run") then
|
2022-01-06 23:29:40 +00:00
|
|
|
runner = "go run"
|
2021-12-15 08:02:22 +00:00
|
|
|
if args == nil or #args == 0 then
|
|
|
|
makeprg = makeprg .. " ."
|
2021-12-16 06:46:52 +00:00
|
|
|
-- vim.api.nvim_buf_set_option(bufnr, "makeprg", makeprg)
|
2021-12-15 08:02:22 +00:00
|
|
|
end
|
|
|
|
efm = efm .. [[,%A%\\t%#%f:%l\ +0x%[0-9A-Fa-f]%\\+]]
|
2021-12-16 06:46:52 +00:00
|
|
|
|
|
|
|
log("go run", makeprg)
|
2021-12-15 08:02:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if makeprg:find("go vet") then
|
2022-01-06 23:29:40 +00:00
|
|
|
runner = "go vet"
|
2021-12-15 08:02:22 +00:00
|
|
|
if args == nil or #args == 0 then
|
|
|
|
makeprg = makeprg .. " ."
|
2021-12-16 06:46:52 +00:00
|
|
|
-- vim.api.nvim_buf_set_option(bufnr, "makeprg", makeprg)
|
2021-12-15 08:02:22 +00:00
|
|
|
end
|
|
|
|
efm = efm .. [[,%-Gexit\ status\ %\\d%\\+]]
|
2021-07-16 14:37:41 +00:00
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
|
2021-12-15 08:02:22 +00:00
|
|
|
if makeprg:find("test") then
|
|
|
|
log("go test")
|
2022-01-06 23:29:40 +00:00
|
|
|
|
|
|
|
runner = "go test"
|
|
|
|
|
2021-12-15 08:02:22 +00:00
|
|
|
-- I feel it is better to output everything
|
|
|
|
-- efm = efm .. [[,]] .. require("go.gotest").efm()
|
2021-12-10 10:13:23 +00:00
|
|
|
end
|
|
|
|
|
2021-12-16 06:46:52 +00:00
|
|
|
local cmd = vim.fn.split(makeprg, " ")
|
2021-07-16 14:37:41 +00:00
|
|
|
|
2021-12-16 06:46:52 +00:00
|
|
|
if args and #args > 0 then
|
|
|
|
cmd = vim.list_extend(cmd, args)
|
|
|
|
-- vim.api.nvim_buf_set_option(bufnr, "makeprg", makeprg)
|
2021-07-16 14:37:41 +00:00
|
|
|
end
|
2021-09-03 02:13:15 +00:00
|
|
|
|
2022-01-11 02:50:11 +00:00
|
|
|
local function handle_color(line)
|
|
|
|
if _GO_NVIM_CFG.run_in_floaterm then
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
if tonumber(vim.fn.match(line, "\\%x1b\\[[0-9;]\\+")) < 0 then
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
if type(line) ~= "string" then
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
line = vim.fn.substitute(line, "\\%x1b\\[[0-9;]\\+[mK]", "", "g")
|
|
|
|
log(line)
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
2021-04-19 06:10:06 +00:00
|
|
|
local function on_event(job_id, data, event)
|
2022-01-11 02:50:11 +00:00
|
|
|
-- log("stdout", data, event)
|
2021-12-15 08:02:22 +00:00
|
|
|
if event == "stdout" then
|
2021-04-19 06:10:06 +00:00
|
|
|
if data then
|
2021-12-15 08:02:22 +00:00
|
|
|
for _, value in ipairs(data) do
|
|
|
|
if value ~= "" then
|
2022-01-11 02:50:11 +00:00
|
|
|
log(value)
|
|
|
|
value = handle_color(value)
|
2021-12-15 08:02:22 +00:00
|
|
|
table.insert(lines, value)
|
|
|
|
end
|
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-15 08:02:22 +00:00
|
|
|
if event == "stderr" then
|
|
|
|
if data then
|
|
|
|
for _, value in ipairs(data) do
|
|
|
|
if value ~= "" then
|
|
|
|
table.insert(errorlines, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-06 23:29:40 +00:00
|
|
|
if next(errorlines) ~= nil and runner == "golangci-lint" then
|
2022-01-11 02:50:11 +00:00
|
|
|
efm =
|
|
|
|
[[level=%tarning\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%tarning\ msg="%m",level=%trror\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%trror\ msg="%m",%f:%l:%c:\ %m,%f:%l:\ %m,%f:%l\ %m]]
|
|
|
|
end
|
2021-12-15 08:02:22 +00:00
|
|
|
end
|
2021-12-10 10:13:23 +00:00
|
|
|
|
2021-12-15 08:02:22 +00:00
|
|
|
if event == "exit" then
|
|
|
|
if #errorlines > 0 then
|
|
|
|
if #lines > 0 then
|
|
|
|
vim.list_extend(errorlines, lines)
|
|
|
|
end
|
|
|
|
vim.fn.setqflist({}, " ", {
|
|
|
|
title = cmd,
|
|
|
|
lines = errorlines,
|
|
|
|
efm = efm,
|
|
|
|
})
|
|
|
|
elseif #lines > 0 then
|
|
|
|
vim.fn.setqflist({}, " ", {
|
|
|
|
title = cmd,
|
|
|
|
lines = lines,
|
|
|
|
})
|
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
vim.api.nvim_command("doautocmd QuickFixCmdPost")
|
2021-12-16 06:46:52 +00:00
|
|
|
|
|
|
|
vim.cmd("botright copen")
|
|
|
|
if type(cmd) == "table" then
|
|
|
|
cmd = table.concat(cmd, " ")
|
|
|
|
end
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify(cmd .. " finished", vim.lsp.log_levels.WARN)
|
2021-04-19 06:10:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-08 16:16:22 +00:00
|
|
|
local job_id = vim.fn.jobstart(cmd, {
|
|
|
|
on_stderr = on_event,
|
|
|
|
on_stdout = on_event,
|
|
|
|
on_exit = on_event,
|
|
|
|
stdout_buffered = true,
|
2021-12-10 00:43:59 +00:00
|
|
|
stderr_buffered = true,
|
2021-07-08 16:16:22 +00:00
|
|
|
})
|
2021-04-19 06:10:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|