2022-11-24 21:04:23 +00:00
|
|
|
local Util = require("lazy.util")
|
2022-11-20 22:25:21 +00:00
|
|
|
local Render = require("lazy.view.render")
|
2022-11-25 21:48:37 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2022-12-23 19:55:49 +00:00
|
|
|
local ViewConfig = require("lazy.view.config")
|
2022-12-23 21:39:16 +00:00
|
|
|
local Git = require("lazy.manage.git")
|
2022-11-20 22:25:21 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@class LazyViewState
|
|
|
|
---@field mode string
|
|
|
|
---@field plugin? string
|
2022-12-23 09:43:22 +00:00
|
|
|
local default_state = {
|
|
|
|
mode = "home",
|
|
|
|
profile = {
|
|
|
|
threshold = 0,
|
2022-12-23 10:47:05 +00:00
|
|
|
sort_time_taken = false,
|
2022-12-23 09:43:22 +00:00
|
|
|
},
|
|
|
|
}
|
2022-12-23 09:18:19 +00:00
|
|
|
|
|
|
|
---@class LazyView
|
|
|
|
---@field buf number
|
|
|
|
---@field win number
|
|
|
|
---@field render LazyRender
|
|
|
|
---@field state LazyViewState
|
|
|
|
---@field win_opts LazyViewWinOpts
|
2022-11-20 22:25:21 +00:00
|
|
|
local M = {}
|
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@type LazyView
|
|
|
|
M.view = nil
|
2022-11-29 09:30:14 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@param mode? string
|
2022-11-29 09:30:14 +00:00
|
|
|
function M.show(mode)
|
2022-12-21 21:28:09 +00:00
|
|
|
if Config.headless then
|
|
|
|
return
|
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
|
|
|
|
M.view = M.view or M.create({ mode = mode })
|
|
|
|
M.view:update(mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param opts? {mode?:string}
|
|
|
|
function M.create(opts)
|
2022-11-20 22:25:21 +00:00
|
|
|
require("lazy.view.colors").setup()
|
2022-12-23 09:18:19 +00:00
|
|
|
opts = opts or {}
|
|
|
|
local self = setmetatable({}, { __index = M })
|
2022-11-20 22:25:21 +00:00
|
|
|
|
2022-12-23 09:43:22 +00:00
|
|
|
self.state = vim.deepcopy(default_state)
|
2022-12-23 09:18:19 +00:00
|
|
|
|
|
|
|
self:mount()
|
|
|
|
|
|
|
|
self.render = Render.new(self)
|
|
|
|
self.update = Util.throttle(Config.options.ui.throttle, self.update)
|
|
|
|
|
2022-12-23 19:55:49 +00:00
|
|
|
self:on_key(ViewConfig.keys.close, self.close)
|
2022-12-23 09:18:19 +00:00
|
|
|
|
|
|
|
self:on({ "BufDelete", "BufLeave", "BufHidden" }, self.close, { once = true })
|
|
|
|
|
|
|
|
self:on("User LazyRender", function()
|
|
|
|
if not (self.buf and vim.api.nvim_buf_is_valid(self.buf)) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
self:update()
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- plugin details
|
2022-12-23 19:55:49 +00:00
|
|
|
self:on_key(ViewConfig.keys.details, function()
|
2022-12-23 09:18:19 +00:00
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
self.state.plugin = self.state.plugin ~= plugin.name and plugin.name or nil
|
|
|
|
self:update()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-12-23 19:55:49 +00:00
|
|
|
self:on_key(ViewConfig.keys.profile_sort, function()
|
2022-12-23 09:43:22 +00:00
|
|
|
if self.state.mode == "profile" then
|
|
|
|
self.state.profile.sort_time_taken = not self.state.profile.sort_time_taken
|
|
|
|
self:update()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-12-23 19:55:49 +00:00
|
|
|
self:on_key(ViewConfig.keys.profile_filter, function()
|
2022-12-23 09:43:22 +00:00
|
|
|
if self.state.mode == "profile" then
|
|
|
|
vim.ui.input({
|
|
|
|
prompt = "Enter time threshold in ms, like 0.5",
|
|
|
|
default = tostring(self.state.profile.threshold),
|
|
|
|
}, function(input)
|
|
|
|
if not input then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local num = input == "" and 0 or tonumber(input)
|
|
|
|
if not num then
|
|
|
|
Util.error("Please input a number")
|
|
|
|
else
|
|
|
|
self.state.profile.threshold = num
|
|
|
|
self:update()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-12-23 21:39:16 +00:00
|
|
|
self:setup_patterns()
|
2022-12-23 09:18:19 +00:00
|
|
|
self:setup_modes()
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param events string|string[]
|
|
|
|
---@param fn fun(self?):boolean?
|
|
|
|
---@param opts? table
|
|
|
|
function M:on(events, fn, opts)
|
|
|
|
if type(events) == "string" then
|
|
|
|
events = { events }
|
|
|
|
end
|
|
|
|
for _, e in ipairs(events) do
|
|
|
|
local event, pattern = e:match("(%w+) (%w+)")
|
|
|
|
event = event or e
|
|
|
|
vim.api.nvim_create_autocmd(
|
|
|
|
event,
|
|
|
|
vim.tbl_extend("force", {
|
|
|
|
pattern = pattern,
|
|
|
|
buffer = not pattern and self.buf or nil,
|
|
|
|
callback = function()
|
|
|
|
return fn(self)
|
|
|
|
end,
|
|
|
|
}, opts or {})
|
|
|
|
)
|
2022-11-20 22:25:21 +00:00
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
end
|
2022-11-20 22:25:21 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@param key string
|
|
|
|
---@param fn fun(self?)
|
2022-12-23 19:55:49 +00:00
|
|
|
---@param desc? string
|
|
|
|
function M:on_key(key, fn, desc)
|
2022-12-23 09:18:19 +00:00
|
|
|
vim.keymap.set("n", key, function()
|
|
|
|
fn(self)
|
|
|
|
end, {
|
|
|
|
nowait = true,
|
|
|
|
buffer = self.buf,
|
2022-12-23 19:55:49 +00:00
|
|
|
desc = desc,
|
2022-12-23 09:18:19 +00:00
|
|
|
})
|
|
|
|
end
|
2022-12-20 18:29:28 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@param mode? string
|
|
|
|
function M:update(mode)
|
|
|
|
if mode then
|
|
|
|
self.state.mode = mode
|
2022-12-20 18:29:28 +00:00
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
if self.buf and vim.api.nvim_buf_is_valid(self.buf) then
|
|
|
|
vim.bo[self.buf].modifiable = true
|
|
|
|
self.render:update()
|
|
|
|
vim.bo[self.buf].modifiable = false
|
|
|
|
vim.cmd.redraw()
|
|
|
|
end
|
|
|
|
end
|
2022-12-20 18:29:28 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
function M:open_url(path)
|
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
if plugin.url then
|
|
|
|
local url = plugin.url:gsub("%.git$", "")
|
|
|
|
Util.open(url .. path)
|
|
|
|
else
|
|
|
|
Util.error("No url for " .. plugin.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:close()
|
|
|
|
local buf = self.buf
|
|
|
|
local win = self.win
|
|
|
|
self.win = nil
|
|
|
|
self.buf = nil
|
|
|
|
M.view = nil
|
|
|
|
vim.diagnostic.reset(Config.ns, buf)
|
|
|
|
vim.schedule(function()
|
|
|
|
if win and vim.api.nvim_win_is_valid(win) then
|
|
|
|
vim.api.nvim_win_close(win, true)
|
|
|
|
end
|
|
|
|
if buf and vim.api.nvim_buf_is_valid(buf) then
|
|
|
|
vim.api.nvim_buf_delete(buf, { force = true })
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:focus()
|
|
|
|
vim.api.nvim_set_current_win(self.win)
|
2022-11-20 22:25:21 +00:00
|
|
|
|
2022-12-19 11:20:02 +00:00
|
|
|
-- 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()
|
2022-12-23 09:18:19 +00:00
|
|
|
if self.win and vim.api.nvim_win_is_valid(self.win) then
|
|
|
|
pcall(vim.api.nvim_set_current_win, self.win)
|
2022-12-21 07:33:10 +00:00
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
return true
|
2022-12-19 11:20:02 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
end
|
2022-12-19 11:20:02 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
function M:mount()
|
|
|
|
self.buf = vim.api.nvim_create_buf(false, false)
|
2022-11-20 22:25:21 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
local function size(max, value)
|
|
|
|
return value > 1 and math.min(value, max) or math.floor(max * value)
|
2022-11-23 15:12:02 +00:00
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
---@class LazyViewWinOpts
|
|
|
|
self.win_opts = {
|
|
|
|
relative = "editor",
|
|
|
|
style = "minimal",
|
|
|
|
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-23 15:12:02 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
self.win_opts.row = (vim.o.lines - self.win_opts.height) / 2
|
|
|
|
self.win_opts.col = (vim.o.columns - self.win_opts.width) / 2
|
|
|
|
self.win = vim.api.nvim_open_win(self.buf, true, self.win_opts)
|
|
|
|
self:focus()
|
2022-11-23 15:12:02 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
vim.bo[self.buf].buftype = "nofile"
|
|
|
|
vim.bo[self.buf].filetype = "lazy"
|
|
|
|
vim.bo[self.buf].bufhidden = "wipe"
|
|
|
|
vim.wo[self.win].conceallevel = 3
|
|
|
|
vim.wo[self.win].spell = false
|
|
|
|
vim.wo[self.win].wrap = true
|
|
|
|
vim.wo[self.win].winhighlight = "Normal:LazyNormal"
|
|
|
|
end
|
2022-11-23 15:12:02 +00:00
|
|
|
|
2022-12-23 21:39:16 +00:00
|
|
|
function M:setup_patterns()
|
|
|
|
self:on_pattern(ViewConfig.keys.hover, {
|
|
|
|
["%f[a-z0-9](" .. string.rep("[a-z0-9]", 7) .. ")%f[^a-z0-9]"] = function(hash)
|
|
|
|
self:diff({ commit = hash, browser = true })
|
2022-11-23 15:12:02 +00:00
|
|
|
end,
|
|
|
|
["#(%d+)"] = function(issue)
|
2022-12-23 09:18:19 +00:00
|
|
|
self:open_url("/issues/" .. issue)
|
2022-11-23 15:12:02 +00:00
|
|
|
end,
|
|
|
|
["README.md"] = function()
|
2022-12-23 09:18:19 +00:00
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
Util.open(plugin.dir .. "/README.md")
|
|
|
|
end
|
2022-11-23 15:12:02 +00:00
|
|
|
end,
|
2022-12-03 20:07:11 +00:00
|
|
|
["|(%S-)|"] = vim.cmd.help, -- vim help links
|
2022-11-23 15:12:02 +00:00
|
|
|
["(https?://%S+)"] = function(url)
|
|
|
|
Util.open(url)
|
|
|
|
end,
|
2022-12-23 21:39:16 +00:00
|
|
|
}, self.hover)
|
|
|
|
function M:hover()
|
|
|
|
if self:diff({ browser = true }) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self:open_url("")
|
|
|
|
end
|
|
|
|
|
|
|
|
---@alias LazyDiff string|{from:string, to:string}
|
|
|
|
|
|
|
|
---@param opts? {commit?:string, browser:boolean}
|
|
|
|
function M:diff(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
local diff
|
|
|
|
if opts.commit then
|
|
|
|
diff = opts.commit
|
|
|
|
elseif plugin._.updated then
|
|
|
|
diff = plugin._.updated
|
|
|
|
else
|
|
|
|
local info = assert(Git.info(plugin.dir))
|
|
|
|
local target = assert(Git.get_target(plugin))
|
|
|
|
diff = { from = info.commit, to = target.commit }
|
|
|
|
end
|
|
|
|
|
|
|
|
if not diff then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.browser then
|
|
|
|
if plugin.url then
|
|
|
|
local url = plugin.url:gsub("%.git$", "")
|
|
|
|
if type(diff) == "string" then
|
|
|
|
Util.open(url .. "/commit/" .. diff)
|
|
|
|
else
|
|
|
|
Util.open(url .. "/compare/" .. diff.from .. ".." .. diff.to)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Util.error("No url for " .. plugin.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
|
2022-12-23 21:39:16 +00:00
|
|
|
--- will create a key mapping that can be used on certain patterns
|
|
|
|
---@param key string
|
|
|
|
---@param patterns table<string, fun(str:string)>
|
|
|
|
---@param fallback? fun(self)
|
|
|
|
function M:on_pattern(key, patterns, fallback)
|
|
|
|
self:on_key(key, function()
|
2022-12-23 09:18:19 +00:00
|
|
|
local line = vim.api.nvim_get_current_line()
|
|
|
|
local pos = vim.api.nvim_win_get_cursor(0)
|
|
|
|
local col = pos[2] + 1
|
|
|
|
|
2022-12-23 21:39:16 +00:00
|
|
|
for pattern, handler in pairs(patterns) do
|
2022-12-23 09:18:19 +00:00
|
|
|
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
|
2022-12-23 21:39:16 +00:00
|
|
|
if fallback then
|
|
|
|
fallback(self)
|
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
end)
|
|
|
|
end
|
2022-11-23 15:12:02 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
function M:setup_modes()
|
2022-12-23 19:55:49 +00:00
|
|
|
local Commands = require("lazy.view.commands")
|
|
|
|
for name, m in pairs(ViewConfig.commands) do
|
2022-12-18 10:42:54 +00:00
|
|
|
if m.key then
|
2022-12-23 09:18:19 +00:00
|
|
|
self:on_key(m.key, function()
|
2022-12-23 19:55:49 +00:00
|
|
|
if self.state.mode == name and m.toggle then
|
|
|
|
return self:update("home")
|
2022-11-29 11:02:25 +00:00
|
|
|
end
|
2022-12-23 19:55:49 +00:00
|
|
|
Commands.cmd(name)
|
|
|
|
end, m.desc)
|
|
|
|
end
|
|
|
|
if m.key_plugin then
|
|
|
|
self:on_key(m.key_plugin, function()
|
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
Commands.cmd(name, { plugins = { plugin } })
|
|
|
|
end
|
|
|
|
end, m.desc_plugin)
|
2022-12-18 10:42:54 +00:00
|
|
|
end
|
2022-11-29 09:30:14 +00:00
|
|
|
end
|
2022-11-20 22:25:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|