feat(extras): LazyExtras can now manage user extras lua/plugins/extras. Fixes #1681

This commit is contained in:
Folke Lemaitre 2023-10-12 21:47:31 +02:00
parent c9892652d2
commit 1bcf6b9a28
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 74 additions and 41 deletions

View file

@ -2,6 +2,9 @@
## 10.x ## 10.x
- User extras under `lua/plugins/extras` can now also be managed
with **LazyExtras**
- `nvim-ts-autotag` is now included by default - `nvim-ts-autotag` is now included by default
- `nvim-treesitter-context` is now included by default - `nvim-treesitter-context` is now included by default

View file

@ -2,10 +2,10 @@ local Config = require("lazyvim.config")
-- Some extras need to be loaded before others -- Some extras need to be loaded before others
local prios = { local prios = {
["editor.aerial"] = 100, ["lazyvim.plugins.extras.editor.aerial"] = 100,
["editor.symbols-outline"] = 100, ["lazyvim.plugins.extras.editor.symbols-outline"] = 100,
["test.core"] = 1, ["lazyvim.plugins.extras.test.core"] = 1,
["dap.core"] = 1, ["lazyvim.plugins.extras.dap.core"] = 1,
} }
table.sort(Config.json.data.extras, function(a, b) table.sort(Config.json.data.extras, function(a, b)
@ -19,5 +19,5 @@ end)
---@param extra string ---@param extra string
return vim.tbl_map(function(extra) return vim.tbl_map(function(extra)
return { import = "lazyvim.plugins.extras." .. extra } return { import = extra }
end, Config.json.data.extras) end, Config.json.data.extras)

View file

@ -5,8 +5,16 @@ local Plugin = require("lazy.core.plugin")
local Text = require("lazy.view.text") local Text = require("lazy.view.text")
local Util = require("lazyvim.util") local Util = require("lazyvim.util")
---@class LazyExtraSource
---@field name string
---@field desc? string
---@field module string
---@class LazyExtra ---@class LazyExtra
---@field name string ---@field name string
---@field source LazyExtraSource
---@field module string
---@field desc? string
---@field enabled boolean ---@field enabled boolean
---@field managed boolean ---@field managed boolean
---@field row? number ---@field row? number
@ -16,6 +24,12 @@ local Util = require("lazyvim.util")
---@class lazyvim.util.extras ---@class lazyvim.util.extras
local M = {} local M = {}
---@type LazyExtraSource[]
M.sources = {
{ name = "LazyVim", desc = "LazyVim extras", module = "lazyvim.plugins.extras" },
{ name = "User", desc = "User extras", module = "plugins.extras" },
}
M.ns = vim.api.nvim_create_namespace("lazyvim.extras") M.ns = vim.api.nvim_create_namespace("lazyvim.extras")
---@type string[] ---@type string[]
M.state = nil M.state = nil
@ -23,22 +37,29 @@ M.state = nil
---@return LazyExtra[] ---@return LazyExtra[]
function M.get() function M.get()
M.state = M.state or LazyConfig.spec.modules M.state = M.state or LazyConfig.spec.modules
local root = LazyConfig.plugins.LazyVim.dir .. "/lua/lazyvim/plugins/extras" local extras = {} ---@type LazyExtra[]
local extras = {} ---@type string[] for _, source in ipairs(M.sources) do
local root = Util.find_root(source.module)
if root then
Util.walk(root, function(path, name, type) Util.walk(root, function(path, name, type)
if type == "file" and name:match("%.lua$") then if type == "file" and name:match("%.lua$") then
local extra = path:sub(#root + 2, -5):gsub("/", ".") name = path:sub(#root + 2, -5):gsub("/", ".")
extras[#extras + 1] = extra extras[#extras + 1] = M.get_extra(source, source.module .. "." .. name)
end end
end) end)
table.sort(extras) end
end
table.sort(extras, function(a, b)
return a.name < b.name
end)
return extras
end
---@param extra string ---@param modname string
return vim.tbl_map(function(extra) ---@param source LazyExtraSource
local modname = "lazyvim.plugins.extras." .. extra function M.get_extra(source, modname)
local enabled = vim.tbl_contains(M.state, modname) local enabled = vim.tbl_contains(M.state, modname)
local spec = Plugin.Spec.new({ import = "lazyvim.plugins.extras." .. extra }, { optional = true }) local spec = Plugin.Spec.new({ import = modname }, { optional = true })
local plugins = {} ---@type string[] local plugins = {} ---@type string[]
local optional = {} ---@type string[] local optional = {} ---@type string[]
for _, p in pairs(spec.plugins) do for _, p in pairs(spec.plugins) do
@ -51,14 +72,17 @@ function M.get()
table.sort(plugins) table.sort(plugins)
table.sort(optional) table.sort(optional)
---@type LazyExtra
return { return {
name = extra, source = source,
name = modname:sub(#source.module + 2),
module = modname,
enabled = enabled, enabled = enabled,
managed = vim.tbl_contains(Config.json.data.extras, extra) or not enabled, desc = require(modname).desc,
managed = vim.tbl_contains(Config.json.data.extras, modname) or not enabled,
plugins = plugins, plugins = plugins,
optional = optional, optional = optional,
} }
end, extras)
end end
---@class LazyExtraView ---@class LazyExtraView
@ -100,17 +124,17 @@ function X:toggle()
end end
extra.enabled = not extra.enabled extra.enabled = not extra.enabled
Config.json.data.extras = vim.tbl_filter(function(name) Config.json.data.extras = vim.tbl_filter(function(name)
return name ~= extra.name return name ~= extra.module
end, Config.json.data.extras) end, Config.json.data.extras)
M.state = vim.tbl_filter(function(name) M.state = vim.tbl_filter(function(name)
return name ~= "lazyvim.plugins.extras." .. extra.name return name ~= extra.module
end, M.state) end, M.state)
if extra.enabled then if extra.enabled then
table.insert(Config.json.data.extras, extra.name) table.insert(Config.json.data.extras, extra.module)
M.state[#M.state + 1] = "lazyvim.plugins.extras." .. extra.name M.state[#M.state + 1] = extra.module
end end
table.sort(Config.json.data.extras) table.sort(Config.json.data.extras)
Config.json.save() Util.json.save()
Util.info( Util.info(
"`" "`"
.. extra.name .. extra.name
@ -180,12 +204,18 @@ function X:extra(extra)
self.text:append(" " .. LazyConfig.options.ui.icons.not_loaded .. " ", hl) self.text:append(" " .. LazyConfig.options.ui.icons.not_loaded .. " ", hl)
end end
self.text:append(extra.name) self.text:append(extra.name)
if extra.source.name ~= "LazyVim" then
self.text:append(" "):append(LazyConfig.options.ui.icons.event .. " " .. extra.source.name, "LazyReasonEvent")
end
for _, plugin in ipairs(extra.plugins) do for _, plugin in ipairs(extra.plugins) do
self.text:append(" "):append(LazyConfig.options.ui.icons.plugin .. "" .. plugin, "LazyReasonPlugin") self.text:append(" "):append(LazyConfig.options.ui.icons.plugin .. "" .. plugin, "LazyReasonPlugin")
end end
for _, plugin in ipairs(extra.optional) do for _, plugin in ipairs(extra.optional) do
self.text:append(" "):append(LazyConfig.options.ui.icons.plugin .. "" .. plugin, "LazyReasonRequire") self.text:append(" "):append(LazyConfig.options.ui.icons.plugin .. "" .. plugin, "LazyReasonRequire")
end end
if extra.desc then
self.text:nl():append(" " .. extra.desc, "LazyComment")
end
self.text:nl() self.text:nl()
end end