Skip to content

Commit edf8310

Browse files
committed
feat(plugin): added Plugin.priority for start plugins
1 parent eec0485 commit edf8310

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

README.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ require("lazy").setup({
103103
| **ft** | `string?` or `string[]` | Lazy-load on filetype |
104104
| **keys** | `string?` or `string[]` or `LazyKeys[]` | Lazy-load on key mapping |
105105
| **module** | `false?` | Do not automatically load this Lua module when it's required somewhere |
106+
| **priority** | `number?` | Only useful for **start** plugins (`lazy=false`) to force loading certain plugins first. Default priority is `50`. It's recommended to set this to a high number for colorschemes. |
106107

107108
### Lazy Loading
108109

@@ -114,9 +115,6 @@ module of plugin `A`, then plugin `A` will be loaded on demand as expected.
114115
If you don't want this behavior for a certain plugin, you can specify that with `module=false`.
115116
You can then manually load the plugin with `:Lazy load foobar.nvim`.
116117

117-
Colorscheme plugins can be configured with `lazy=true`. The plugin will automagically load
118-
when doing `colorscheme foobar`.
119-
120118
You can configure **lazy.nvim** to lazy-load all plugins by default with `config.defaults.lazy = true`.
121119

122120
Additionally, you can also lazy-load on **events**, **commands**,
@@ -128,6 +126,15 @@ Plugins will be lazy-loaded when one of the following is `true`:
128126
- it has an `event`, `cmd`, `ft` or `keys` key
129127
- `config.defaults.lazy == true`
130128

129+
#### 🌈 Colorschemes
130+
131+
Colorscheme plugins can be configured with `lazy=true`. The plugin will automagically load
132+
when doing `colorscheme foobar`.
133+
134+
> **NOTE:** since **start** plugins can possibly change existing highlight groups,
135+
> it's important to make sure that your main **colorscheme** is loaded first.
136+
> To ensure this you can use the `priority=1000` field. **_(see the examples)_**
137+
131138
#### ⌨️ Lazy Key Mappings
132139

133140
The `keys` property can be a `string` or `string[]` for simple normal-mode mappings, or it
@@ -186,7 +193,15 @@ version of plugins that support Semver.
186193
```lua
187194
return {
188195
-- the colorscheme should be available when starting Neovim
189-
"folke/tokyonight.nvim",
196+
{
197+
"folke/tokyonight.nvim",
198+
lazy = false, -- make sure we load this during startup if it is your main colorscheme
199+
priority = 1000, -- make sure to load this before all the other start plugins
200+
config = function()
201+
-- load the colorscheme here
202+
vim.cmd([[colorscheme tokyonight]])
203+
end,
204+
},
190205

191206
-- I have a separate config.mappings file where I require which-key.
192207
-- With lazy the plugin will be automatically loaded when it is required somewhere

lua/lazy/core/loader.lua

+21-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ local Handler = require("lazy.core.handler")
44

55
local M = {}
66

7+
local DEFAULT_PRIORITY = 50
8+
79
---@type LazyPlugin[]
810
M.loading = {}
911
M.init_done = false
@@ -73,8 +75,9 @@ function M.startup()
7375

7476
-- 2. load start plugin
7577
Util.track({ start = "start" })
76-
for _, plugin in pairs(Config.plugins) do
77-
if plugin.lazy == false and not plugin._.loaded then
78+
for _, plugin in ipairs(M.get_start_plugins()) do
79+
-- plugin may be loaded by another plugin in the meantime
80+
if not plugin._.loaded then
7881
M.load(plugin, { start = "start" })
7982
end
8083
end
@@ -103,6 +106,22 @@ function M.startup()
103106
Util.track()
104107
end
105108

109+
function M.get_start_plugins()
110+
---@type LazyPlugin[]
111+
local start = {}
112+
for _, plugin in pairs(Config.plugins) do
113+
if plugin.lazy == false and not plugin._.loaded then
114+
start[#start + 1] = plugin
115+
end
116+
end
117+
table.sort(start, function(a, b)
118+
local ap = a.priority or DEFAULT_PRIORITY
119+
local bp = b.priority or DEFAULT_PRIORITY
120+
return ap > bp
121+
end)
122+
return start
123+
end
124+
106125
---@class Loader
107126
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
108127
---@param reason {[string]:string}

lua/lazy/example.lua

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
return {
22
-- the colorscheme should be available when starting Neovim
3-
"folke/tokyonight.nvim",
3+
{
4+
"folke/tokyonight.nvim",
5+
lazy = false, -- make sure we load this during startup if it is your main colorscheme
6+
priority = 1000, -- make sure to load this before all the other start plugins
7+
config = function()
8+
-- load the colorscheme here
9+
vim.cmd([[colorscheme tokyonight]])
10+
end,
11+
},
412

513
-- I have a separate config.mappings file where I require which-key.
614
-- With lazy the plugin will be automatically loaded when it is required somewhere

lua/lazy/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
---@field enabled? boolean|(fun():boolean)
4242
---@field cond? boolean|(fun():boolean)
4343
---@field lazy? boolean
44+
---@field priority? number Only useful for lazy=false plugins to force loading certain plugins first. Default priority is 50
4445
---@field dev? boolean If set, then link to the respective folder under your ~/projects
4546

4647
---@class LazyPlugin: LazyPluginBase,LazyPluginHandlers,LazyPluginHooks,LazyPluginRef

0 commit comments

Comments
 (0)