2021-04-19 06:10:06 +00:00
|
|
|
-- https://phelipetls.github.io/posts/async-make-in-nvim-with-lua/
|
|
|
|
local M = {}
|
2021-09-03 02:13:15 +00:00
|
|
|
local log = require("go.utils").log
|
2021-07-16 14:37:41 +00:00
|
|
|
function M.make(...)
|
|
|
|
local args = {...}
|
2021-04-19 06:10:06 +00:00
|
|
|
local lines = {""}
|
|
|
|
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")
|
|
|
|
local indent = '%\\%( %\\)'
|
2021-07-16 14:37:41 +00:00
|
|
|
if not makeprg then
|
|
|
|
return
|
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
|
|
|
|
vim.cmd([[setl errorformat =%-G#\ %.%#]])
|
2021-07-16 14:37:41 +00:00
|
|
|
if makeprg:find("go build") then
|
2021-04-19 06:10:06 +00:00
|
|
|
vim.cmd([[setl errorformat+=%-G%.%#panic:\ %m]])
|
|
|
|
vim.cmd([[setl errorformat+=%Ecan\'t\ load\ package:\ %m]])
|
|
|
|
vim.cmd([[setl errorformat+=%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:%c:\ %m]])
|
|
|
|
vim.cmd([[setl errorformat+=%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:\ %m]])
|
|
|
|
vim.cmd([[setl errorformat+=%C%*\\s%m]])
|
|
|
|
vim.cmd([[setl errorformat+=%-G%.%#]])
|
2021-07-16 14:37:41 +00:00
|
|
|
end
|
|
|
|
if makeprg:find('golangci-lint') then
|
2021-04-19 06:10:06 +00:00
|
|
|
-- lint
|
|
|
|
vim.cmd([[setl errorformat+=%-G#\ %.%#,%f:%l:%c:\\?\ %m]])
|
|
|
|
end
|
2021-07-16 14:37:41 +00:00
|
|
|
if makeprg == 'go run' and #args == 0 then
|
|
|
|
vim.cmd([[setl makeprg=go\ run\ \.]])
|
|
|
|
makeprg = makeprg .. ' .'
|
|
|
|
vim.api.nvim_buf_set_option(bufnr, 'makeprg', makeprg)
|
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
|
2021-07-16 14:37:41 +00:00
|
|
|
local arg = ' '
|
|
|
|
for _, v in pairs(args or {}) do
|
|
|
|
arg = arg .. ' ' .. v
|
2021-07-08 16:16:22 +00:00
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
|
2021-11-13 03:29:42 +00:00
|
|
|
log(makeprg, args)
|
2021-07-16 14:37:41 +00:00
|
|
|
if #arg then
|
|
|
|
makeprg = makeprg .. arg
|
|
|
|
|
|
|
|
vim.api.nvim_buf_set_option(bufnr, 'makeprg', makeprg)
|
|
|
|
end
|
2021-09-03 02:13:15 +00:00
|
|
|
|
|
|
|
-- vim.cmd([[make %:r]])
|
2021-04-19 06:10:06 +00:00
|
|
|
local cmd = vim.fn.expandcmd(makeprg)
|
|
|
|
|
|
|
|
local function on_event(job_id, data, event)
|
|
|
|
if event == "stdout" or event == "stderr" then
|
|
|
|
if data then
|
2021-09-03 02:13:15 +00:00
|
|
|
-- log(data)
|
2021-04-19 06:10:06 +00:00
|
|
|
vim.list_extend(lines, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if event == "exit" then
|
|
|
|
vim.fn.setqflist({}, " ", {
|
|
|
|
title = cmd,
|
|
|
|
lines = lines,
|
|
|
|
efm = vim.api.nvim_buf_get_option(bufnr, "errorformat")
|
|
|
|
})
|
|
|
|
vim.api.nvim_command("doautocmd QuickFixCmdPost")
|
|
|
|
end
|
2021-11-13 03:29:42 +00:00
|
|
|
if lines and #lines > 1 then
|
2021-07-08 16:16:22 +00:00
|
|
|
vim.cmd("copen")
|
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
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,
|
|
|
|
stderr_buffered = true
|
|
|
|
})
|
2021-04-19 06:10:06 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|