refactor: auto-gen plugin types

This commit is contained in:
Folke Lemaitre 2024-12-07 21:29:35 +01:00
parent 9fb88c67b6
commit 0194f18cfa
6 changed files with 118 additions and 48 deletions

View file

@ -3,6 +3,7 @@
local M = {}
---@class snacks.bigfile.Config
---@field enabled? boolean
local defaults = {
notify = true, -- show notification when big file detected
size = 1.5 * 1024 * 1024, -- 1.5MB

View file

@ -66,6 +66,7 @@ math.randomseed(os.time())
---@field width number
---@class snacks.dashboard.Config
---@field enabled? boolean
---@field sections snacks.dashboard.Section
---@field formats table<string, snacks.dashboard.Text|fun(item:snacks.dashboard.Item, ctx:snacks.dashboard.Format.ctx):snacks.dashboard.Text>
local defaults = {

View file

@ -366,27 +366,79 @@ function M.write(name, lines)
end
function M._build()
local plugins = {} ---@type {name:string, config:boolean}[]
local skip = { "docs", "health" }
local files = {} ---@type table<string, string>
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
print(name .. ".md")
local path = ("lua/snacks/%s"):format(file)
local lines = vim.fn.readfile(path)
local info = M.extract(lines)
M.write(name, M.render(name, info))
if name == "init" then
local readme = table.concat(vim.fn.readfile("README.md"), "\n")
local example = table.concat(vim.fn.readfile("docs/examples/init.lua"), "\n")
example = example:gsub(".*\nreturn {", "{", 1)
readme = M.replace("config", readme, M.md(info.config))
readme = M.replace("example", readme, M.md(example))
vim.fn.writefile(vim.split(readme, "\n"), "README.md")
end
files[name] = file
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)
local info = M.extract(lines)
local readme = table.concat(vim.fn.readfile("README.md"), "\n")
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
local line = ("---@field %s? snacks.%s.Config"):format(plugin.name, plugin.name)
table.insert(lines, line)
end
end
local config_lines = vim.split(info.config or "", "\n")
table.remove(config_lines, 1)
vim.list_extend(lines, config_lines)
info.config = table.concat(lines, "\n")
M.write("init", M.render("init", info))
example = example:gsub(".*\nreturn {", "{", 1)
readme = M.replace("config", readme, M.md(info.config))
readme = M.replace("example", readme, M.md(example))
vim.fn.writefile(vim.split(readme, "\n"), "README.md")
end
vim.cmd.checktime()
-- types.lua
local lines = {} ---@type string[]
lines[#lines + 1] = "---@meta _"
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)
end
lines[#lines + 1] = ""
lines[#lines + 1] = "---@class snacks.plugins.Config"
for _, plugin in ipairs(plugins) do
if plugin.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")
end
function M.fix_titles()

View file

@ -1,25 +1,4 @@
---@class Snacks
---@field bigfile snacks.bigfile
---@field bufdelete snacks.bufdelete
---@field config snacks.config
---@field dashboard snacks.dashboard
---@field debug snacks.debug
---@field git snacks.git
---@field gitbrowse snacks.gitbrowse
---@field lazygit snacks.lazygit
---@field notifier snacks.notifier
---@field notify snacks.notify
---@field quickfile snacks.quickfile
---@field health snacks.health
---@field profiler snacks.profiler
---@field rename snacks.rename
---@field scratch snacks.scratch
---@field statuscolumn snacks.statuscolumn
---@field terminal snacks.terminal
---@field toggle snacks.toggle
---@field util snacks.util
---@field win snacks.win
---@field words snacks.words
---@class Snacks: snacks.plugins
local M = {}
setmetatable(M, {
@ -36,26 +15,15 @@ _G.Snacks = M
---@field example? string
---@field config? fun(opts: table, defaults: table)
---@class snacks.Config
---@field bigfile? snacks.bigfile.Config | { enabled: boolean }
---@field gitbrowse? snacks.gitbrowse.Config
---@field lazygit? snacks.lazygit.Config
---@field notifier? snacks.notifier.Config | { enabled: boolean }
---@field quickfile? { enabled: boolean }
---@field statuscolumn? snacks.statuscolumn.Config | { enabled: boolean }
---@class snacks.Config: snacks.plugins.Config
---@field styles? table<string, snacks.win.Config>
---@field dashboard? snacks.dashboard.Config | { enabled: boolean }
---@field terminal? snacks.terminal.Config
---@field toggle? snacks.toggle.Config
---@field win? snacks.win.Config
---@field words? snacks.words.Config
local config = {
styles = {},
bigfile = { enabled = false },
dashboard = { enabled = false },
notifier = { enabled = false },
quickfile = { enabled = false },
statuscolumn = { enabled = false },
styles = {},
words = { enabled = false },
}

View file

@ -100,6 +100,7 @@ Snacks.config.style("notification.history", {
})
---@class snacks.notifier.Config
---@field enabled? boolean
---@field keep? fun(notif: snacks.notifier.Notif): boolean # global keep function
local defaults = {
timeout = 3000, -- default timeout in ms

47
lua/snacks/types.lua Normal file
View file

@ -0,0 +1,47 @@
---@meta _
---@class snacks.plugins
---@field animate snacks.animate
---@field bigfile snacks.bigfile
---@field bufdelete snacks.bufdelete
---@field dashboard snacks.dashboard
---@field debug snacks.debug
---@field dim snacks.dim
---@field git snacks.git
---@field gitbrowse snacks.gitbrowse
---@field indent snacks.indent
---@field lazygit snacks.lazygit
---@field notifier snacks.notifier
---@field notify snacks.notify
---@field profiler snacks.profiler
---@field quickfile snacks.quickfile
---@field rename snacks.rename
---@field scope snacks.scope
---@field scratch snacks.scratch
---@field statuscolumn snacks.statuscolumn
---@field terminal snacks.terminal
---@field toggle snacks.toggle
---@field util snacks.util
---@field win snacks.win
---@field words snacks.words
---@field zen snacks.zen
---@class snacks.plugins.Config
---@field animate? snacks.animate.Config
---@field bigfile? snacks.bigfile.Config
---@field dashboard? snacks.dashboard.Config
---@field dim? snacks.dim.Config
---@field gitbrowse? snacks.gitbrowse.Config
---@field indent? snacks.indent.Config
---@field lazygit? snacks.lazygit.Config
---@field notifier? snacks.notifier.Config
---@field profiler? snacks.profiler.Config
---@field quickfile? snacks.quickfile.Config
---@field scope? snacks.scope.Config
---@field scratch? snacks.scratch.Config
---@field statuscolumn? snacks.statuscolumn.Config
---@field terminal? snacks.terminal.Config
---@field toggle? snacks.toggle.Config
---@field win? snacks.win.Config
---@field words? snacks.words.Config
---@field zen? snacks.zen.Config