diff --git a/lua/go.lua b/lua/go.lua index eda58a2..5aeb042 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -45,12 +45,13 @@ function go.setup(cfg) vim.cmd([[command! Gofmt lua require("go.format").gofmt()]]) vim.cmd([[command! Goimport lua require("go.format").goimport()]]) - - vim.cmd([[command! GoBuild :setl makeprg=go\ build | :GoMake]]) - vim.cmd([[command! GoGenerate :setl makeprg=go\ generate | :GoMake]]) - vim.cmd([[command! GoRun :setl makeprg=go\ run | :GoMake]]) - - vim.cmd([[command! GoTest :setl makeprg=go\ test\ -v\ ./... | :GoMake]]) + vim.cmd([[command! GoGenerate :setl makeprg=go\ generate | :GoMake]]) + vim.cmd( + [[command! -nargs=* GoBuild :setl makeprg=go\ build | lua require'go.asyncmake'.make()]]) + vim.cmd( + [[command! -nargs=* GoRun :setl makeprg=go\ run | lua require'go.asyncmake'.make()]]) + vim.cmd( + [[command! -nargs=* GoTest :setl makeprg=go\ test\ | lua require'go.asyncmake'.make()]]) -- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]]) vim.cmd([[command! GoLint :setl makeprg=golangci-lint\ run\ --out-format\ tab | :GoMake]]) diff --git a/lua/go/asyncmake.lua b/lua/go/asyncmake.lua index f115256..54b34cc 100644 --- a/lua/go/asyncmake.lua +++ b/lua/go/asyncmake.lua @@ -1,31 +1,46 @@ -- https://phelipetls.github.io/posts/async-make-in-nvim-with-lua/ local M = {} -function M.make() +function M.make(...) + local args = {...} 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 = '%\\%( %\\)' + if not makeprg then + return + end vim.cmd([[setl errorformat =%-G#\ %.%#]]) - if makeprg == "go build" then + if makeprg:find("go build") then 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%.%#]]) - else + end + if makeprg:find('golangci-lint') then -- lint vim.cmd([[setl errorformat+=%-G#\ %.%#,%f:%l:%c:\\?\ %m]]) end - - if not makeprg then - return + 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 + local arg = ' ' + for _, v in pairs(args or {}) do + arg = arg .. ' ' .. v + end + + if #arg then + makeprg = makeprg .. arg + + vim.api.nvim_buf_set_option(bufnr, 'makeprg', makeprg) + end local cmd = vim.fn.expandcmd(makeprg) local function on_event(job_id, data, event)