Skip to content

Commit

Permalink
fix: attaching to nofile buffers (#175)
Browse files Browse the repository at this point in the history
* chore(doc): auto generate docs

* fix: guard attaching to nofile buffers

* chore: update comment

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
xiaoshihou514 and github-actions[bot] authored Dec 7, 2024
1 parent 48f2f75 commit 246b7dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion doc/guard.nvim.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 December 03
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 December 07

==============================================================================
Table of Contents *guard.nvim-table-of-contents*
Expand Down
9 changes: 1 addition & 8 deletions lua/guard/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ local M = {}
M.group = api.nvim_create_augroup('Guard', { clear = true })

function M.try_attach_to_buf(buf)
if
#api.nvim_get_autocmds({
group = M.group,
event = 'BufWritePre',
buffer = buf,
}) > 0
then
-- already attached
if not util.check_should_attach(buf) then
return
end
au('BufWritePre', {
Expand Down
14 changes: 14 additions & 0 deletions lua/guard/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ end
--- @param filename string
--- @return string|false
local function exists(filename)
---@diagnostic disable-next-line: undefined-field
local stat = vim.uv.fs_stat(filename)
return stat and stat.type or false
end
Expand Down Expand Up @@ -132,6 +133,7 @@ end
---@return string, string?
function M.buf_get_info(buf)
local fname = vim.fn.fnameescape(api.nvim_buf_get_name(buf))
---@diagnostic disable-next-line: undefined-field
return fname, M.get_lsp_root() or vim.uv.cwd()
end

Expand Down Expand Up @@ -215,4 +217,16 @@ function M.eval(xs)
end, xs)
end

---@param buf number
---@return boolean
function M.check_should_attach(buf)
local bo = vim.bo[buf]
-- check if it's not attached already and has an underlying file
return #api.nvim_get_autocmds({
group = M.group,
event = 'BufWritePre',
buffer = buf,
}) == 0 and bo.buftype ~= 'nofile'
end

return M

0 comments on commit 246b7dc

Please sign in to comment.