Skip to content

Commit

Permalink
feat: show all plugins and better actions fallback
Browse files Browse the repository at this point in the history
include: enabled, disabled, to-cleaned...
  • Loading branch information
phanen committed May 5, 2024
1 parent 4b50450 commit c06d639
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 65 deletions.
11 changes: 8 additions & 3 deletions lua/fzf-lua-overlay/previewers/lazy.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local lazy_cfg = package.loaded['lazy.core.config']

local previewer = require('fzf-lua.previewer.fzf')

local lazy_previewer = previewer.cmd_async:extend()
Expand All @@ -12,11 +10,18 @@ end

function lazy_previewer:cmdline(o)
o = o or {}
local lazy_cfg = package.loaded['lazy.core.config']
self.cwd = lazy_cfg.options.root

-- item can be a fullname or just a plugin name
local act = require('fzf-lua.shell').raw_preview_action_cmd(function(items, _)
local slices = vim.split(items[1], '/')
local repo = slices[#slices]
local dir = lazy_cfg.plugins[repo].dir
local plugins = require('fzf-lua-overlay.util').get_lazy_plugins()
local dir = plugins[repo].dir

-- stupid but work
if not vim.uv.fs_stat(dir) then return 'echo Not Installed!' end

-- builtin preview(e.g. `buffer_or_file:extend()`) support limit static file format
-- use bat to show readme then fallback to `ls`
Expand Down
138 changes: 76 additions & 62 deletions lua/fzf-lua-overlay/providers/lazy.lua
Original file line number Diff line number Diff line change
@@ -1,87 +1,101 @@
---@type FzfLuaOverlaySpec
local M = {}

local lazy_cfg = package.loaded['lazy.core.config']

local lazy_previewer = require('fzf-lua-overlay.previewers.lazy')

M.name = 'fzf_exec'

M.fzf_exec_arg = function(fzf_cb)
coroutine.wrap(function()
local co = coroutine.running()
local plugins = lazy_cfg.plugins
for plug_name in pairs(plugins) do
fzf_cb(plug_name, function() coroutine.resume(co) end)
local plugins = require('fzf-lua-overlay.util').get_lazy_plugins()
for p_name in pairs(plugins) do
fzf_cb(p_name, function() coroutine.resume(co) end)
coroutine.yield()
end
fzf_cb()
end)()
end

-- helper to run cb on parsed selected plugins
---@param cb fun(prop_name: string?, p_name: string, plugins)
local p_do = function(cb, prop_name)
return function(selected)
local slices = vim.split(selected[1], '/')
local name = slices[#slices]
local plugins = require('fzf-lua-overlay.util').get_lazy_plugins()
local plugin = plugins[name] or {}
local prop = prop_name and plugin[prop_name] or nil
cb(prop, name, plugin)
end
end

local toggle_fullname = function()
require('fzf-lua').fzf_exec(
function(fzf_cb)
coroutine.wrap(function()
local co = coroutine.running()
local plugins = require('fzf-lua-overlay.util').get_lazy_plugins()
for _, p_spec in pairs(plugins) do
local fullname = p_spec[1]
if not fullname then
local url = p_spec.url
-- give a dummy name for "clean" plugins
if not url then
fullname = 'unknown/' .. p_spec.name
else
local url_slice = vim.split(url, '/')
local username = url_slice[#url_slice - 1]
local repo = url_slice[#url_slice]
fullname = username .. '/' .. repo
end
end
fzf_cb(fullname, function() coroutine.resume(co) end)
coroutine.yield()
end
fzf_cb()
end)()
end,
vim.tbl_deep_extend(
'force',
M.opts,
{ actions = { ['ctrl-g'] = require('fzf-lua-overlay').lazy } }
)
)
end

M.opts = {
prompt = 'lazy> ',
previewer = lazy_previewer,
actions = {
['default'] = function(selected)
local slices = vim.split(selected[1], '/')
local name = slices[#slices]
require('fzf-lua-overlay.util').chdir(lazy_cfg.plugins[name].dir)
end,
['ctrl-o'] = function(selected)
local slices = vim.split(selected[1], '/')
local name = slices[#slices]
vim.ui.open(lazy_cfg.plugins[name].url)
end,
['ctrl-l'] = function(selected)
local slices = vim.split(selected[1], '/')
local name = slices[#slices]
require('fzf-lua').files { cwd = lazy_cfg.plugins[name].dir }
end,
['ctrl-n'] = function(selected)
local slices = vim.split(selected[1], '/')
local name = slices[#slices]
require('fzf-lua').live_grep_native { cwd = lazy_cfg.plugins[name].dir }
end,
['ctrl-r'] = function(selected)
local slices = vim.split(selected[1], '/')
local name = slices[#slices]
if lazy_cfg.plugins[name]._.loaded then
vim.cmd.Lazy('reload ' .. name)
['default'] = p_do(function(dir, name, plugin)
vim.print(plugin)
if plugin._.installed then
require('fzf-lua-overlay.util').chdir(dir)
else
vim.cmd.Lazy('load ' .. name)
-- TODO: no api for non-loaded plugins...
-- vim.cmd.Lazy('install ' .. name)
end
end,
-- toggle username prefix
['ctrl-g'] = function()
require('fzf-lua').fzf_exec(
function(fzf_cb)
coroutine.wrap(function()
local co = coroutine.running()
local plugins = lazy_cfg.plugins
for _, plug_spec in pairs(plugins) do
local plug_name = plug_spec[1]
if not plug_name then
local url = plug_spec.url
vim.print(url)
if not url then goto continue end
local url_slice = vim.split(url, '/')
local username = url_slice[#url_slice - 1]
local repo = url_slice[#url_slice]
plug_name = username .. '/' .. repo
end
fzf_cb(plug_name, function() coroutine.resume(co) end)
coroutine.yield()
::continue::
end
fzf_cb()
end)()
end,
vim.tbl_deep_extend('force', M.opts, {
actions = { ['ctrl-g'] = require('fzf-lua-overlay').lazy },
})
)
end,
end, 'dir'),
['ctrl-o'] = p_do(function(url, _, plugin)
-- cleaned plugin has not url, so we search it
if not url then url = ('https://github.com/search?q=%s'):format(plugin.name) end
vim.ui.open(url)
end, 'url'),
['ctrl-l'] = p_do(function(dir)
if dir then require('fzf-lua').files { cwd = dir } end
end, 'dir'),
['ctrl-n'] = p_do(function(dir) require('fzf-lua').live_grep_native { cwd = dir } end, 'dir'),
['ctrl-r'] = p_do(function(_, name, plugin)
if plugin._ and plugin._.loaded then
vim.notify('Reload ' .. name, vim.log.levels.WARN)
require('lazy.core.loader').reload(plugin)
else
require('lazy.core.loader').load(plugin, { cmd = 'Lazy load' })
-- vim.cmd.Lazy('load ' .. name)
end
end),
['ctrl-g'] = toggle_fullname,
},
}

Expand Down
14 changes: 14 additions & 0 deletions lua/fzf-lua-overlay/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@ u.gitroot = function(bufname)
return root
end

u.get_lazy_plugins = function()
-- https://github.com/folke/lazy.nvim/blob/d3974346b6cef2116c8e7b08423256a834cb7cbc/lua/lazy/view/render.lua#L38-L40
-- TODO: throttle...
local cfg = package.loaded['lazy.core.config']
local plugins = vim.tbl_extend('keep', {}, cfg.plugins, cfg.to_clean, cfg.spec.disabled)

-- kind="clean" seems not named in table
for i, p in ipairs(plugins) do
plugins[p.name] = p
plugins[i] = nil
end
return plugins
end

return u

0 comments on commit c06d639

Please sign in to comment.