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(...)
|
2021-12-10 00:43:59 +00:00
|
|
|
local args = { ... }
|
|
|
|
local lines = {}
|
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
|
|
|
|
|
|
|
local indent = "%\\%( %\\)"
|
2021-07-16 14:37:41 +00:00
|
|
|
if not makeprg then
|
2021-12-10 10:13:23 +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-07-16 14:37:41 +00:00
|
|
|
if makeprg:find("go build") then
|
2021-12-10 00:43:59 +00:00
|
|
|
vim.cmd([[setl errorformat=%-G#\ %.%#]])
|
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
|
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-10 00:43:59 +00:00
|
|
|
vim.cmd([[setl errorformat=%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:%c:\ %m]])
|
|
|
|
vim.cmd([[setl errorformat+=%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:\ %m]])
|
2021-04-19 06:10:06 +00:00
|
|
|
end
|
2021-12-10 00:43:59 +00:00
|
|
|
if makeprg == "go run" and #args == 0 then
|
2021-07-16 14:37:41 +00:00
|
|
|
vim.cmd([[setl makeprg=go\ run\ \.]])
|
2021-12-10 00:43:59 +00:00
|
|
|
makeprg = makeprg .. " ."
|
|
|
|
vim.api.nvim_buf_set_option(bufnr, "makeprg", makeprg)
|
2021-07-16 14:37:41 +00:00
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
|
2021-12-10 10:13:23 +00:00
|
|
|
if makeprg:find('test') then
|
|
|
|
log('go test')
|
|
|
|
end
|
|
|
|
|
2021-12-10 00:43:59 +00:00
|
|
|
local arg = " "
|
2021-07-16 14:37:41 +00:00
|
|
|
for _, v in pairs(args or {}) do
|
2021-12-10 00:43:59 +00:00
|
|
|
arg = arg .. " " .. v
|
2021-07-08 16:16:22 +00:00
|
|
|
end
|
2021-04-19 06:10:06 +00:00
|
|
|
|
2021-07-16 14:37:41 +00:00
|
|
|
if #arg then
|
|
|
|
makeprg = makeprg .. arg
|
|
|
|
|
2021-12-10 00:43:59 +00:00
|
|
|
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
|
|
|
|
|
|
|
-- vim.cmd([[make %:r]])
|
2021-04-19 06:10:06 +00:00
|
|
|
local cmd = vim.fn.expandcmd(makeprg)
|
|
|
|
|
2021-12-10 10:13:23 +00:00
|
|
|
log(cmd)
|
2021-04-19 06:10:06 +00:00
|
|
|
local function on_event(job_id, data, event)
|
|
|
|
if event == "stdout" or event == "stderr" then
|
|
|
|
if data then
|
2021-12-10 10:13:23 +00:00
|
|
|
log('stdout', data)
|
2021-04-19 06:10:06 +00:00
|
|
|
vim.list_extend(lines, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if event == "exit" then
|
2021-12-10 10:13:23 +00:00
|
|
|
|
|
|
|
log(data)
|
2021-04-19 06:10:06 +00:00
|
|
|
vim.fn.setqflist({}, " ", {
|
|
|
|
title = cmd,
|
|
|
|
lines = lines,
|
2021-12-10 00:43:59 +00:00
|
|
|
efm = vim.api.nvim_buf_get_option(bufnr, "errorformat"),
|
2021-04-19 06:10:06 +00:00
|
|
|
})
|
|
|
|
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,
|
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
|