lazy.nvim/lua/lazy/view/init.lua

245 lines
6.9 KiB
Lua
Raw Normal View History

local Util = require("lazy.util")
2022-11-20 22:25:21 +00:00
local Render = require("lazy.view.render")
local Config = require("lazy.core.config")
2022-11-20 22:25:21 +00:00
local M = {}
2022-11-29 09:30:14 +00:00
M.modes = {
{ name = "home", key = "H", desc = "Go back to plugin list" },
2022-11-29 09:30:14 +00:00
{ name = "install", key = "I", desc = "Install missing plugins" },
{ name = "update", key = "U", desc = "Update all plugins. This will also update the lockfile" },
{ name = "sync", key = "S", desc = "Run install, clean and update" },
{ name = "clean", key = "X", desc = "Clean plugins that are no longer needed" },
{ name = "check", key = "C", desc = "Check for updates and show the log (git fetch)" },
{ name = "log", key = "L", desc = "Show recent updates for all plugins" },
{ name = "restore", key = "R", desc = "Updates all plugins to the state in the lockfile" },
2022-11-29 11:02:25 +00:00
{ name = "profile", key = "P", desc = "Show detailed profiling", toggle = true },
{ name = "debug", key = "D", desc = "Show debug information", toggle = true },
{ name = "help", key = "?", desc = "Toggle this help page", toggle = true },
2022-12-18 10:42:54 +00:00
{ name = "clear", desc = "Clear finished tasks", hide = true },
{
name = "load",
desc = "Load a plugin that has not been loaded yet. Similar to `:packadd`. Like `:Lazy load foo.nvim`",
hide = true,
},
{ plugin = true, name = "update", key = "u", desc = "Update this plugin. This will also update the lockfile" },
{
plugin = true,
name = "clean",
key = "x",
desc = "Delete this plugin. WARNING: this will delete the plugin even if it should be installed!",
},
{ plugin = true, name = "check", key = "c", desc = "Check for updates for this plugin and show the log (git fetch)" },
{ plugin = true, name = "install", key = "i", desc = "Install this plugin" },
{ plugin = true, name = "log", key = "gl", desc = "Show recent updates for this plugin" },
{ plugin = true, name = "restore", key = "r", desc = "Restore this plugin to the state in the lockfile" },
2022-11-29 09:30:14 +00:00
}
---@type string?
M.mode = nil
2022-11-20 22:25:21 +00:00
function M.setup()
require("lazy.view.commands").setup()
require("lazy.view.colors").setup()
end
2022-11-29 09:30:14 +00:00
function M.show(mode)
M.mode = mode or M.mode or "home"
2022-11-20 22:25:21 +00:00
require("lazy.view.colors").setup()
if M._buf and vim.api.nvim_buf_is_valid(M._buf) then
2022-12-05 19:49:23 +00:00
-- vim.api.nvim_win_set_cursor(M._win, { 1, 0 })
2022-11-29 09:30:14 +00:00
vim.cmd([[do User LazyRender]])
2022-11-20 22:25:21 +00:00
return
end
local buf = vim.api.nvim_create_buf(false, false)
M._buf = buf
local function size(max, value)
return value > 1 and math.min(value, max) or math.floor(max * value)
end
2022-11-20 22:25:21 +00:00
local opts = {
relative = "editor",
style = "minimal",
2022-11-30 22:13:08 +00:00
border = Config.options.ui.border,
width = size(vim.o.columns, Config.options.ui.size.width),
height = size(vim.o.lines, Config.options.ui.size.height),
noautocmd = true,
2022-11-20 22:25:21 +00:00
}
2022-11-20 22:25:21 +00:00
opts.row = (vim.o.lines - opts.height) / 2
opts.col = (vim.o.columns - opts.width) / 2
local win = vim.api.nvim_open_win(buf, true, opts)
M._win = win
2022-11-20 22:25:21 +00:00
vim.api.nvim_set_current_win(win)
-- it seems that setting the current win doesn't work before VimEnter,
-- so do that then
if vim.v.vim_did_enter ~= 1 then
vim.api.nvim_create_autocmd("VimEnter", {
once = true,
callback = function()
vim.api.nvim_set_current_win(win)
end,
})
end
2022-11-20 22:25:21 +00:00
vim.bo[buf].buftype = "nofile"
vim.bo[buf].filetype = "lazy"
2022-11-20 22:25:21 +00:00
vim.bo[buf].bufhidden = "wipe"
vim.wo[win].conceallevel = 3
vim.wo[win].spell = false
vim.wo[win].wrap = true
vim.wo[win].winhighlight = "Normal:LazyNormal"
local function close()
M._buf = nil
vim.diagnostic.reset(Config.ns, buf)
vim.schedule(function()
if vim.api.nvim_buf_is_valid(buf) then
vim.api.nvim_buf_delete(buf, { force = true })
end
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
end
end)
2022-11-20 22:25:21 +00:00
end
vim.keymap.set("n", "q", close, {
nowait = true,
buffer = buf,
})
2022-11-20 23:27:28 +00:00
vim.api.nvim_create_autocmd({ "BufDelete", "BufLeave", "BufHidden" }, {
2022-11-20 22:25:21 +00:00
once = true,
buffer = buf,
callback = close,
})
local render = Render.new(buf, win, 2)
2022-11-30 22:13:35 +00:00
local update = Util.throttle(Config.options.ui.throttle, function()
if buf and vim.api.nvim_buf_is_valid(buf) then
vim.bo[buf].modifiable = true
render:update()
vim.bo[buf].modifiable = false
vim.cmd.redraw()
end
2022-11-20 22:25:21 +00:00
end)
local function get_plugin()
local pos = vim.api.nvim_win_get_cursor(win)
return render:get_plugin(pos[1])
end
vim.keymap.set("n", "<cr>", function()
local plugin = get_plugin()
if plugin then
if render._details == plugin.name then
render._details = nil
else
render._details = plugin.name
end
update()
end
end, {
nowait = true,
buffer = buf,
})
local function open(path)
local plugin = get_plugin()
if plugin then
2022-12-06 10:12:54 +00:00
local url = plugin.url:gsub("%.git$", "")
if Util.file_exists(url) then
url = "https://github.com/" .. plugin[1]
end
Util.open(url .. path)
end
end
M.keys(buf, {
["%s(" .. string.rep("[a-z0-9]", 7) .. ")%s"] = function(hash)
open("/commit/" .. hash)
end,
["%s(" .. string.rep("[a-z0-9]", 7) .. ")$"] = function(hash)
open("/commit/" .. hash)
end,
["^(" .. string.rep("[a-z0-9]", 7) .. ")%s"] = function(hash)
open("/commit/" .. hash)
end,
["#(%d+)"] = function(issue)
open("/issues/" .. issue)
end,
["README.md"] = function()
local plugin = get_plugin()
Util.open(plugin.dir .. "/README.md")
end,
["|(%S-)|"] = vim.cmd.help, -- vim help links
["(https?://%S+)"] = function(url)
Util.open(url)
end,
})
2022-11-29 09:30:14 +00:00
for _, m in ipairs(M.modes) do
2022-12-18 10:42:54 +00:00
if m.key then
vim.keymap.set("n", m.key, function()
local Commands = require("lazy.view.commands")
if m.plugin then
local plugin = get_plugin()
if plugin then
Commands.cmd(m.name, { plugin })
end
else
if M.mode == m.name and m.toggle then
M.mode = nil
return update()
end
Commands.cmd(m.name)
2022-11-29 11:02:25 +00:00
end
2022-12-18 10:42:54 +00:00
end, { buffer = buf })
end
2022-11-29 09:30:14 +00:00
end
2022-11-20 22:25:21 +00:00
vim.api.nvim_create_autocmd("User", {
pattern = "LazyRender",
callback = function()
if not vim.api.nvim_buf_is_valid(buf) then
return true
end
update()
2022-11-20 22:25:21 +00:00
end,
})
update()
end
---@param handlers table<string, fun(str:string)>
function M.keys(buf, handlers)
local function map(lhs)
vim.keymap.set("n", lhs, function()
local line = vim.api.nvim_get_current_line()
local pos = vim.api.nvim_win_get_cursor(0)
local col = pos[2] + 1
for pattern, handler in pairs(handlers) do
local from = 1
local to, url
while from do
from, to, url = line:find(pattern, from)
if from and col >= from and col <= to then
return handler(url)
end
if from then
from = to + 1
end
end
end
end, { buffer = buf, silent = true })
end
map("K")
2022-11-20 22:25:21 +00:00
end
return M