1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-15 23:39:27 +08:00

feat(nvim-plug): support enabled option

This commit is contained in:
Eric Wong 2025-02-05 10:03:37 +08:00
parent 2bc42deaf8
commit 8368712a5e
No known key found for this signature in database
GPG Key ID: 41BB7053E835C848
3 changed files with 46 additions and 26 deletions

View File

@ -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<string>`, commands lazy loading |
| `events` | `table<string>`, events lazy loading |
| `on_ft` | `table<string>`, filetypes lazy loading |
| `type` | `string`, plugin type including `color`, `plugin` |
| `build` | `string` or `table<string>`, executed by [job](https://spacevim.org/api/job/) api |
| name | description |
| --------- | --------------------------------------------------------------------------------------------------- |
| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` |
| `cmds` | `table<string>`, commands lazy loading |
| `events` | `table<string>`, events lazy loading |
| `on_ft` | `table<string>`, filetypes lazy loading |
| `type` | `string`, plugin type including `color`, `plugin` |
| `build` | `string` or `table<string>`, 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

View File

@ -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<PluginSpec>
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

View File

@ -21,6 +21,7 @@ local config = require('plug.config')
--- @field path string
--- @field build string|table<string>
--- @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]