Skip to content

Commit

Permalink
feat(plugin): added config.defaults.cond. Fixes #640
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Mar 14, 2023
1 parent 10f5844 commit 9afba38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require("lazy").setup({
| **dev** | `boolean?` | When `true`, a local plugin directory will be used instead. See `config.dev` |
| **lazy** | `boolean?` | When `true`, the plugin will only be loaded when needed. Lazy-loaded plugins are automatically loaded when their Lua modules are `required`, or when one of the lazy-loading handlers triggers |
| **enabled** | `boolean?` or `fun():boolean` | When `false`, or if the `function` returns false, then this plugin will not be included in the spec |
| **cond** | `boolean?` or `fun():boolean` | When `false`, or if the `function` returns false, then this plugin will not be loaded. Useful to disable some plugins in vscode, or firenvim for example. |
| **cond** | `boolean?` or `fun(LazyPlugin):boolean` | When `false`, or if the `function` returns false, then this plugin will not be loaded. Useful to disable some plugins in vscode, or firenvim for example. |
| **dependencies** | `LazySpec[]` | A list of plugin names or plugin specs that should be loaded when the plugin loads. Dependencies are always lazy-loaded unless specified otherwise. When specifying a name, make sure the plugin spec has been defined somewhere else. |
| **init** | `fun(LazyPlugin)` | `init` functions are always executed during startup |
| **opts** | `table` or `fun(LazyPlugin, opts:table)` | `opts` should be a table (will be merged with parent specs), return a table (replaces parent specs) or should change a table. The table will be passed to the `Plugin.config()` function. Setting this value will imply `Plugin.config()` |
Expand Down Expand Up @@ -303,6 +303,9 @@ return {
defaults = {
lazy = false, -- should plugins be lazy-loaded?
version = nil,
-- default `cond` you can use to globally disable a lot of plugins
-- when running inside vscode for example
cond = nil, ---@type boolean|fun(self:LazyPlugin):boolean|nil
-- version = "*", -- enable this to try installing the latest stable versions of plugins
},
-- leave nil when passing the spec as the first argument to setup()
Expand Down
3 changes: 3 additions & 0 deletions lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ M.defaults = {
defaults = {
lazy = false, -- should plugins be lazy-loaded?
version = nil,
-- default `cond` you can use to globally disable a lot of plugins
-- when running inside vscode for example
cond = nil, ---@type boolean|fun(self:LazyPlugin):boolean|nil
-- version = "*", -- enable this to try installing the latest stable versions of plugins
},
-- leave nil when passing the spec as the first argument to setup()
Expand Down
8 changes: 6 additions & 2 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,12 @@ function M._load(plugin, reason, opts)
return Util.error("Plugin " .. plugin.name .. " is not installed")
end

if plugin.cond ~= nil and not (opts and opts.force) then
if plugin.cond == false or (type(plugin.cond) == "function" and not plugin.cond()) then
local cond = plugin.cond
if cond == nil then
cond = Config.options.defaults.cond
end
if cond ~= nil and not (opts and opts.force) then
if cond == false or (type(cond) == "function" and not cond(plugin)) then
plugin._.cond = false
return
end
Expand Down

0 comments on commit 9afba38

Please sign in to comment.