refactor: move all meta stuff to snacks.meta

This commit is contained in:
Folke Lemaitre 2024-12-10 11:19:08 +01:00
parent 140204fde5
commit 22c5ffd12c
4 changed files with 112 additions and 61 deletions

View file

@ -11,19 +11,12 @@ local M = setmetatable({}, {
end
end,
})
M.prefix = ""
M.needs_setup = {
"bigfile",
"dashboard",
"indent",
"input",
"notifier",
"quickfile",
"scroll",
"statuscolumn",
"words",
M.meta = {
desc = "Snacks health checks",
readme = false,
health = false,
}
function M.check()
@ -50,29 +43,21 @@ function M.check()
M.error("`snacks.nvim` not found in lazy")
end
end
local root = debug.getinfo(1, "S").source:match("@(.*)")
root = vim.fn.fnamemodify(root, ":h")
for file, t in vim.fs.dir(root, { depth = 1 }) do
local name = t == "file" and file:match("(.*)%.lua") or file
if name and not vim.tbl_contains({ "init", "docs", "health", "types" }, name) then
local mod = Snacks[name] --[[@as {health?: fun()}]]
assert(type(mod) == "table", ("`Snacks.%s` not found"):format(name))
local opts = Snacks.config[name] or {} --[[@as {enabled?: boolean}]]
local needs_setup = vim.tbl_contains(M.needs_setup, name)
if needs_setup or mod.health then
M.start(("Snacks.%s"):format(name))
-- M.prefix = ("`Snacks.%s` "):format(name)
if needs_setup then
if opts.enabled then
M.ok("setup {enabled}")
else
M.warn("setup {disabled}")
end
end
if mod.health then
mod.health()
for _, plugin in ipairs(Snacks.meta.get()) do
local opts = Snacks.config[plugin.name] or {} --[[@as {enabled?: boolean}]]
if plugin.meta.health ~= false and (plugin.meta.needs_setup or plugin.health) then
M.start(("Snacks.%s"):format(plugin.name))
-- M.prefix = ("`Snacks.%s` "):format(name)
if plugin.meta.needs_setup then
if opts.enabled then
M.ok("setup {enabled}")
else
M.warn("setup {disabled}")
end
end
if plugin.health then
plugin.health()
end
end
end
end

View file

@ -1,5 +1,10 @@
local M = {}
M.meta = {
desc = "Doc-gen for Snacks",
hide = true,
}
local query = vim.treesitter.query.parse(
"lua",
[[
@ -366,33 +371,21 @@ function M.write(name, lines)
end
function M._build()
local plugins = {} ---@type {name:string, config:boolean}[]
local skip = { "docs", "icons" }
local files = {} ---@type table<string, string>
local plugins = Snacks.meta.get()
for file, t in vim.fs.dir("lua/snacks", { depth = 1 }) do
local name = vim.fn.fnamemodify(file, ":t:r")
if not vim.tbl_contains(skip, name) then
file = t == "directory" and ("%s/init.lua"):format(file) or file
files[name] = file
for _, plugin in pairs(plugins) do
if plugin.meta.docs then
local name = plugin.name
print("[gen] " .. name .. ".md")
local lines = vim.fn.readfile(plugin.file)
local info = M.extract(lines)
if name ~= "init" and name ~= "types" then
plugin.meta.config = info.config ~= nil
end
M.write(name, M.render(name, info))
end
end
for name, file in pairs(files) do
print(name .. ".md")
local path = ("lua/snacks/%s"):format(file)
local lines = vim.fn.readfile(path)
local info = M.extract(lines)
if name ~= "init" and name ~= "types" then
table.insert(plugins, { name = name, config = info.config and true or false })
end
M.write(name, M.render(name, info))
end
table.sort(plugins, function(a, b)
return a.name < b.name
end)
do
local path = "lua/snacks/init.lua"
local lines = vim.fn.readfile(path)
@ -401,8 +394,8 @@ function M._build()
local example = table.concat(vim.fn.readfile("docs/examples/init.lua"), "\n")
lines = {}
lines[1] = "---@class snacks.Config"
for _, plugin in ipairs(plugins) do
if plugin.config then
for _, plugin in pairs(plugins) do
if plugin.meta.config then
local line = ("---@field %s? snacks.%s.Config"):format(plugin.name, plugin.name)
table.insert(lines, line)
end
@ -428,17 +421,19 @@ function M._build()
lines[#lines + 1] = ""
lines[#lines + 1] = "---@class snacks.plugins"
for _, plugin in ipairs(plugins) do
lines[#lines + 1] = ("---@field %s snacks.%s"):format(plugin.name, plugin.name)
if plugin.meta.types then
lines[#lines + 1] = ("---@field %s snacks.%s"):format(plugin.name, plugin.name)
end
end
lines[#lines + 1] = ""
lines[#lines + 1] = "---@class snacks.plugins.Config"
for _, plugin in ipairs(plugins) do
if plugin.config then
for _, plugin in pairs(plugins) do
if plugin.meta.config then
lines[#lines + 1] = ("---@field %s? snacks.%s.Config"):format(plugin.name, plugin.name)
end
end
vim.fn.writefile(lines, "lua/snacks/types.lua")
vim.fn.writefile(lines, "lua/snacks/meta/types.lua")
end
function M.fix_titles()

68
lua/snacks/meta/init.lua Normal file
View file

@ -0,0 +1,68 @@
---@class snacks.meta
local M = {}
M.meta = {
desc = "Meta functions for Snacks",
readme = false,
}
---@class snacks.meta.Meta
---@field desc string
---@field needs_setup? boolean
---@field hide? boolean
---@field readme? boolean
---@field docs? boolean
---@field health? boolean
---@field types? boolean
---@field config? boolean
---@class snacks.meta.Plugin
---@field name string
---@field file string
---@field meta snacks.meta.Meta
---@field health? fun()
--- Get the metadata for all snacks plugins
function M.get()
local ret = {} ---@type snacks.meta.Plugin[]
local root = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":h:h")
for file, t in vim.fs.dir(root, { depth = 1 }) do
local name = vim.fn.fnamemodify(file, ":t:r")
file = t == "directory" and ("%s/init.lua"):format(file) or file
file = root .. "/" .. file
local mod = name == "init"
and setmetatable({
meta = {
desc = "Snacks",
hide = true,
docs = true,
},
}, { __index = Snacks })
or Snacks[name] --[[@as snacks.meta.Plugin]]
assert(type(mod) == "table", ("`Snacks.%s` not found"):format(name))
assert(type(mod.meta) == "table", ("`Snacks.%s.meta` not found"):format(name))
assert(type(mod.meta.desc) == "string", ("`Snacks.%s.meta.desc` not found"):format(name))
for _, prop in ipairs({ "readme", "docs", "health", "types" }) do
if mod.meta[prop] == nil then
mod.meta[prop] = not mod.meta.hide
end
end
ret[#ret + 1] = setmetatable({
name = name,
file = file,
}, {
__index = mod,
__tostring = function(self)
return "snacks." .. self.name
end,
})
end
table.sort(ret, function(a, b)
return a.name < b.name
end)
return ret
end
return M

View file

@ -11,7 +11,9 @@
---@field gitbrowse snacks.gitbrowse
---@field health snacks.health
---@field indent snacks.indent
---@field input snacks.input
---@field lazygit snacks.lazygit
---@field meta snacks.meta
---@field notifier snacks.notifier
---@field notify snacks.notify
---@field profiler snacks.profiler
@ -35,6 +37,7 @@
---@field dim? snacks.dim.Config
---@field gitbrowse? snacks.gitbrowse.Config
---@field indent? snacks.indent.Config
---@field input? snacks.input.Config
---@field lazygit? snacks.lazygit.Config
---@field notifier? snacks.notifier.Config
---@field profiler? snacks.profiler.Config