Skip to content

Commit

Permalink
feat(config): use lazy's new custom events (Event.mappings) for a b…
Browse files Browse the repository at this point in the history
…etter `LazyFile`
  • Loading branch information
folke committed Oct 12, 2023
1 parent 8373467 commit 4ea1c68
Showing 1 changed file with 10 additions and 35 deletions.
45 changes: 10 additions & 35 deletions lua/lazyvim/util/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ M.renames = {
-- Properly load file based plugins without blocking the UI
function M.lazy_file()
M.use_lazy_file = M.use_lazy_file and vim.fn.argc(-1) > 0
---@diagnostic disable-next-line: undefined-field
M.use_lazy_file = M.use_lazy_file and require("lazy.core.handler.event").trigger_events == nil

M.fix_lazy_file()
-- Add support for the LazyFile event
local Event = require("lazy.core.handler.event")

if not M.use_lazy_file then
if M.use_lazy_file then
-- We'll handle delayed execution of events ourselves
Event.mappings.LazyFile = { id = "LazyFile", event = "User", pattern = "LazyFile" }
Event.mappings["User LazyFile"] = Event.mappings.LazyFile
else
-- Don't delay execution of LazyFile events, but let lazy know about the mapping
Event.mappings.LazyFile = { id = "LazyFile", event = { "BufReadPost", "BufNewFile", "BufWritePre" } }
Event.mappings["User LazyFile"] = Event.mappings.LazyFile
return
end

Expand All @@ -39,11 +45,8 @@ function M.lazy_file()
if #events == 0 then
return
end
local Event = require("lazy.core.handler.event")
vim.api.nvim_del_augroup_by_name("lazy_file")

Util.track({ event = "LazyVim.lazy_file" })

---@type table<string,string[]>
local skips = {}
for _, event in ipairs(events) do
Expand All @@ -67,7 +70,6 @@ function M.lazy_file()
end
vim.api.nvim_exec_autocmds("CursorMoved", { modeline = false })
events = {}
Util.track()
end

-- schedule wrap so that nested autocmds are executed
Expand Down Expand Up @@ -118,31 +120,4 @@ function M.fix_renames()
end
end

function M.fix_lazy_file()
local Config = require("lazyvim.config")
-- Add support for the LazyFile event
local Handler = require("lazy.core.handler")
local Event = require("lazy.core.handler.event")
if M.use_lazy_file then
local _event = Event._event
---@diagnostic disable-next-line: duplicate-set-field
Event._event = function(self, value)
return value == "LazyFile" and "User LazyFile" or _event(self, value)
end
else
---@param plugin LazyPlugin
function Event:values(plugin)
local ret = Handler.values(self, plugin)
if ret["LazyFile"] or ret["User LazyFile"] then
for _, event in ipairs(M.lazy_file_events) do
ret[event] = event
end
ret["LazyFile"] = nil
ret["User LazyFile"] = nil
end
return ret
end
end
end

return M

0 comments on commit 4ea1c68

Please sign in to comment.