diff --git a/lua/guard/events.lua b/lua/guard/events.lua index 6bd8118..c1f4fd1 100644 --- a/lua/guard/events.lua +++ b/lua/guard/events.lua @@ -132,11 +132,12 @@ function M.try_attach_lint_to_buf(buf, events) end ---@param ft string -function M.fmt_attach_to_existing(ft) +---@param events AutocmdConfig[]? +function M.fmt_attach_to_existing(ft, events) local bufs = api.nvim_list_bufs() for _, buf in ipairs(bufs) do if vim.bo[buf].ft == ft then - M.try_attach_fmt_to_buf(buf) + M.try_attach_fmt_to_buf(buf, events) end end end diff --git a/lua/guard/filetype.lua b/lua/guard/filetype.lua index 3afa2d8..e0de7b9 100644 --- a/lua/guard/filetype.lua +++ b/lua/guard/filetype.lua @@ -75,8 +75,11 @@ local function box(ft) M[it] = box(it) M[it].formatter = self.formatter end - events.fmt_watch_ft(it, M[it].events) - events.fmt_attach_to_existing(it) + -- BUG: maybe we want each tool to have its own events... + local first = M[it].formatter[1] + local aus = type(first) == 'table' and first.autocmds or nil + events.fmt_watch_ft(it, aus) + events.fmt_attach_to_existing(it, aus) end return self end diff --git a/lua/guard/util.lua b/lua/guard/util.lua index 58588da..f474d32 100644 --- a/lua/guard/util.lua +++ b/lua/guard/util.lua @@ -232,16 +232,20 @@ end ---@param cb function ---@return AutocmdOpt function M.au_option_copy(opt, group, cb) - if not opt or vim.tbl_isempty(opt) then - return opt + local t + if not opt or type(opt) ~= 'table' or vim.tbl_isempty(opt) then + t = {} + else + t = opt end return { group = group, - callback = opt.callback or cb, - buffer = opt.buffer, - nested = opt.nested, - once = opt.once, - pattern = opt.pattern, + callback = (not t.callback and not t.command) and cb or t.callback, + command = t.command, + buffer = t.buffer, + nested = t.nested, + once = t.once, + pattern = t.pattern, } end diff --git a/spec/format_spec.lua b/spec/format_spec.lua index 1211a2f..97ab3af 100644 --- a/spec/format_spec.lua +++ b/spec/format_spec.lua @@ -1,11 +1,16 @@ ---@diagnostic disable: undefined-field, undefined-global local api = vim.api -local equal = assert.equal +local equal, same = assert.equal, assert.are.same local ft = require('guard.filetype') local gapi = require('guard.api') +local group = require('guard.events').group describe('format module', function() local bufnr + local ill_lua = { + 'local a', + ' = "test"', + } before_each(function() for k, _ in pairs(ft) do ft[k] = nil @@ -15,6 +20,9 @@ describe('format module', function() vim.bo[bufnr].filetype = 'lua' api.nvim_set_current_buf(bufnr) vim.cmd('silent! write! /tmp/fmt_spec_test.lua') + vim.iter(api.nvim_get_autocmds({ group = group })):each(function(it) + api.nvim_del_autocmd(it.id) + end) end) it('can format with single formatter', function() @@ -23,10 +31,7 @@ describe('format module', function() args = { '-' }, stdin = true, }) - api.nvim_buf_set_lines(bufnr, 0, -1, false, { - 'local a', - ' = "test"', - }) + api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua) gapi.fmt() vim.wait(500) local line = api.nvim_buf_get_lines(bufnr, 0, -1, false)[1] @@ -43,10 +48,7 @@ describe('format module', function() args = { '-s', ' ' }, stdin = true, }) - api.nvim_buf_set_lines(bufnr, 0, -1, false, { - 'local a', - ' = "test"', - }) + api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua) gapi.fmt() vim.wait(500) local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false) @@ -59,10 +61,7 @@ describe('format module', function() return table.concat(vim.split(acc, '\n'), '') .. vim.inspect(range) end, }) - api.nvim_buf_set_lines(bufnr, 0, -1, false, { - 'local a', - ' = "test"', - }) + api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua) gapi.fmt() vim.wait(500) local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false) @@ -109,4 +108,27 @@ describe('format module', function() lines = api.nvim_buf_get_lines(bufnr, 0, -1, false) assert.are.same({ 'def' }, lines) end) + + it('can format on custom events', function() + ft('lua'):fmt({ + cmd = 'stylua', + args = { '-' }, + stdin = true, + autocmds = { + { event = 'ColorScheme', opt = {} }, + }, + }) + + api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua) + + vim.cmd('silent! write! /tmp/fmt_spec_test.lua') + vim.wait(500) + + same(ill_lua, api.nvim_buf_get_lines(bufnr, 0, -1, false)) + + vim.cmd.colorscheme('blue') + vim.wait(500) + + equal([[local a = 'test']], api.nvim_buf_get_lines(bufnr, 0, -1, false)[1]) + end) end)