From d909aa1a518cde13125ff47e9c68fa622847f206 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Thu, 18 Apr 2024 12:49:06 +0700 Subject: [PATCH] feat(nvim): Port LazyFile REF: https://github.com/folke/lazy.nvim/issues/1182 --- .config/nvim/lua/null/plugins/treesitter.lua | 4 ++ .config/nvim/lua/null/util.lua | 70 ++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/.config/nvim/lua/null/plugins/treesitter.lua b/.config/nvim/lua/null/plugins/treesitter.lua index 654d229..cad7dd2 100644 --- a/.config/nvim/lua/null/plugins/treesitter.lua +++ b/.config/nvim/lua/null/plugins/treesitter.lua @@ -1,6 +1,10 @@ +require("null.util").lazy_file() + return { { "nvim-treesitter/nvim-treesitter", + build = ":TSUpdate", + event = { "LazyFile", "VeryLazy" }, opts = { ensure_installed = { "c", diff --git a/.config/nvim/lua/null/util.lua b/.config/nvim/lua/null/util.lua index 75b39d7..00d465c 100644 --- a/.config/nvim/lua/null/util.lua +++ b/.config/nvim/lua/null/util.lua @@ -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 + 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