feat(nvim): Port LazyFile

REF: https://github.com/folke/lazy.nvim/issues/1182
This commit is contained in:
Ahmad Ansori Palembani 2024-04-18 12:49:06 +07:00
parent e217bfa86c
commit d909aa1a51
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 74 additions and 0 deletions

View file

@ -1,6 +1,10 @@
require("null.util").lazy_file()
return {
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
event = { "LazyFile", "VeryLazy" },
opts = {
ensure_installed = {
"c",

View file

@ -30,4 +30,74 @@ function M.map(modes, key, target, opts)
})
end
-- << REF: https://github.com/LazyVim/LazyVim/blob/7a5dbea/lua/lazyvim/util/plugin.lua#L59-L124
-- 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
-- Add support for the LazyFile event
local Event = require("lazy.core.handler.event")
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
local events = {} ---@type {event: string, buf: number, data?: any}[]
local done = false
local function load()
if #events == 0 or done then
return
end
done = true
vim.api.nvim_del_augroup_by_name("lazy_file")
---@type table<string,string[]>
local skips = {}
for _, event in ipairs(events) do
skips[event.event] = skips[event.event] or Event.get_augroups(event.event)
end
vim.api.nvim_exec_autocmds("User", { pattern = "LazyFile", modeline = false })
for _, event in ipairs(events) do
if vim.api.nvim_buf_is_valid(event.buf) then
Event.trigger({
event = event.event,
exclude = skips[event.event],
data = event.data,
buf = event.buf,
})
if vim.bo[event.buf].filetype then
Event.trigger({
event = "FileType",
buf = event.buf,
})
end
end
end
vim.api.nvim_exec_autocmds("CursorMoved", { modeline = false })
events = {}
end
-- schedule wrap so that nested autocmds are executed
-- and the UI can continue rendering without blocking
load = vim.schedule_wrap(load)
vim.api.nvim_create_autocmd(M.lazy_file_events, {
group = vim.api.nvim_create_augroup("lazy_file", { clear = true }),
callback = function(event)
table.insert(events, event)
load()
end,
})
end
-- >>
return M