Skip to content

Commit 83d5d65

Browse files
committed
fix: get formatter custom events working
1 parent e2604e7 commit 83d5d65

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

lua/guard/events.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@ function M.try_attach_lint_to_buf(buf, events)
132132
end
133133

134134
---@param ft string
135-
function M.fmt_attach_to_existing(ft)
135+
---@param events AutocmdConfig[]?
136+
function M.fmt_attach_to_existing(ft, events)
136137
local bufs = api.nvim_list_bufs()
137138
for _, buf in ipairs(bufs) do
138139
if vim.bo[buf].ft == ft then
139-
M.try_attach_fmt_to_buf(buf)
140+
M.try_attach_fmt_to_buf(buf, events)
140141
end
141142
end
142143
end

lua/guard/filetype.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ local function box(ft)
7575
M[it] = box(it)
7676
M[it].formatter = self.formatter
7777
end
78-
events.fmt_watch_ft(it, M[it].events)
79-
events.fmt_attach_to_existing(it)
78+
-- BUG: maybe we want each tool to have its own events...
79+
local first = M[it].formatter[1]
80+
local aus = type(first) == 'table' and first.autocmds or nil
81+
events.fmt_watch_ft(it, aus)
82+
events.fmt_attach_to_existing(it, aus)
8083
end
8184
return self
8285
end

lua/guard/util.lua

+11-7
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,20 @@ end
232232
---@param cb function
233233
---@return AutocmdOpt
234234
function M.au_option_copy(opt, group, cb)
235-
if not opt or vim.tbl_isempty(opt) then
236-
return opt
235+
local t
236+
if not opt or type(opt) ~= 'table' or vim.tbl_isempty(opt) then
237+
t = {}
238+
else
239+
t = opt
237240
end
238241
return {
239242
group = group,
240-
callback = opt.callback or cb,
241-
buffer = opt.buffer,
242-
nested = opt.nested,
243-
once = opt.once,
244-
pattern = opt.pattern,
243+
callback = (not t.callback and not t.command) and cb or t.callback,
244+
command = t.command,
245+
buffer = t.buffer,
246+
nested = t.nested,
247+
once = t.once,
248+
pattern = t.pattern,
245249
}
246250
end
247251

spec/format_spec.lua

+35-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
---@diagnostic disable: undefined-field, undefined-global
22
local api = vim.api
3-
local equal = assert.equal
3+
local equal, same = assert.equal, assert.are.same
44
local ft = require('guard.filetype')
55
local gapi = require('guard.api')
6+
local group = require('guard.events').group
67

78
describe('format module', function()
89
local bufnr
10+
local ill_lua = {
11+
'local a',
12+
' = "test"',
13+
}
914
before_each(function()
1015
for k, _ in pairs(ft) do
1116
ft[k] = nil
@@ -15,6 +20,9 @@ describe('format module', function()
1520
vim.bo[bufnr].filetype = 'lua'
1621
api.nvim_set_current_buf(bufnr)
1722
vim.cmd('silent! write! /tmp/fmt_spec_test.lua')
23+
vim.iter(api.nvim_get_autocmds({ group = group })):each(function(it)
24+
api.nvim_del_autocmd(it.id)
25+
end)
1826
end)
1927

2028
it('can format with single formatter', function()
@@ -23,10 +31,7 @@ describe('format module', function()
2331
args = { '-' },
2432
stdin = true,
2533
})
26-
api.nvim_buf_set_lines(bufnr, 0, -1, false, {
27-
'local a',
28-
' = "test"',
29-
})
34+
api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua)
3035
gapi.fmt()
3136
vim.wait(500)
3237
local line = api.nvim_buf_get_lines(bufnr, 0, -1, false)[1]
@@ -43,10 +48,7 @@ describe('format module', function()
4348
args = { '-s', ' ' },
4449
stdin = true,
4550
})
46-
api.nvim_buf_set_lines(bufnr, 0, -1, false, {
47-
'local a',
48-
' = "test"',
49-
})
51+
api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua)
5052
gapi.fmt()
5153
vim.wait(500)
5254
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
@@ -59,10 +61,7 @@ describe('format module', function()
5961
return table.concat(vim.split(acc, '\n'), '') .. vim.inspect(range)
6062
end,
6163
})
62-
api.nvim_buf_set_lines(bufnr, 0, -1, false, {
63-
'local a',
64-
' = "test"',
65-
})
64+
api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua)
6665
gapi.fmt()
6766
vim.wait(500)
6867
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
@@ -109,4 +108,27 @@ describe('format module', function()
109108
lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
110109
assert.are.same({ 'def' }, lines)
111110
end)
111+
112+
it('can format on custom events', function()
113+
ft('lua'):fmt({
114+
cmd = 'stylua',
115+
args = { '-' },
116+
stdin = true,
117+
autocmds = {
118+
{ event = 'ColorScheme', opt = {} },
119+
},
120+
})
121+
122+
api.nvim_buf_set_lines(bufnr, 0, -1, false, ill_lua)
123+
124+
vim.cmd('silent! write! /tmp/fmt_spec_test.lua')
125+
vim.wait(500)
126+
127+
same(ill_lua, api.nvim_buf_get_lines(bufnr, 0, -1, false))
128+
129+
vim.cmd.colorscheme('blue')
130+
vim.wait(500)
131+
132+
equal([[local a = 'test']], api.nvim_buf_get_lines(bufnr, 0, -1, false)[1])
133+
end)
112134
end)

0 commit comments

Comments
 (0)