From 8368712a5ea3f72647eb5bbb6d40f59b270fee28 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 5 Feb 2025 10:03:37 +0800 Subject: [PATCH] feat(nvim-plug): support `enabled` option --- bundle/nvim-plug/README.md | 23 ++++++++++------ bundle/nvim-plug/lua/plug/init.lua | 40 +++++++++++++++------------- bundle/nvim-plug/lua/plug/loader.lua | 9 +++++++ 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/bundle/nvim-plug/README.md b/bundle/nvim-plug/README.md index 8e32d2494..25d002300 100644 --- a/bundle/nvim-plug/README.md +++ b/bundle/nvim-plug/README.md @@ -22,6 +22,12 @@ require('plug').add({ events = { 'VimEnter' }, config = function() end, }, + { + 'wsdjeg/vim-chat', + enabled = function() + return vim.fn.has('nvim-0.10.0') == 1 + end, + }, { 'wsdjeg/flygrep.nvim', cmds = { 'FlyGrep' }, @@ -38,14 +44,15 @@ require('plug').add({ ## Plugin Spec -| name | description | -| -------- | --------------------------------------------------------------------------------- | -| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | -| `cmds` | `table`, commands lazy loading | -| `events` | `table`, events lazy loading | -| `on_ft` | `table`, filetypes lazy loading | -| `type` | `string`, plugin type including `color`, `plugin` | -| `build` | `string` or `table`, executed by [job](https://spacevim.org/api/job/) api | +| name | description | +| --------- | --------------------------------------------------------------------------------------------------- | +| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | +| `cmds` | `table`, commands lazy loading | +| `events` | `table`, events lazy loading | +| `on_ft` | `table`, filetypes lazy loading | +| `type` | `string`, plugin type including `color`, `plugin` | +| `build` | `string` or `table`, executed by [job](https://spacevim.org/api/job/) api | +| `enabled` | `boolean` or `function` evaluated when startup, when it is false, plugin will be skiped | ## Commands diff --git a/bundle/nvim-plug/lua/plug/init.lua b/bundle/nvim-plug/lua/plug/init.lua index 26440ce7a..0816ed6d8 100644 --- a/bundle/nvim-plug/lua/plug/init.lua +++ b/bundle/nvim-plug/lua/plug/init.lua @@ -9,38 +9,42 @@ local M = {} local all_plugins = {} -local hooks = require("plug.hooks") -local loader = require("plug.loader") -local config = require("plug.config") +local hooks = require('plug.hooks') +local loader = require('plug.loader') +local config = require('plug.config') function M.setup(opt) - config.setup(opt) + config.setup(opt) end --- @param plugins table function M.add(plugins) - for _, plug in ipairs(plugins) do - loader.parser(plug) - all_plugins[plug.name] = plug - if plug.cmds then - hooks.on_cmds(plug.cmds, plug) - end - if plug.events then - hooks.on_events(plug.events, plug) - end + for _, plug in ipairs(plugins) do + loader.parser(plug) + if not plug.enabled then + goto continue + end + all_plugins[plug.name] = plug + if plug.cmds then + hooks.on_cmds(plug.cmds, plug) + end + if plug.events then + hooks.on_events(plug.events, plug) + end if plug.on_ft then hooks.on_ft(plug.on_ft, plug) end - if not plug.events and not plug.cmds and not plug.on_ft then - loader.load(plug) - end - end + if not plug.events and not plug.cmds and not plug.on_ft then + loader.load(plug) + end + ::continue:: + end end function M.get() - return all_plugins + return all_plugins end return M diff --git a/bundle/nvim-plug/lua/plug/loader.lua b/bundle/nvim-plug/lua/plug/loader.lua index 0a8dcc2e7..d090c1ae6 100644 --- a/bundle/nvim-plug/lua/plug/loader.lua +++ b/bundle/nvim-plug/lua/plug/loader.lua @@ -21,6 +21,7 @@ local config = require('plug.config') --- @field path string --- @field build string|table --- @field is_local boolean true for local plugin +--- @field when boolean|string|function local function is_local_plugin(plugSpec) if plugSpec.is_local or vim.fn.isdirectory(plugSpec[1]) == 1 then @@ -35,6 +36,14 @@ local function unique_name(plugSpec) end function M.parser(plugSpec) + if type(plugSpec.enabled) == "nil" then + plugSpec.enabled = true + elseif type(plugSpec.enabled) == "function" then + plugSpec.enabled = plugSpec.enabled() + elseif type(plugSpec.enabled) ~= "boolean" or plugSpec.enabled == false then + plugSpec.enabled = false + return plugSpec + end plugSpec.name = unique_name(plugSpec) if is_local_plugin(plugSpec) then plugSpec.rtp = plugSpec[1]