From 07b467738d3ca0863e957a2bca86825f6aff92df Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Thu, 22 Dec 2022 13:49:00 +0100 Subject: [PATCH] feat(loader): automatically lazy-load colorschemes --- README.md | 4 ++++ lua/lazy/core/loader.lua | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 3fb6069..14a0403 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ - 🔎 Automatically check for updates - 📋 Commit, branch, tag, version, and full [Semver](https://devhints.io/semver) support - 📈 Statusline component to see the number of pending updates +- 🎨 Automatically lazy-loads colorschemes ## ⚡️ Requirements @@ -112,6 +113,9 @@ module of plugin `A`, then plugin `A` will be loaded on demand as expected. If you don't want this behavior for a certain plugin, you can specify that with `module=false`. You can then manually load the plugin with `:Lazy load foobar.nvim`. +Colorscheme plugins can be configured with `lazy=true`. The plugin will automagically load +when doing `colorscheme foobar`. + You can configure **lazy.nvim** to lazy-load all plugins by default with `config.defaults.lazy = true`. Additionally, you can also lazy-load on **events**, **commands**, diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index c4b0df2..2e66930 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -37,6 +37,12 @@ function M.setup() M.disabled_rtp_plugins[file] = true end + vim.api.nvim_create_autocmd("ColorSchemePre", { + callback = function(event) + M.colorscheme(event.match) + end, + }) + -- autoload opt plugins table.insert(package.loaders, M.autoload) end @@ -191,6 +197,22 @@ function M.source(path) Util.track() end +function M.colorscheme(name) + if vim.tbl_contains(vim.fn.getcompletion("", "color"), name) then + return + end + for _, plugin in pairs(Config.plugins) do + if not plugin._.loaded then + for _, ext in ipairs({ "lua", "vim" }) do + local path = plugin.dir .. "/colors/" .. name .. "." .. ext + if vim.loop.fs_stat(path) then + return M.load(plugin, { colorscheme = name }) + end + end + end + end +end + -- This loader is added as the very last one. -- This only hits when the modname is not cached and -- even then only once per plugin. So pretty much never.