Skip to content

Commit

Permalink
scaffold more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoshihou514 committed Dec 18, 2024
1 parent 5975c60 commit e3dc4b8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lua/guard/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function M.enable_lint(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local ft = require('guard.filetype')[vim.bo[buf].ft] or {}
if ft.linter and #ft.linter > 0 then
events.try_attach_lint_to_buf(buf, require('guard.util').linter_events(ft.linter[1]))
events.try_attach_lint_to_buf(buf, require('guard.util').linter_events(ft.linter[1]), ft)
end
end

Expand Down
50 changes: 41 additions & 9 deletions lua/guard/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local M = {}
M.group = api.nvim_create_augroup('Guard', { clear = true })

M.user_fmt_autocmds = {}
M.user_lint_autocmds = {}

local debounce_timer = nil
local function debounced_lint(opt)
Expand Down Expand Up @@ -92,9 +93,22 @@ function M.check_fmt_should_attach(buf)
end

---@param buf number
---@param ft string
---@return boolean
function M.check_lint_should_attach(buf)
return #M.get_lint_autocmds(buf) == 0 and vim.bo[buf].buftype ~= 'nofile'
function M.check_lint_should_attach(buf, ft)
if vim.bo[buf].buftype == 'nofile' then
return false
end

local aus = M.get_lint_autocmds(buf)

return #iter(aus)
:filter(ft == '*' and function(it)
return it.pattern == '*'
end or function(it)
return it.pattern ~= '*'
end)
:totable() == 0
end

---@param buf number
Expand All @@ -111,9 +125,9 @@ end

---@param buf number
---@param events string[]
---@param skip_check boolean
function M.try_attach_lint_to_buf(buf, events, skip_check)
if not skip_check and not M.check_lint_should_attach(buf) then
---@param ft string
function M.try_attach_lint_to_buf(buf, events, ft)
if not M.check_lint_should_attach(buf, ft) then
return
end

Expand All @@ -124,15 +138,15 @@ function M.try_attach_lint_to_buf(buf, events, skip_check)
pattern = 'GuardFmt',
callback = function(opt)
if opt.data.status == 'done' then
debounced_lint(opt)
lazy_debounced_lint(opt)
end
end,
})
else
au(ev, {
group = M.group,
buffer = buf,
callback = debounced_lint,
callback = lazy_debounced_lint,
})
end
end
Expand Down Expand Up @@ -222,14 +236,14 @@ function M.lint_watch_ft(ft, events)
pattern = ft,
group = M.group,
callback = function(args)
M.try_attach_lint_to_buf(args.buf, events, ft == '*')
M.try_attach_lint_to_buf(args.buf, events, ft)
end,
})
end

---@param events EventOption[]
---@param ft string
function M.attach_custom(ft, events)
function M.fmt_attach_custom(ft, events)
M.user_fmt_autocmds[ft] = {}
-- we don't know what autocmds are passed in, so these are attached asap
iter(events):each(function(event)
Expand All @@ -245,4 +259,22 @@ function M.attach_custom(ft, events)
end)
end

---@param events EventOption[]
---@param ft string
function M.lint_attach_custom(ft, events)
M.user_lint_autocmds[ft] = {}
-- we don't know what autocmds are passed in, so these are attached asap
iter(events):each(function(event)
table.insert(
M.user_fmt_autocmds[ft],
api.nvim_create_autocmd(
event.name,
maybe_fill_auoption(event.opt or {}, function(opt)
-- TODO
end)
)
)
end)
end

return M
20 changes: 17 additions & 3 deletions lua/guard/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ local function box(ft)

if type(config) == 'table' and config.events then
-- use user's custom events
events.attach_custom(it, config.events)
events.fmt_attach_custom(it, config.events)
else
events.fmt_watch_ft(it, self.formatter)
events.fmt_attach_to_existing(it)
Expand All @@ -100,7 +100,13 @@ local function box(ft)
M[it] = box(it)
M[it].linter = self.linter
end
events.lint_watch_ft(it, evs)

if type(config) == 'table' and config.events then
-- use user's custom events
events.lint_attach_custom(it, config.events)
else
events.lint_watch_ft(it, evs)
end
end
return self
end
Expand All @@ -109,7 +115,15 @@ local function box(ft)
if not check_type(config, { 'table', 'string', 'function' }) then
return
end
self[current][#self[current] + 1] = try_as(current, config)
local c = try_as(current, config)
self[current][#self[current] + 1] = c

if current == 'linter' and type(c) == 'table' and c.events then
for _, it in ipairs(self:ft()) do
require('guard.events').lint_attach_custom(it, config.events)
end
end

return self
end

Expand Down

0 comments on commit e3dc4b8

Please sign in to comment.