feat(ui): you can now hover over a plugin to open a diff of updates or the plugin homepage

This commit is contained in:
Folke Lemaitre 2022-12-23 22:39:16 +01:00
parent a973c2edc2
commit 593d6e400b
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 63 additions and 15 deletions

View File

@ -2,6 +2,7 @@ local Util = require("lazy.util")
local Render = require("lazy.view.render") local Render = require("lazy.view.render")
local Config = require("lazy.core.config") local Config = require("lazy.core.config")
local ViewConfig = require("lazy.view.config") local ViewConfig = require("lazy.view.config")
local Git = require("lazy.manage.git")
---@class LazyViewState ---@class LazyViewState
---@field mode string ---@field mode string
@ -95,7 +96,7 @@ function M.create(opts)
end end
end) end)
self:setup_hover() self:setup_patterns()
self:setup_modes() self:setup_modes()
return self return self
end end
@ -226,16 +227,10 @@ function M:mount()
vim.wo[self.win].winhighlight = "Normal:LazyNormal" vim.wo[self.win].winhighlight = "Normal:LazyNormal"
end end
function M:setup_hover() function M:setup_patterns()
local handlers = { self:on_pattern(ViewConfig.keys.hover, {
["%s(" .. string.rep("[a-z0-9]", 7) .. ")%s"] = function(hash) ["%f[a-z0-9](" .. string.rep("[a-z0-9]", 7) .. ")%f[^a-z0-9]"] = function(hash)
self:open_url("/commit/" .. hash) self:diff({ commit = hash, browser = true })
end,
["%s(" .. string.rep("[a-z0-9]", 7) .. ")$"] = function(hash)
self:open_url("/commit/" .. hash)
end,
["^(" .. string.rep("[a-z0-9]", 7) .. ")%s"] = function(hash)
self:open_url("/commit/" .. hash)
end, end,
["#(%d+)"] = function(issue) ["#(%d+)"] = function(issue)
self:open_url("/issues/" .. issue) self:open_url("/issues/" .. issue)
@ -250,14 +245,62 @@ function M:setup_hover()
["(https?://%S+)"] = function(url) ["(https?://%S+)"] = function(url)
Util.open(url) Util.open(url)
end, end,
} }, self.hover)
function M:hover()
if self:diff({ browser = true }) then
return
end
self:open_url("")
end
self:on_key(ViewConfig.keys.hover, function() ---@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
--- 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()
local line = vim.api.nvim_get_current_line() local line = vim.api.nvim_get_current_line()
local pos = vim.api.nvim_win_get_cursor(0) local pos = vim.api.nvim_win_get_cursor(0)
local col = pos[2] + 1 local col = pos[2] + 1
for pattern, handler in pairs(handlers) do for pattern, handler in pairs(patterns) do
local from = 1 local from = 1
local to, url local to, url
while from do while from do
@ -270,6 +313,9 @@ function M:setup_hover()
end end
end end
end end
if fallback then
fallback(self)
end
end) end)
end end

View File

@ -149,7 +149,9 @@ function M:help()
self:append("Most properties can be hovered with ") self:append("Most properties can be hovered with ")
self:append("<K>", "LazySpecial") self:append("<K>", "LazySpecial")
self:append(" to open links, help files, readmes and git commits."):nl():nl() self:append(" to open links, help files, readmes and git commits."):nl()
self:append("When hovering on a plugin anywhere else, a diff will be opened if there are updates"):nl()
self:append("or the plugin was just updated. Otherwise the plugin webpage will open."):nl():nl()
self:append("Keyboard Shortcuts", "LazyH2"):nl() self:append("Keyboard Shortcuts", "LazyH2"):nl()
for _, mode in ipairs(ViewConfig.get_commands()) do for _, mode in ipairs(ViewConfig.get_commands()) do