Skip to content

Commit

Permalink
perf: improve initial installation and startup performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Apr 1, 2024
1 parent fdbdd5e commit 432897f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ body:
-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.g.astronvim_first_install = true -- lets AstroNvim know that this is an initial installation
-- stylua: ignore
vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable", lazypath })
end
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ Some user's might not want to use an entire template or do any customization. He
```lua
local lazypath = vim.fn.stdpath "data" .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.g.astronvim_first_install = true -- lets AstroNvim know that this is an initial installation
-- stylua: ignore
vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable", lazypath })
end
Expand Down
77 changes: 47 additions & 30 deletions lua/astronvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ M.did_init = false

M.config = require "astronvim.config"

local function lazy_notify()
-- Based on notification lazy loading in LazyVim
-- https://github.com/LazyVim/LazyVim/blob/a50f92f7550fb6e9f21c0852e6cb190e6fcd50f5/lua/lazyvim/util/init.lua#L90-L125
local notifications = {}
local function notify_queue(...) table.insert(notifications, vim.F.pack_len(...)) end
local original_notify = vim.notify
vim.notify = notify_queue

local uv = vim.uv or vim.loop
local timer, checker = uv.new_timer(), uv.new_check()

local replay = function()
timer:stop()
checker:stop()
if vim.notify == notify_queue then vim.notify = original_notify end
vim.schedule(function()
vim.tbl_map(function(notif) vim.notify(vim.F.unpack_len(notif)) end, notifications)
end)
end

-- wait till vim.notify has been replaced
checker:start(function()
if vim.notify ~= notify_queue then replay() end
end)
-- or if it took more than 500ms, then something went wrong
timer:start(500, 0, replay)
end

function M.init()
if vim.fn.has "nvim-0.9" == 0 then
vim.api.nvim_echo({
Expand All @@ -14,36 +42,25 @@ function M.init()
vim.cmd.quit()
end

if not M.did_init then
M.did_init = true

if vim.g.astronvim_first_install then
vim.api.nvim_create_autocmd("User", {
desc = "Load Mason and Treesitter after Lazy installs plugins",
once = true,
pattern = "LazyInstall",
callback = function()
vim.cmd.bw()
vim.api.nvim_exec_autocmds("VimEnter", { modeline = false })
end,
})
end

-- force setup during initialization
local plugin = require("lazy.core.config").spec.plugins.AstroNvim

local opts = require("lazy.core.plugin").values(plugin, "opts")
if opts.pin_plugins == nil then opts.pin_plugins = plugin.version ~= nil end

---@diagnostic disable-next-line: cast-local-type
opts = vim.tbl_deep_extend("force", M.config, opts)
---@cast opts -nil
M.config = opts

if not vim.g.mapleader and M.config.mapleader then vim.g.mapleader = M.config.mapleader end
if not vim.g.maplocalleader and M.config.maplocalleader then vim.g.maplocalleader = M.config.maplocalleader end
if M.config.icons_enabled == false then vim.g.icons_enabled = false end
end
if M.did_init then return end
M.did_init = true

lazy_notify()

-- force setup during initialization
local plugin = require("lazy.core.config").spec.plugins.AstroNvim

local opts = require("lazy.core.plugin").values(plugin, "opts")
if opts.pin_plugins == nil then opts.pin_plugins = plugin.version ~= nil end

---@diagnostic disable-next-line: cast-local-type
opts = vim.tbl_deep_extend("force", M.config, opts)
---@cast opts -nil
M.config = opts

if not vim.g.mapleader and M.config.mapleader then vim.g.mapleader = M.config.mapleader end
if not vim.g.maplocalleader and M.config.maplocalleader then vim.g.maplocalleader = M.config.maplocalleader end
if M.config.icons_enabled == false then vim.g.icons_enabled = false end
end

function M.setup() end
Expand Down

0 comments on commit 432897f

Please sign in to comment.