feat(extras): added a recommended plugin/language section to :LazyExtras

This commit is contained in:
Folke Lemaitre 2024-05-18 14:11:50 +02:00
parent 434883632c
commit 16eb3d9473
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -16,12 +16,15 @@ local Text = require("lazy.view.text")
---@field desc? string ---@field desc? string
---@field enabled boolean ---@field enabled boolean
---@field managed boolean ---@field managed boolean
---@field recommended? boolean
---@field row? number ---@field row? number
---@field section? string
---@field plugins string[] ---@field plugins string[]
---@field optional string[] ---@field optional string[]
---@class lazyvim.util.extras ---@class lazyvim.util.extras
local M = {} local M = {}
M.buf = 0
---@type LazyExtraSource[] ---@type LazyExtraSource[]
M.sources = { M.sources = {
@ -33,6 +36,24 @@ M.ns = vim.api.nvim_create_namespace("lazyvim.extras")
---@type string[] ---@type string[]
M.state = nil M.state = nil
---@param opts {ft?: string|string[], root?: string|string[]}
---@return boolean
function M.wants(opts)
if opts.ft then
opts.ft = type(opts.ft) == "string" and { opts.ft } or opts.ft
for _, f in ipairs(opts.ft) do
if vim.bo[M.buf].filetype == f then
return true
end
end
end
if opts.root then
opts.root = type(opts.root) == "string" and { opts.root } or opts.root
return #LazyVim.root.detectors.pattern(M.buf, opts.root) > 0
end
return false
end
---@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
@ -74,6 +95,12 @@ function M.get_extra(source, modname)
table.sort(plugins) table.sort(plugins)
table.sort(optional) table.sort(optional)
---@type boolean|(fun():boolean?)|nil
local recommended = require(modname).recommended or false
if type(recommended) == "function" then
recommended = recommended() or false
end
---@type LazyExtra ---@type LazyExtra
return { return {
source = source, source = source,
@ -81,6 +108,7 @@ function M.get_extra(source, modname)
module = modname, module = modname,
enabled = enabled, enabled = enabled,
desc = require(modname).desc, desc = require(modname).desc,
recommended = recommended,
managed = vim.tbl_contains(Config.json.data.extras, modname) or not enabled, managed = vim.tbl_contains(Config.json.data.extras, modname) or not enabled,
plugins = plugins, plugins = plugins,
optional = optional, optional = optional,
@ -97,6 +125,7 @@ local X = {}
---@return LazyExtraView ---@return LazyExtraView
function X.new() function X.new()
local self = setmetatable({}, { __index = X }) local self = setmetatable({}, { __index = X })
M.buf = vim.api.nvim_get_current_buf()
self.float = Float.new({ title = "LazyVim Extras" }) self.float = Float.new({ title = "LazyVim Extras" })
self.float:on_key("x", function() self.float:on_key("x", function()
self:toggle() self:toggle()
@ -186,8 +215,14 @@ function X:render()
:append("<x>", "LazySpecial") :append("<x>", "LazySpecial")
:append(" key", "LazyComment") :append(" key", "LazyComment")
:nl() :nl()
for _, extra in ipairs(self.extras) do
extra.section = nil
end
self:section({ enabled = true, title = "Enabled" }) self:section({ enabled = true, title = "Enabled" })
self:section({ enabled = false, title = "Disabled" }) self:section({ recommended = true, filter = "^lang%.", title = "Recommended Languages", empty = false })
self:section({ recommended = true, title = "Recommended Plugins", empty = false })
self:section({ title = "Languages", filter = "^lang%." })
self:section({ title = "Plugins" })
end end
---@param extra LazyExtra ---@param extra LazyExtra
@ -206,6 +241,9 @@ 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.recommended then
self.text:append(" "):append(LazyConfig.options.ui.icons.favorite or "", "LazyCommit")
end
if extra.source.name ~= "LazyVim" then if extra.source.name ~= "LazyVim" then
self.text:append(" "):append(LazyConfig.options.ui.icons.event .. " " .. extra.source.name, "LazyReasonEvent") self.text:append(" "):append(LazyConfig.options.ui.icons.event .. " " .. extra.source.name, "LazyReasonEvent")
end end
@ -221,16 +259,24 @@ function X:extra(extra)
self.text:nl() self.text:nl()
end end
---@param opts {enabled?:boolean, title?:string} ---@param opts {enabled?:boolean, title:string, recommended?:boolean, filter?:string, empty?:boolean}
function X:section(opts) function X:section(opts)
opts = opts or {} opts = opts or {}
---@type LazyExtra[] ---@type LazyExtra[]
local extras = vim.tbl_filter(function(extra) local extras = vim.tbl_filter(function(extra)
return opts.enabled == nil or extra.enabled == opts.enabled return extra.section == nil
and (opts.enabled == nil or extra.enabled == opts.enabled)
and (opts.recommended == nil or extra.recommended == opts.recommended)
and (opts.filter == nil or extra.name:find(opts.filter))
end, self.extras) end, self.extras)
if opts.empty == false and #extras == 0 then
return
end
self.text:nl():append(opts.title .. ":", "LazyH2"):append(" (" .. #extras .. ")", "LazyComment"):nl() self.text:nl():append(opts.title .. ":", "LazyH2"):append(" (" .. #extras .. ")", "LazyComment"):nl()
for _, extra in ipairs(extras) do for _, extra in ipairs(extras) do
extra.section = opts.title
self:extra(extra) self:extra(extra)
end end
end end