From abd282564a31c5dec14e2038ebf04fdac9ea3278 Mon Sep 17 00:00:00 2001 From: ray-x Date: Wed, 27 Mar 2024 13:47:11 +1100 Subject: [PATCH] improve null_ls loading events and allow golangci_linter linters to be configurable --- lua/go/commands.lua | 3 --- lua/go/gotest.lua | 12 ++++++++---- lua/go/null_ls.lua | 20 ++++++++++++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lua/go/commands.lua b/lua/go/commands.lua index b93b4f7..253db67 100644 --- a/lua/go/commands.lua +++ b/lua/go/commands.lua @@ -226,9 +226,6 @@ return { nargs = '*', }) - -- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]]) - --print-issued-lines=false - vim.cmd( [[command! GoLint :setl makeprg=golangci-lint\ run\ --print-issued-lines=false\ --exclude-use-default=false | :GoMake]] ) diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index 681aa34..761a382 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -125,13 +125,13 @@ local function get_test_filebufnr() if not fn:find('test%.go$') then fn = require('go.alternate').alternate() fn = vfn.fnamemodify(fn, ':p') -- expand to full path + -- check if file exists + if vfn.filereadable(fn) == 0 then + return 0, 'no test file' + end local uri = vim.uri_from_fname(fn) bufnr = vim.uri_to_bufnr(uri) log(fn, bufnr, uri) - if vfn.filereadable(vim.uri_to_fname(uri)) == 0 then - -- no test file existed - return 0, 'no test file' - end if not vim.api.nvim_buf_is_loaded(bufnr) then vfn.bufload(bufnr) end @@ -547,6 +547,10 @@ M.get_test_cases = function() fpath = '.' .. sep .. vfn.fnamemodify(vfn.expand('%:p'), ':.:r') .. '_test.go' end -- utils.log(args) + -- check if test file exists + if vfn.filereadable(fpath) == 0 then + return + end local tests = M.get_testfunc() if vim.fn.empty(tests) == 1 then -- TODO maybe with treesitter or lsp list all functions in current file and regex with Test diff --git a/lua/go/null_ls.lua b/lua/go/null_ls.lua index 5797cb0..c547aad 100644 --- a/lua/go/null_ls.lua +++ b/lua/go/null_ls.lua @@ -166,6 +166,7 @@ local h = require('null-ls.helpers') local methods = require('null-ls.methods') local DIAGNOSTICS_ON_SAVE = methods.internal.DIAGNOSTICS_ON_SAVE +local DIAGNOSTICS_ON_OPEN = methods.internal.DIAGNOSTICS_ON_OPEN -- register with -- null_ls.register(gotest) @@ -179,7 +180,7 @@ return { url = 'https://golangci-lint.run/', description = 'A Go linter aggregator.', }, - method = DIAGNOSTICS_ON_SAVE, + method = { DIAGNOSTICS_ON_OPEN, DIAGNOSTICS_ON_SAVE }, filetypes = { 'go' }, generator_opts = { command = 'golangci-lint', @@ -192,14 +193,19 @@ return { local rfname = vfn.fnamemodify(vfn.expand('%'), ':~:.') trace(rfname) -- repplace $FILENAME ? golangci_diags = {} -- CHECK: is here the best place to call - return { + local args = { 'run', '--fix=false', '--out-format=json', - '--path-prefix', - '$ROOT', - '$FILENAME', } + for _, linter in ipairs(_GO_NVIM_CFG.null_ls.golangci_lint.disable) do + table.insert(args, '--disable=' .. linter) + end + for _, linter in ipairs(_GO_NVIM_CFG.null_ls.golangci_lint.enable) do + table.insert(args, '--enable=' .. linter) + end + args = vim.list_extend(args, { '--path-prefix', '$ROOT', '$FILENAME' }) + return args end, check_exit_code = function(code) if code > 2 then @@ -211,6 +217,7 @@ return { return true end, on_output = function(msg, done) + trace('golangci-lint finished with code', done, msg) if msg == nil then return {} end @@ -234,6 +241,7 @@ return { for _, m in pairs(msgs) do if vfn.empty(m) == 0 then + trace('lint message: ', m) local entry = vfn.json_decode(m) if entry['Report'] and entry['Report']['Error'] then trace('report', entry['Report']['Error']) @@ -278,7 +286,7 @@ return { local cmd = {} return h.make_builtin({ name = 'gotest', - method = null_ls.methods.DIAGNOSTICS_ON_SAVE, + method = { DIAGNOSTICS_ON_OPEN, DIAGNOSTICS_ON_SAVE }, filetypes = { 'go' }, generator_opts = { command = 'go',