Skip to content

Instantly share code, notes, and snippets.

@phelipetls
Last active February 8, 2024 23:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save phelipetls/639a1b5f021d17c4124cccc83e518566 to your computer and use it in GitHub Desktop.
Save phelipetls/639a1b5f021d17c4124cccc83e518566 to your computer and use it in GitHub Desktop.
Run :make asynchronously in Neovim
local M = {}
function M.make()
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")
if not makeprg then return end
local cmd = vim.fn.expandcmd(makeprg)
local function on_event(job_id, data, event)
if event == "stdout" or event == "stderr" then
if data then
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
end
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,
}
)
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment