Skip to content

Commit

Permalink
fix: get formatter custom events working
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoshihou514 committed Dec 13, 2024
1 parent e2604e7 commit 83d5d65
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
5 changes: 3 additions & 2 deletions lua/guard/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lua/guard/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions lua/guard/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 35 additions & 13 deletions spec/format_spec.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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]
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 83d5d65

Please sign in to comment.