Skip to content

Commit

Permalink
perf(startup): render a file opened from the cmdline as soon as possi…
Browse files Browse the repository at this point in the history
…ble and get rid of lazy_file logic
  • Loading branch information
folke committed May 14, 2024
1 parent b29d169 commit 965a469
Showing 1 changed file with 26 additions and 65 deletions.
91 changes: 26 additions & 65 deletions lua/lazyvim/util/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local Plugin = require("lazy.core.plugin")
---@class lazyvim.util.plugin
local M = {}

M.use_lazy_file = true
M.lazy_file_events = { "BufReadPost", "BufNewFile", "BufWritePre" }

---@type table<string, string>
Expand Down Expand Up @@ -55,76 +54,38 @@ function M.extra_idx(name)
end
end

-- 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
local augroups = Event.get_augroups(event.event)
local groups = vim.tbl_filter(function(t)
return not vim.tbl_contains({ t }, "filetypedetect")
end, augroups)
skips[event.event] = skips[event.event] or groups
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
-- This autocmd will only trigger when a file was loaded from the cmdline.
-- It will render the file as quickly as possible.
vim.api.nvim_create_autocmd("BufReadPost", {
once = true,
callback = function(event)
-- Skip if we already entered vim
if vim.v.vim_did_enter == 1 then
return
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)
-- Try to guess the filetype (may change later on during Neovim startup)
local ft = vim.filetype.match({ buf = event.buf })
if ft then
-- Add treesitter highlights and fallback to syntax
local lang = vim.treesitter.language.get_lang(ft)
if not (lang and pcall(vim.treesitter.start, event.buf, lang)) then
vim.bo[event.buf].syntax = ft
vim.notify("Could not load treesitter for " .. ft, "warn", { title = "LazyVim" })
end

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()
-- Trigger early redraw
vim.cmd([[redraw]])
end
end,
})

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

Event.mappings.LazyFile = { id = "LazyFile", event = M.lazy_file_events }
Event.mappings["User LazyFile"] = Event.mappings.LazyFile
end

function M.fix_imports()
Expand Down

0 comments on commit 965a469

Please sign in to comment.