Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lua/guard/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ local function watch_ft(fts)
group = group,
pattern = fts,
callback = function(args)
format.attach_to_buf(args.buf)
if
#api.nvim_get_autocmds({
group = group,
event = 'BufWritePre',
buffer = args.buf,
}) == 0
then
format.attach_to_buf(args.buf)
end
end,
desc = 'guard',
})
Expand Down
6 changes: 3 additions & 3 deletions lua/guard/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ local function attach_to_buf(buf)
api.nvim_create_autocmd('BufWritePre', {
group = require('guard.events').group,
buffer = buf,
callback = function(opt)
if not vim.bo[opt.buf].modified then
callback = function()
if not vim.bo[buf].modified then
return
end
require('guard.format').do_fmt(opt.buf)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't remove there. otherwise it's a closure.

Copy link
Copy Markdown
Member Author

@xiaoshihou514 xiaoshihou514 Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? 😕

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback it's a function you used buff number outside it then it's a closure when this callback run it will use this variable . buffer not same :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

require('guard.format').do_fmt(buf)
end,
})
end
Expand Down