From 1e2b5afcf7ed0ce3b0dd9ba3ba5f054a9e2b8f38 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 12 Feb 2025 02:04:57 +0800 Subject: [PATCH] feat(nvim-plug): support priority option --- bundle/nvim-plug/README.md | 49 +++++++++++++++++++++++++++- bundle/nvim-plug/lua/plug/config.lua | 2 ++ bundle/nvim-plug/lua/plug/init.lua | 37 ++++++++++++++++----- bundle/nvim-plug/test/init.lua | 3 ++ 4 files changed, 82 insertions(+), 9 deletions(-) diff --git a/bundle/nvim-plug/README.md b/bundle/nvim-plug/README.md index 178214ec5..c995db4bf 100644 --- a/bundle/nvim-plug/README.md +++ b/bundle/nvim-plug/README.md @@ -14,6 +14,7 @@ - [Commands](#commands) - [Default UI](#default-ui) - [Custom Plugin UI](#custom-plugin-ui) + - [Plugin priority](#plugin-priority) - [Feedback](#feedback) @@ -50,6 +51,8 @@ require('plug').setup({ https_proxy = 'http://127.0.0.1:7890', -- default history depth for `git clone` clone_depth = 1, + -- plugin priority, readme [plugin priority] for more info + enable_priority = false }) ``` @@ -114,8 +117,10 @@ The plugin spec is inspired by [dein.nvim](https://github.com/Shougo/dein.vim). | `tag` | `string` specific git tag | | `type` | `string` specific plugin type, this can be git, raw or none, if it is raw, `script_type` must be set | | `autoload` | `boolean`, load plugin after git clone | +| `priority` | `number`, default is 50, set the order in which plugins are loaded | -`config` and `config_after` function will be not be called if the plugin has not been installed. +- `config` and `config_after` function will be not be called if the plugin has not been installed. +- `priority` does not work for lazy plugins. ## Commands @@ -179,6 +184,48 @@ require('plug').setup({ }) ``` +### Plugin priority + +By default this feature is disabled, plugins will be loaded when run `add({plugins})` function. +To enable plugin priority feature, you need to call `plug.load()` after `plug.add()` function. +This option is not for lazy plugins. + +for example: + +```lua +require('plug').setup({ + max_processes = 5, + enable_priority = true, +}) +require('plug').add({ + { + 'wsdjeg/scrollbar.vim', + events = { 'VimEnter' }, + }, + { + 'wsdjeg/vim-chat', + enabled = function() + return vim.fn.has('nvim-0.10.0') == 1 + end, + }, + { + 'wsdjeg/flygrep.nvim', + cmds = { 'FlyGrep' }, + config = function() + require('flygrep').setup() + end, + }, + { + 'rakr/vim-one', + config = function() + vim.cmd('colorscheme one') + end, + priority = 100, + }, +}) +require('plug').load() +``` + ## Feedback The development of this plugin is in [`SpaceVim/bundle/nvim-plug`](https://github.com/SpaceVim/SpaceVim/tree/master/bundle/nvim-plug) directory. diff --git a/bundle/nvim-plug/lua/plug/config.lua b/bundle/nvim-plug/lua/plug/config.lua index 31c8f3317..2f98a99f2 100644 --- a/bundle/nvim-plug/lua/plug/config.lua +++ b/bundle/nvim-plug/lua/plug/config.lua @@ -13,6 +13,7 @@ M.max_processes = 5 M.base_url = 'https://github.com/' M.ui = 'notify' M.clone_depth = '1' +M.enable_priority = false function M.setup(opt) M.bundle_dir = opt.bundle_dir or M.bundle_dir M.max_processes = opt.max_processes or M.max_processes @@ -22,6 +23,7 @@ function M.setup(opt) M.https_proxy = opt.https_proxy M.clone_depth = opt.clone_depth or M.clone_depth M.raw_plugin_dir = opt.raw_plugin_dir or M.raw_plugin_dir + M.enable_priority = opt.enable_priority or M.enable_priority end return M diff --git a/bundle/nvim-plug/lua/plug/init.lua b/bundle/nvim-plug/lua/plug/init.lua index 2bc83f5b5..d44d102f1 100644 --- a/bundle/nvim-plug/lua/plug/init.lua +++ b/bundle/nvim-plug/lua/plug/init.lua @@ -48,14 +48,16 @@ function M.add(plugins, skip_deps) hooks.on_func(plug.on_func, plug) end - if - not plug.events - and not plug.cmds - and not plug.on_ft - and not plug.on_map - and not plug.on_func - then - loader.load(plug) + if not config.enable_priority then + if + not plug.events + and not plug.cmds + and not plug.on_ft + and not plug.on_map + and not plug.on_func + then + loader.load(plug) + end end ::continue:: end @@ -66,4 +68,23 @@ function M.get() return all_plugins end +function M.load() + if config.enable_priority then + local start = {} + for _, v in pairs(all_plugins) do + if not v.events and not v.cmds and not v.on_ft and not v.on_map and not v.on_func then + table.insert(start, v) + end + end + table.sort(start, function(a, b) + local priority_a = a.priority or 50 + local priority_b = b.priority or 50 + return priority_a > priority_b + end) + for _, v in ipairs(start) do + loader.load(v) + end + end +end + return M diff --git a/bundle/nvim-plug/test/init.lua b/bundle/nvim-plug/test/init.lua index b2ad1aafa..7bfbd6e86 100644 --- a/bundle/nvim-plug/test/init.lua +++ b/bundle/nvim-plug/test/init.lua @@ -15,6 +15,7 @@ require('plug').setup({ ui = 'default', http_proxy = 'http://127.0.0.1:7890', https_proxy = 'http://127.0.0.1:7890', + enable_priority = true }) require('plug').add({ @@ -70,6 +71,7 @@ require('plug').add({ config = function() vim.cmd('colorscheme one') end, + priority = 100, }, { 'wsdjeg/flygrep.nvim', @@ -83,4 +85,5 @@ require('plug').add({ on_map = { '(clever-f' }, }, }) +require('plug').load() vim.cmd('nmap f (clever-f-f)')