From 3f4d2dcbb7b15227066e7e6b782cf22443fb8a0d Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 28 Oct 2023 06:00:05 +0200 Subject: [PATCH] Run tests and add diag on startup (when opening a folder with go.mod) --- lua/myworkflow/autocommands.lua | 19 ++++++++++++++++++- lua/myworkflow/go-tests.lua | 25 +++++++++++++++++++------ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lua/myworkflow/autocommands.lua b/lua/myworkflow/autocommands.lua index 91015ec..9b53590 100644 --- a/lua/myworkflow/autocommands.lua +++ b/lua/myworkflow/autocommands.lua @@ -15,7 +15,24 @@ vim.api.nvim_create_autocmd('BufWritePost', pattern = { "*.go" }, callback = function() gotests.goRunTests() - gotests.goSetMarks() + gotests.goSetAllMarks() end } ) + +vim.api.nvim_create_autocmd('VimEnter', { + pattern = { "*" }, + group = atgroup, + nested = true, + callback = function() + -- we have to defer the autocommand, because otherwise treesitter + -- will not highlight syntax (all code is plain white) + vim.defer_fn(function() + local gomod_exists = vim.fn.filereadable("go.mod") + if gomod_exists ~= 0 then + gotests.goRunTests() + gotests.goSetAllMarks() + end + end, 500) + end +}) diff --git a/lua/myworkflow/go-tests.lua b/lua/myworkflow/go-tests.lua index 84de63a..fd513de 100644 --- a/lua/myworkflow/go-tests.lua +++ b/lua/myworkflow/go-tests.lua @@ -49,11 +49,11 @@ vim.api.nvim_create_user_command('GoTest', function() end, {}) vim.api.nvim_create_user_command('GoPrintResults', function() - M.goPrintResults() + print(vim.inspect(M.results)) end, {}) vim.api.nvim_create_user_command('GoSetTestMarks', function() - M.goSetMarks() + M.goSetAllMarks() end, {}) vim.api.nvim_create_user_command('GoClearResults', function() @@ -97,13 +97,26 @@ function M.goRunTests() vim.fn.jobwait({ id }) end -function M.goPrintResults() - print(vim.inspect(M.results)) +function M.goSetAllMarks() + local files = vim.fn.system({ "find", "-type", "f", "-name", "*.go" }) + + local sep = "\n" + local filetable = {} + for str in string.gmatch(files, "([^" .. sep .. "]+)") do + table.insert(filetable, str) + end + + for _, file in ipairs(filetable) do + vim.fn.bufadd(file) + vim.fn.bufload(file) + local bufno = vim.fn.bufnr(file) + M.goSetMarks(bufno) + end end -function M.goSetMarks() - local currbuf = vim.api.nvim_get_current_buf() +function M.goSetMarks(currbuf) vim.api.nvim_buf_clear_namespace(currbuf, ns, 0, -1) + vim.diagnostic.set(ns, currbuf, {}, {}) local text = {} local failed = {}