2022-12-15 21:08:04 +08:00
|
|
|
local Util = require("lazy.util")
|
2022-12-15 07:22:40 +08:00
|
|
|
|
2022-12-15 21:08:04 +08:00
|
|
|
local M = {}
|
2022-12-15 07:22:40 +08:00
|
|
|
|
|
|
|
function M.indent(str, indent)
|
|
|
|
local lines = vim.split(str, "\n")
|
|
|
|
for l, line in ipairs(lines) do
|
|
|
|
lines[l] = (" "):rep(indent) .. line
|
|
|
|
end
|
|
|
|
return table.concat(lines, "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param str string
|
|
|
|
function M.fix_indent(str)
|
|
|
|
local lines = vim.split(str, "\n")
|
|
|
|
|
2023-01-20 05:08:58 +08:00
|
|
|
local first = table.remove(lines, 1)
|
|
|
|
|
2022-12-15 07:22:40 +08:00
|
|
|
local width = 120
|
|
|
|
for _, line in ipairs(lines) do
|
2023-01-20 05:08:58 +08:00
|
|
|
if not line:find("^%s*$") then
|
|
|
|
width = math.min(width, #line:match("^%s*"))
|
|
|
|
end
|
2022-12-15 07:22:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
for l, line in ipairs(lines) do
|
2022-12-21 18:43:05 +08:00
|
|
|
lines[l] = line:sub(width + 1)
|
2022-12-15 07:22:40 +08:00
|
|
|
end
|
2023-01-20 05:08:58 +08:00
|
|
|
table.insert(lines, 1, first)
|
2022-12-15 07:22:40 +08:00
|
|
|
return table.concat(lines, "\n")
|
|
|
|
end
|
|
|
|
|
2023-01-09 16:47:23 +08:00
|
|
|
---@alias ReadmeBlock {content:string, lang?:string}
|
|
|
|
---@param contents table<string, ReadmeBlock|string>
|
2023-01-20 05:08:58 +08:00
|
|
|
---@param readme_file? string
|
|
|
|
function M.save(contents, readme_file)
|
|
|
|
local readme = Util.read_file(readme_file or "README.md")
|
2023-01-09 16:47:23 +08:00
|
|
|
for tag, block in pairs(contents) do
|
|
|
|
if type(block) == "string" then
|
|
|
|
block = { content = block, lang = "lua" }
|
|
|
|
end
|
|
|
|
---@cast block ReadmeBlock
|
|
|
|
local content = M.fix_indent(block.content)
|
2022-12-15 07:22:40 +08:00
|
|
|
content = content:gsub("%%", "%%%%")
|
|
|
|
content = vim.trim(content)
|
2022-12-15 15:46:25 +08:00
|
|
|
local pattern = "(<%!%-%- " .. tag .. ":start %-%->).*(<%!%-%- " .. tag .. ":end %-%->)"
|
2022-12-15 07:22:40 +08:00
|
|
|
if not readme:find(pattern) then
|
|
|
|
error("tag " .. tag .. " not found")
|
|
|
|
end
|
2023-01-09 16:47:23 +08:00
|
|
|
if block.lang then
|
|
|
|
readme = readme:gsub(pattern, "%1\n\n```" .. block.lang .. "\n" .. content .. "\n```\n\n%2")
|
2022-12-15 15:46:25 +08:00
|
|
|
else
|
2023-01-09 16:47:23 +08:00
|
|
|
readme = readme:gsub(pattern, "%1\n\n" .. content .. "\n\n%2")
|
2022-12-15 15:46:25 +08:00
|
|
|
end
|
2022-12-15 07:22:40 +08:00
|
|
|
end
|
|
|
|
|
2023-01-20 05:08:58 +08:00
|
|
|
Util.write_file(readme_file or "README.md", readme)
|
2023-01-09 16:47:23 +08:00
|
|
|
vim.cmd.checktime()
|
2022-12-15 07:22:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@return string
|
|
|
|
function M.extract(file, pattern)
|
2022-12-15 21:08:04 +08:00
|
|
|
local init = Util.read_file(file)
|
2022-12-15 07:22:40 +08:00
|
|
|
return assert(init:match(pattern))
|
|
|
|
end
|
|
|
|
|
2023-01-09 16:47:23 +08:00
|
|
|
---@return ReadmeBlock
|
2022-12-18 18:42:54 +08:00
|
|
|
function M.commands()
|
|
|
|
local commands = require("lazy.view.commands").commands
|
2022-12-24 03:55:49 +08:00
|
|
|
local modes = require("lazy.view.config").commands
|
2022-12-22 05:27:36 +08:00
|
|
|
modes.load.opts = true
|
2022-12-18 18:42:54 +08:00
|
|
|
local lines = {
|
2022-12-19 22:20:38 +08:00
|
|
|
{ "Command", "Lua", "Description" },
|
2022-12-18 18:42:54 +08:00
|
|
|
{ "---", "---", "---", "---" },
|
|
|
|
}
|
2022-12-22 05:27:36 +08:00
|
|
|
Util.foreach(modes, function(name, mode)
|
|
|
|
if commands[name] then
|
2022-12-24 03:55:49 +08:00
|
|
|
if mode.plugins_required then
|
2022-12-19 22:20:38 +08:00
|
|
|
lines[#lines + 1] = {
|
2022-12-22 05:27:36 +08:00
|
|
|
("`:Lazy %s {plugins}`"):format(name),
|
|
|
|
([[`require("lazy").%s(opts)`]]):format(name),
|
2022-12-19 22:20:38 +08:00
|
|
|
mode.desc,
|
|
|
|
}
|
2022-12-24 03:55:49 +08:00
|
|
|
elseif mode.plugins then
|
2022-12-19 22:20:38 +08:00
|
|
|
lines[#lines + 1] = {
|
2022-12-22 05:27:36 +08:00
|
|
|
("`:Lazy %s [plugins]`"):format(name),
|
|
|
|
([[`require("lazy").%s(opts?)`]]):format(name),
|
2022-12-19 22:20:38 +08:00
|
|
|
mode.desc,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lines[#lines + 1] = {
|
2022-12-22 05:27:36 +08:00
|
|
|
("`:Lazy %s`"):format(name),
|
|
|
|
([[`require("lazy").%s()`]]):format(name),
|
2022-12-19 22:20:38 +08:00
|
|
|
mode.desc,
|
|
|
|
}
|
|
|
|
end
|
2022-12-18 18:42:54 +08:00
|
|
|
end
|
2022-12-22 05:27:36 +08:00
|
|
|
end)
|
2023-01-09 16:47:23 +08:00
|
|
|
return { content = M.table(lines) }
|
2022-12-26 16:37:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param lines string[][]
|
|
|
|
function M.table(lines)
|
|
|
|
---@type string[]
|
2022-12-18 18:42:54 +08:00
|
|
|
local ret = {}
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
ret[#ret + 1] = "| " .. table.concat(line, " | ") .. " |"
|
|
|
|
end
|
|
|
|
return table.concat(ret, "\n")
|
|
|
|
end
|
|
|
|
|
2023-01-09 16:47:23 +08:00
|
|
|
---@return ReadmeBlock
|
2022-12-26 16:37:26 +08:00
|
|
|
function M.colors()
|
|
|
|
local str = M.extract("lua/lazy/view/colors.lua", "\nM%.colors = ({.-\n})")
|
|
|
|
---@type table<string,string>
|
|
|
|
local comments = {}
|
|
|
|
for _, line in ipairs(vim.split(str, "\n")) do
|
|
|
|
local group, desc = line:match("^ (%w+) = .* -- (.*)")
|
|
|
|
if group then
|
|
|
|
comments[group] = desc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local lines = {
|
|
|
|
{ "Highlight Group", "Default Group", "Description" },
|
|
|
|
{ "---", "---", "---" },
|
|
|
|
}
|
|
|
|
Util.foreach(require("lazy.view.colors").colors, function(group, link)
|
|
|
|
lines[#lines + 1] = { "**Lazy" .. group .. "**", "***" .. link .. "***", comments[group] or "" }
|
|
|
|
end)
|
2023-01-09 16:47:23 +08:00
|
|
|
return { content = M.table(lines) }
|
2022-12-26 16:37:26 +08:00
|
|
|
end
|
|
|
|
|
2022-12-15 07:22:40 +08:00
|
|
|
function M.update()
|
|
|
|
local config = M.extract("lua/lazy/core/config.lua", "\nM%.defaults = ({.-\n})")
|
|
|
|
config = config:gsub("%s*debug = false.\n", "\n")
|
|
|
|
M.save({
|
|
|
|
bootstrap = M.extract("lua/lazy/init.lua", "function M%.bootstrap%(%)\n(.-)\nend"),
|
2022-12-27 20:34:07 +08:00
|
|
|
stats = M.extract("lua/lazy/stats.lua", "\nM%._stats = ({.-\n})"),
|
2022-12-15 07:22:40 +08:00
|
|
|
config = config,
|
2022-12-15 21:08:04 +08:00
|
|
|
spec = Util.read_file("lua/lazy/example.lua"),
|
2022-12-18 18:42:54 +08:00
|
|
|
commands = M.commands(),
|
2022-12-26 16:37:26 +08:00
|
|
|
colors = M.colors(),
|
2022-12-15 07:22:40 +08:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-01-09 16:47:23 +08:00
|
|
|
---@param plugins? LazyPlugin[]
|
|
|
|
---@return ReadmeBlock
|
|
|
|
function M.plugins(plugins)
|
|
|
|
plugins = plugins or require("lazy.core.config").plugins
|
|
|
|
---@type string[]
|
2023-01-09 15:05:46 +08:00
|
|
|
local lines = {}
|
2023-01-09 16:47:23 +08:00
|
|
|
Util.foreach(plugins, function(name, plugin)
|
2022-12-31 06:58:01 +08:00
|
|
|
if plugin.url then
|
|
|
|
lines[#lines + 1] = "- [" .. name .. "](" .. plugin.url:gsub("%.git$", "") .. ")"
|
|
|
|
end
|
|
|
|
end)
|
2023-01-09 16:47:23 +08:00
|
|
|
return { content = table.concat(lines, "\n") }
|
2022-12-31 06:58:01 +08:00
|
|
|
end
|
2022-12-15 07:22:40 +08:00
|
|
|
|
|
|
|
return M
|