2022-11-25 21:48:37 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2022-12-24 10:26:09 +00:00
|
|
|
local Diff = require("lazy.view.diff")
|
|
|
|
local Float = require("lazy.view.float")
|
2023-10-09 10:25:42 +01:00
|
|
|
local Git = require("lazy.manage.git")
|
|
|
|
local Render = require("lazy.view.render")
|
|
|
|
local Util = require("lazy.util")
|
|
|
|
local ViewConfig = require("lazy.view.config")
|
2022-11-20 22:25:21 +00:00
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@class LazyViewState
|
|
|
|
---@field mode string
|
2022-12-25 20:07:30 +00:00
|
|
|
---@field plugin? {name:string, kind?: LazyPluginKind}
|
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
|
|
|
|
2022-12-24 10:26:09 +00:00
|
|
|
---@class LazyView: LazyFloat
|
2022-12-23 09:18:19 +00:00
|
|
|
---@field render LazyRender
|
|
|
|
---@field state LazyViewState
|
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
|
|
|
|
2023-01-05 15:52:02 +00:00
|
|
|
function M.visible()
|
|
|
|
return M.view and M.view.win and vim.api.nvim_win_is_valid(M.view.win)
|
|
|
|
end
|
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@param mode? string
|
2022-11-29 09:30:14 +00:00
|
|
|
function M.show(mode)
|
2023-01-07 08:01:45 +00:00
|
|
|
if Config.headless() then
|
2022-12-21 21:28:09 +00:00
|
|
|
return
|
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
|
2023-01-05 15:52:02 +00:00
|
|
|
M.view = M.visible() and M.view or M.create()
|
2022-12-24 10:26:09 +00:00
|
|
|
if mode then
|
|
|
|
M.view.state.mode = mode
|
|
|
|
end
|
|
|
|
M.view:update()
|
2022-12-23 09:18:19 +00:00
|
|
|
end
|
|
|
|
|
2022-12-25 20:07:30 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M:is_selected(plugin)
|
|
|
|
return vim.deep_equal(self.state.plugin, { name = plugin.name, kind = plugin._.kind })
|
|
|
|
end
|
|
|
|
|
2022-12-25 12:45:56 +00:00
|
|
|
function M.create()
|
2022-12-24 10:26:09 +00:00
|
|
|
local self = setmetatable({}, { __index = setmetatable(M, { __index = Float }) })
|
|
|
|
---@cast self LazyView
|
2023-05-27 13:28:09 +01:00
|
|
|
Float.init(self, {
|
|
|
|
title = Config.options.ui.title,
|
|
|
|
title_pos = Config.options.ui.title_pos,
|
2024-03-27 07:48:55 +00:00
|
|
|
noautocmd = false,
|
2023-05-27 13:28:09 +01:00
|
|
|
})
|
2022-12-24 10:26:09 +00:00
|
|
|
|
2023-01-19 07:45:27 +00:00
|
|
|
if Config.options.ui.wrap then
|
2023-06-12 07:28:10 +01:00
|
|
|
Util.wo(self.win, "wrap", true)
|
|
|
|
Util.wo(self.win, "linebreak", true)
|
|
|
|
Util.wo(self.win, "breakindent", true)
|
2023-01-19 07:45:27 +00:00
|
|
|
else
|
2023-06-12 07:28:10 +01:00
|
|
|
Util.wo(self.win, "wrap", false)
|
2023-01-19 07:45:27 +00:00
|
|
|
end
|
|
|
|
|
2022-12-23 09:43:22 +00:00
|
|
|
self.state = vim.deepcopy(default_state)
|
2022-12-23 09:18:19 +00:00
|
|
|
|
|
|
|
self.render = Render.new(self)
|
2024-06-26 13:28:22 +01:00
|
|
|
local update = self.update
|
|
|
|
self.update = Util.throttle(Config.options.ui.throttle, function()
|
|
|
|
update(self)
|
|
|
|
end)
|
2022-12-23 09:18:19 +00:00
|
|
|
|
2024-03-27 07:48:55 +00:00
|
|
|
for _, pattern in ipairs({ "LazyRender", "LazyFloatResized" }) do
|
|
|
|
self:on({ "User" }, function()
|
|
|
|
if not (self.buf and vim.api.nvim_buf_is_valid(self.buf)) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
self:update()
|
|
|
|
end, { pattern = pattern })
|
|
|
|
end
|
2022-12-23 09:18:19 +00:00
|
|
|
|
2022-12-31 09:38:03 +00:00
|
|
|
vim.keymap.set("n", ViewConfig.keys.abort, function()
|
|
|
|
require("lazy.manage.process").abort()
|
2023-02-17 12:41:16 +00:00
|
|
|
return ViewConfig.keys.abort
|
2022-12-31 09:38:03 +00:00
|
|
|
end, { silent = true, buffer = self.buf, expr = true })
|
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
-- 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
|
2022-12-25 20:07:30 +00:00
|
|
|
local selected = {
|
|
|
|
name = plugin.name,
|
|
|
|
kind = plugin._.kind,
|
|
|
|
}
|
|
|
|
self.state.plugin = not vim.deep_equal(self.state.plugin, selected) and selected or nil
|
2022-12-23 09:18:19 +00:00
|
|
|
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({
|
2022-12-28 16:38:08 +00:00
|
|
|
prompt = "Enter time threshold in ms: ",
|
2022-12-23 09:43:22 +00:00
|
|
|
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)
|
|
|
|
|
2023-10-12 06:18:53 +01:00
|
|
|
for lhs, rhs in pairs(Config.options.ui.custom_keys) do
|
|
|
|
if rhs then
|
|
|
|
local handler = type(rhs) == "table" and rhs[1] or rhs
|
|
|
|
self:on_key(lhs, function()
|
2022-12-24 10:28:19 +00:00
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
handler(plugin)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2022-12-24 10:26:09 +00:00
|
|
|
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
|
|
|
|
|
2022-12-24 10:26:09 +00:00
|
|
|
function M:update()
|
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
|
|
|
|
|
2022-12-23 21:39:16 +00:00
|
|
|
function M:setup_patterns()
|
2023-08-26 15:47:18 +01:00
|
|
|
local commit_pattern = "%f[%w](" .. string.rep("[a-f0-9]", 7) .. ")%f[%W]"
|
2022-12-23 21:39:16 +00:00
|
|
|
self:on_pattern(ViewConfig.keys.hover, {
|
2022-12-24 10:26:09 +00:00
|
|
|
[commit_pattern] = function(hash)
|
2022-12-23 21:39:16 +00:00
|
|
|
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,
|
2023-05-20 08:26:23 +01:00
|
|
|
["|(%S-)|"] = function(h)
|
|
|
|
vim.cmd.help(h)
|
|
|
|
self:close()
|
|
|
|
end,
|
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)
|
2022-12-24 10:26:09 +00:00
|
|
|
self:on_pattern(ViewConfig.keys.diff, {
|
|
|
|
[commit_pattern] = function(hash)
|
|
|
|
self:diff({ commit = hash })
|
|
|
|
end,
|
|
|
|
}, self.diff)
|
2022-12-30 08:08:21 +00:00
|
|
|
self:on_pattern(ViewConfig.commands.restore.key_plugin, {
|
|
|
|
[commit_pattern] = function(hash)
|
|
|
|
self:restore({ commit = hash })
|
|
|
|
end,
|
|
|
|
}, self.restore)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param opts? {commit:string}
|
|
|
|
function M:restore(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local Lockfile = require("lazy.manage.lock")
|
|
|
|
local Commands = require("lazy.view.commands")
|
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
if opts.commit then
|
|
|
|
Lockfile.get(plugin).commit = opts.commit
|
|
|
|
end
|
|
|
|
Commands.cmd("restore", { plugins = { plugin } })
|
|
|
|
end
|
2022-12-24 10:26:09 +00:00
|
|
|
end
|
|
|
|
|
2022-12-23 21:39:16 +00:00
|
|
|
function M:hover()
|
2024-05-12 08:43:30 +01:00
|
|
|
if self:diff({ browser = true, hover = true }) then
|
2022-12-23 21:39:16 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
self:open_url("")
|
|
|
|
end
|
|
|
|
|
2024-05-12 08:43:30 +01:00
|
|
|
---@param opts? {commit?:string, browser:boolean, hover:boolean}
|
2022-12-23 21:39:16 +00:00
|
|
|
function M:diff(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local plugin = self.render:get_plugin()
|
|
|
|
if plugin then
|
|
|
|
local diff
|
|
|
|
if opts.commit then
|
2022-12-24 10:26:09 +00:00
|
|
|
diff = { commit = opts.commit }
|
2022-12-23 21:39:16 +00:00
|
|
|
elseif plugin._.updated then
|
2022-12-24 10:26:09 +00:00
|
|
|
diff = vim.deepcopy(plugin._.updated)
|
2022-12-23 21:39:16 +00:00
|
|
|
else
|
|
|
|
local info = assert(Git.info(plugin.dir))
|
|
|
|
local target = assert(Git.get_target(plugin))
|
|
|
|
diff = { from = info.commit, to = target.commit }
|
2024-05-12 08:43:30 +01:00
|
|
|
if opts.hover and diff.from == diff.to then
|
|
|
|
return
|
|
|
|
end
|
2022-12-23 21:39:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not diff then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-12-24 10:26:09 +00:00
|
|
|
for k, v in pairs(diff) do
|
|
|
|
diff[k] = v:sub(1, 7)
|
|
|
|
end
|
|
|
|
|
2022-12-23 21:39:16 +00:00
|
|
|
if opts.browser then
|
2022-12-24 10:26:09 +00:00
|
|
|
Diff.handlers.browser(plugin, diff)
|
|
|
|
else
|
|
|
|
Diff.handlers[Config.options.diff.cmd](plugin, diff)
|
2022-12-23 21:39:16 +00:00
|
|
|
end
|
2023-09-27 11:51:11 +01:00
|
|
|
|
|
|
|
return true
|
2022-12-23 21:39:16 +00:00
|
|
|
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
|
2022-12-24 10:26:09 +00:00
|
|
|
self.state.mode = "home"
|
|
|
|
return self:update()
|
2022-11-29 11:02:25 +00:00
|
|
|
end
|
2022-12-23 19:55:49 +00:00
|
|
|
Commands.cmd(name)
|
|
|
|
end, m.desc)
|
|
|
|
end
|
2022-12-30 08:08:21 +00:00
|
|
|
if m.key_plugin and name ~= "restore" then
|
2022-12-23 19:55:49 +00:00
|
|
|
self:on_key(m.key_plugin, function()
|
2024-05-31 12:55:54 +01:00
|
|
|
local esc = vim.api.nvim_replace_termcodes("<esc>", true, true, true)
|
|
|
|
vim.api.nvim_feedkeys(esc, "n", false)
|
2024-05-26 09:40:08 +01:00
|
|
|
local plugins = {}
|
|
|
|
if vim.api.nvim_get_mode().mode:lower() == "v" then
|
|
|
|
local f, t = vim.fn.line("."), vim.fn.line("v")
|
2024-05-31 12:55:54 +01:00
|
|
|
if f > t then
|
|
|
|
f, t = t, f
|
|
|
|
end
|
2024-05-26 09:40:08 +01:00
|
|
|
for i = f, t do
|
2024-05-31 15:28:33 +01:00
|
|
|
local plugin = self.render:get_plugin(i)
|
|
|
|
if plugin then
|
|
|
|
plugins[plugin.name] = plugin
|
|
|
|
end
|
2024-05-26 09:40:08 +01:00
|
|
|
end
|
2024-05-31 15:28:33 +01:00
|
|
|
plugins = vim.tbl_values(plugins)
|
2024-05-26 09:40:08 +01:00
|
|
|
else
|
|
|
|
plugins[1] = self.render:get_plugin()
|
|
|
|
end
|
|
|
|
if #plugins > 0 then
|
|
|
|
Commands.cmd(name, { plugins = plugins })
|
2022-12-23 19:55:49 +00:00
|
|
|
end
|
2024-05-26 09:40:08 +01:00
|
|
|
end, m.desc_plugin, { "n", "x" })
|
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
|