Skip to content

Commit 246b7dc

Browse files
fix: attaching to nofile buffers (#175)
* 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>
1 parent 48f2f75 commit 246b7dc

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

doc/guard.nvim.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 December 03
1+
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 December 07
22

33
==============================================================================
44
Table of Contents *guard.nvim-table-of-contents*

lua/guard/events.lua

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@ local M = {}
88
M.group = api.nvim_create_augroup('Guard', { clear = true })
99

1010
function M.try_attach_to_buf(buf)
11-
if
12-
#api.nvim_get_autocmds({
13-
group = M.group,
14-
event = 'BufWritePre',
15-
buffer = buf,
16-
}) > 0
17-
then
18-
-- already attached
11+
if not util.check_should_attach(buf) then
1912
return
2013
end
2114
au('BufWritePre', {

lua/guard/util.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ end
8383
--- @param filename string
8484
--- @return string|false
8585
local function exists(filename)
86+
---@diagnostic disable-next-line: undefined-field
8687
local stat = vim.uv.fs_stat(filename)
8788
return stat and stat.type or false
8889
end
@@ -132,6 +133,7 @@ end
132133
---@return string, string?
133134
function M.buf_get_info(buf)
134135
local fname = vim.fn.fnameescape(api.nvim_buf_get_name(buf))
136+
---@diagnostic disable-next-line: undefined-field
135137
return fname, M.get_lsp_root() or vim.uv.cwd()
136138
end
137139

@@ -215,4 +217,16 @@ function M.eval(xs)
215217
end, xs)
216218
end
217219

220+
---@param buf number
221+
---@return boolean
222+
function M.check_should_attach(buf)
223+
local bo = vim.bo[buf]
224+
-- check if it's not attached already and has an underlying file
225+
return #api.nvim_get_autocmds({
226+
group = M.group,
227+
event = 'BufWritePre',
228+
buffer = buf,
229+
}) == 0 and bo.buftype ~= 'nofile'
230+
end
231+
218232
return M

0 commit comments

Comments
 (0)