From 632e91b07601ed4415b18af052e9d20f6edc625e Mon Sep 17 00:00:00 2001 From: phanium Date: Fri, 11 Oct 2024 11:08:41 +0800 Subject: [PATCH] refactor: share previewers also cleanup fzf previewer not fancy which we never use --- README.md | 1 + .../{previewers/lazy.lua => previewers.lua} | 101 ++++++++---------- lua/flo/providers/gitignore.lua | 36 ++----- lua/flo/providers/lazy.lua | 2 +- lua/flo/providers/license.lua | 19 ++-- lua/flo/util.lua | 8 -- 6 files changed, 56 insertions(+), 111 deletions(-) rename lua/flo/{previewers/lazy.lua => previewers.lua} (60%) diff --git a/README.md b/README.md index 07d2d88..918693a 100644 --- a/README.md +++ b/README.md @@ -113,3 +113,4 @@ vim.api.nvim_create_autocmd("BufDelete", { ## todo * [x] integration with dirstack.nvim (https://github.com/phanen/dirstack.nvim/commit/f5efd5e8c7768c22d2d52f6d1ae827a54ccaf416) * [x] inject new pickers into fzflua builtin +* [ ] generic gh api can also be used in lazy previewer (fair enough... btw we finally need more structured-async) diff --git a/lua/flo/previewers/lazy.lua b/lua/flo/previewers.lua similarity index 60% rename from lua/flo/previewers/lazy.lua rename to lua/flo/previewers.lua index cf91d0e..fde0496 100644 --- a/lua/flo/previewers/lazy.lua +++ b/lua/flo/previewers.lua @@ -1,7 +1,15 @@ -local builtin_previewer = require('fzf-lua.previewer.builtin') -local fzf_previewer = require('fzf-lua.previewer.fzf') +local M = {} +local previewer = require('fzf-lua.previewer.builtin') local floutil = require('flo.util') +local preview_with = function(_self, content) + local tmpbuf = _self:get_tmp_buffer() + vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, content) + if _self.filetype then vim.bo[tmpbuf].filetype = _self.filetype end + _self:set_preview_buf(tmpbuf) + _self.win:update_scrollbar() +end + local github_raw_url = function(url, filepath) return url:gsub('github.com', 'raw.githubusercontent.com'):gsub('%.git$', '') .. '/master/' @@ -47,56 +55,10 @@ local parse_plugin_type = function(_, plugin) return p_type.INS_NO_MD end -local lazy_fzf = fzf_previewer.cmd_async:extend() - -function lazy_fzf:new(o, op, fzf_win) - lazy_fzf.super.new(self, o, op, fzf_win) - self.bat_cmd = 'bat --color=always --style=numbers,changes' - self.ls_cmd = 'eza --color=always --tree --level=3 --icons=always {}' - return self -end +M.lazy = previewer.base:extend() -function lazy_fzf:cmdline(_) - return require('fzf-lua').shell.raw_preview_action_cmd(function(items, _) - local plugin = parse_entry(self, items[1]) - local t, data = parse_plugin_type(self, plugin) - - local handlers = { - -- TODO: parse local dir (absolute, or relative to vim.fn.stdpath('config')) - [p_type.LOCAL] = 'echo Local module!', - - -- https://raw.githubusercontent.com/author/repo/master/README.md - -- main? master - -- FIXME: 1. if subprocess false, still return 0; 2. 404 is even not a false (drop output if 404?) - -- anyway, we just always run both commands here - [p_type.UNINS_GH] = function() - return ('echo "> Not Installed (fetch from github)!\n" && { curl -sL %s | %s --language md; }; { curl -sL %s | %s --language md; }'):format( - github_raw_url(plugin.url, 'README.md'), - self.bat_cmd, - github_raw_url(plugin.url, 'readme.md'), - self.bat_cmd - ) - end, - - [p_type.UNINS_NO_GH] = 'echo "> Not Installed (not github)"!', - - -- TODO: buffer_or_file/quickfix - [p_type.INS_MD] = ('%s %s'):format(self.bat_cmd, data), - - [p_type.INS_NO_MD] = self.ls_cmd:format(plugin.dir), - } - local cmdline = handlers[t] - if type(cmdline) == 'function' then cmdline = cmdline() end - if cmdline then return cmdline end - return 'echo Unknown plugin type!' - end, '{}', self.opts.debug) -end - -local lazy_builtin = builtin_previewer.base:extend() --- local lazy_builtin = builtin_previewer.buffer_or_file:extend() - -function lazy_builtin:new(o, opts, fzf_win) - lazy_builtin.super.new(self, o, opts, fzf_win) +function M.lazy:new(o, opts, fzf_win) + M.lazy.super.new(self, o, opts, fzf_win) -- self.filetype = 'man' self.cmd = o.cmd or 'man -c %s | col -bx' self.cmd = type(self.cmd) == 'function' and self.cmd() or self.cmd @@ -104,10 +66,10 @@ function lazy_builtin:new(o, opts, fzf_win) -- lazy_builtin.super.new(self, o, op, fzf_win) self.ls_cmd = 'ls -lh' -- FIXME: why this is needed (why fzf previewer don't needed this...) - return setmetatable(self, self) + return self end -function lazy_builtin:populate_preview_buf(entry_str) +function M.lazy:populate_preview_buf(entry_str) if entry_str == '' then self:clear_preview_buf(true) return @@ -157,13 +119,34 @@ function lazy_builtin:populate_preview_buf(entry_str) ---@diagnostic disable-next-line: param-type-mismatch vim.schedule_wrap(function(obj) local content = vim.split(obj.stdout, '\n') - floutil.preview_with(self, content) + preview_with(self, content) end) ) end -return { - fzf = lazy_fzf, - builtin = lazy_builtin, - -- builtin = lazy_fzf, -} +M.gitignore = previewer.buffer_or_file:extend() + +function M.gitignore:new(o, opts, fzf_win) + M.gitignore.super.new(self, o, opts, fzf_win) + self.api_root = opts.api_root + self.filetype = opts.filetype + self.json_key = opts.json_key + return self +end + +function M.gitignore:populate_preview_buf(entry_str) + if entry_str == '' then + self:clear_preview_buf(true) + return + end + floutil.gh_cache( + self.api_root .. '/' .. entry_str, + vim.schedule_wrap(function(_, json) + local content = assert(json[self.json_key]) + content = vim.split(content, '\n') + preview_with(self, content) + end) + ) +end + +return M diff --git a/lua/flo/providers/gitignore.lua b/lua/flo/providers/gitignore.lua index 34bda2b..7c5c077 100644 --- a/lua/flo/providers/gitignore.lua +++ b/lua/flo/providers/gitignore.lua @@ -1,37 +1,13 @@ local floutil = require('flo.util') -local builtin_previewer = require('fzf-lua.previewer.builtin') ---@type FzfLuaOverlaySpec local M = {} -local api_root = 'gitignore/templates' -local previewer = builtin_previewer.buffer_or_file:extend() - -function previewer:new(o, opts, fzf_win) - previewer.super.new(self, o, opts, fzf_win) - self.api_root = api_root - self.filetype = 'gitignore' - self.json_key = 'source' - return self -end - -function previewer:populate_preview_buf(entry_str) - if entry_str == '' then - self:clear_preview_buf(true) - return - end - floutil.gh_cache( - self.api_root .. '/' .. entry_str, - vim.schedule_wrap(function(_, json) - local content = assert(json[self.json_key]) - content = vim.split(content, '\n') - floutil.preview_with(self, content) - end) - ) -end - M.opts = { - previewer = { _ctor = function() return previewer end }, + previewer = { _ctor = function() return require('flo.previewers').gitignore end }, + api_root = 'gitignore/templates', + json_key = 'source', + filetype = 'gitignore', actions = { ['default'] = function(selected) local root = floutil.gitroot() @@ -42,7 +18,7 @@ M.opts = { if confirm ~= 1 then return end end local filetype = assert(selected[1]) - floutil.gh_cache(api_root .. '/' .. filetype, function(_, json) + floutil.gh_cache(M.opts.api_root .. '/' .. filetype, function(_, json) local content = assert(json.source) floutil.write_file(path, content) vim.cmd.edit(path) @@ -53,7 +29,7 @@ M.opts = { M.fn = function(opts) local contents = function(fzf_cb) - floutil.gh_cache(api_root, function(_, json) + floutil.gh_cache(M.opts.api_root, function(_, json) coroutine.wrap(function() local co = coroutine.running() vim.iter(json):each(function(item) diff --git a/lua/flo/providers/lazy.lua b/lua/flo/providers/lazy.lua index c63ec42..5d05d1e 100644 --- a/lua/flo/providers/lazy.lua +++ b/lua/flo/providers/lazy.lua @@ -68,7 +68,7 @@ local p_do = function(cb, limit) end M.opts = { - previewer = require('flo.previewers.lazy').builtin, + previewer = { _ctor = function() return require('flo.previewers').lazy end }, actions = { ['default'] = p_do(function(p) local dir = p.dir diff --git a/lua/flo/providers/license.lua b/lua/flo/providers/license.lua index 8cf2af1..68ce694 100644 --- a/lua/flo/providers/license.lua +++ b/lua/flo/providers/license.lua @@ -4,18 +4,11 @@ local _, fn, uv = vim.api, vim.fn, vim.uv ---@type FzfLuaOverlaySpec local M = {} -local api_root = 'licenses' -local previewer = require('flo.providers.gitignore').opts.previewer._ctor():extend() -function previewer:new(o, opts, fzf_win) - previewer.super:new(o, opts, fzf_win) - self.api_root = api_root - self.filetype = 'text' - self.json_key = 'body' - return self -- use setmetatable(self, self) can avoid ctor -end - M.opts = { - previewer = { _ctor = function() return previewer end }, + previewer = { _ctor = function() return require('flo.previewers').gitignore:extend() end }, + api_root = 'licenses', + json_key = 'body', + filetype = 'text', actions = { ['default'] = function(selected) local root = floutil.gitroot() @@ -30,7 +23,7 @@ M.opts = { if path and fn.confirm('Override?', '&Yes\n&No') ~= 1 then return end local license = assert(selected[1]) - floutil.gh_cache('licenses/' .. license, function(_, json) + floutil.gh_cache(M.opts.api_root .. 'licenses/' .. license, function(_, json) local content = assert(json.body) floutil.write_file(path, content) vim.cmd.edit(path) @@ -41,7 +34,7 @@ M.opts = { M.fn = function(opts) local contents = function(fzf_cb) - floutil.gh_cache('licenses', function(_, json) + floutil.gh_cache(M.opts.api_root, function(_, json) coroutine.wrap(function() local co = coroutine.running() vim.iter(json):each(function(item) diff --git a/lua/flo/util.lua b/lua/flo/util.lua index 16e87a6..4f8bb95 100644 --- a/lua/flo/util.lua +++ b/lua/flo/util.lua @@ -167,14 +167,6 @@ M.snake_to_camel = function(name) return table.concat(parts, '') end -M.preview_with = function(_self, content) - local tmpbuf = _self:get_tmp_buffer() - vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, content) - if _self.filetype then vim.bo[tmpbuf].filetype = _self.filetype end - _self:set_preview_buf(tmpbuf) - _self.win:update_scrollbar() -end - M.ls = function(path, _fn) for name, type in fs.dir(path) do local fname = fs.joinpath(path, name)