~gpanders/dotfiles

89d168bfce4ac7a94f4e0ed38405d86c79df293e — Gregory Anders a month ago 7042f5e
nvim: rewrite async grep using libuv

use libuv directly rather than going through the jobstart() API which
ends up going through libuv anyway.
1 files changed, 31 insertions(+), 25 deletions(-)

M .config/nvim/lua/grep.lua
M .config/nvim/lua/grep.lua => .config/nvim/lua/grep.lua +31 -25
@@ 1,35 1,41 @@
return function(l, args, mods)
    local eargs = vim.fn.expandcmd(args)
return function(l, cmdargs, mods)
    local expanded = vim.fn.expandcmd(cmdargs)
    local grepcmd
    if string.match(vim.o.grepprg, "%$%*") then
        grepcmd = string.gsub(vim.o.grepprg, "%$%*", eargs)
        grepcmd = string.gsub(vim.o.grepprg, "%$%*", expanded)
    else
        grepcmd = vim.o.grepprg .. " " .. eargs
        grepcmd = vim.o.grepprg .. " " .. expanded
    end

    local opts = {
        stdout_buffered = true,
        stdin = "null",
        on_stdout = function(_, data)
            if data[#data] == "" then
                table.remove(data)
            end
    local args = vim.split(grepcmd, " ")
    local cmd = table.remove(args, 1)

            local what = { title = grepcmd, efm = vim.o.grepformat, nr = "$", lines = data }
            if l then
                vim.fn.setloclist(0, {}, " ", what)
            else
                vim.fn.setqflist({}, " ", what)
            end
    local stdout = vim.loop.new_pipe(false)
    local chunks = {}

            vim.cmd(mods .. " " .. (l and "lopen" or "copen"))
        end,
        on_exit = function()
            vim.cmd("doautocmd QuickFixCmdPost " .. (l and "lgrep" or "grep"))
        end,
    }
    vim.api.nvim_command("doautocmd QuickFixCmdPre " .. (l and "lgrep" or "grep"))

    vim.loop.spawn(cmd, {
        args = args,
        stdio = { nil, stdout, nil }
    }, vim.schedule_wrap(function()
        local lines = vim.split(vim.trim(table.concat(chunks)), "\n")
        local what = { title = grepcmd, efm = vim.o.grepformat, nr = "$", lines = lines }
        if l then
            vim.fn.setloclist(0, {}, " ", what)
        else
            vim.fn.setqflist({}, " ", what)
        end
        vim.api.nvim_command(mods .. " " .. (l and "lopen" or "copen"))
        vim.api.nvim_command("doautocmd QuickFixCmdPost " .. (l and "lgrep" or "grep"))
    end))

    vim.loop.read_start(stdout, function(err, data)
        assert(not err, err)
        if data then
            table.insert(chunks, data)
        end
    end)

    vim.cmd("doautocmd QuickFixCmdPre " .. (l and "lgrep" or "grep"))
    vim.fn.jobstart(grepcmd, opts)
    print(grepcmd)
end