Skip to content

Commit de1ddf7

Browse files
phanenibhagwan
authored andcommitted
chore: fzf previewer types
chore: base64 compat chore: dedup codes
1 parent 2f9e1af commit de1ddf7

6 files changed

Lines changed: 58 additions & 29 deletions

File tree

lua/fzf-lua/previewer/fzf.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local Object = require "fzf-lua.class"
77

88
local Previewer = {}
99

10+
---@class fzf-lua.previewer.Fzf: fzf-lua.Object,{}
1011
Previewer.base = Object:extend()
1112

1213
-- Previewer base object
@@ -64,6 +65,8 @@ function Previewer.base:fzf_delimiter()
6465
end
6566

6667
-- Generic shell command previewer
68+
---@class fzf-lua.previewer.Cmd : fzf-lua.previewer.Fzf,{}
69+
---@field super fzf-lua.previewer.Fzf,{}
6770
Previewer.cmd = Previewer.base:extend()
6871

6972
function Previewer.cmd:new(o, opts)
@@ -92,6 +95,8 @@ function Previewer.cmd:action(o)
9295
end
9396

9497
-- Specialized bat previewer
98+
---@class fzf-lua.previewer.Bat : fzf-lua.previewer.Cmd,{}
99+
---@field super fzf-lua.previewer.Cmd,{}
95100
Previewer.bat = Previewer.cmd:extend()
96101

97102
function Previewer.bat:new(o, opts)
@@ -114,6 +119,8 @@ function Previewer.bat:cmdline(o)
114119
end
115120

116121
-- Specialized head previewer
122+
---@class fzf-lua.previewer.Head : fzf-lua.previewer.Cmd,{}
123+
---@field super fzf-lua.previewer.Cmd,{}
117124
Previewer.head = Previewer.cmd:extend()
118125

119126
function Previewer.head:new(o, opts)
@@ -133,6 +140,8 @@ function Previewer.head:cmdline(o)
133140
end
134141

135142
-- new async_action from nvim-fzf
143+
---@class fzf-lua.previewer.CmdAsync : fzf-lua.previewer.Cmd,{}
144+
---@field super fzf-lua.previewer.Cmd,{}
136145
Previewer.cmd_async = Previewer.base:extend()
137146

138147
function Previewer.cmd_async:new(o, opts)
@@ -218,6 +227,8 @@ function Previewer.cmd_async:cmdline(o)
218227
return act
219228
end
220229

230+
---@class fzf-lua.previewer.BatAsync : fzf-lua.previewer.CmdAsync,{}
231+
---@field super fzf-lua.previewer.CmdAsync,{}
221232
Previewer.bat_async = Previewer.cmd_async:extend()
222233

223234
---@param lnum string?
@@ -283,6 +294,8 @@ function Previewer.bat_async:cmdline(o)
283294
return act
284295
end
285296

297+
---@class fzf-lua.previewer.GitDiff : fzf-lua.previewer.Fzf,{}
298+
---@field super fzf-lua.previewer.Fzf,{}
286299
Previewer.git_diff = Previewer.base:extend()
287300

288301
function Previewer.git_diff:new(o, opts)
@@ -384,6 +397,8 @@ function Previewer.git_diff:cmdline(o)
384397
return act
385398
end
386399

400+
---@class fzf-lua.previewer.fzf.ManPages : fzf-lua.previewer.Fzf,{}
401+
---@field super fzf-lua.previewer.Fzf,{}
387402
Previewer.man_pages = Previewer.base:extend()
388403

389404
function Previewer.man_pages:new(o, opts)
@@ -403,6 +418,8 @@ function Previewer.man_pages:cmdline(o)
403418
return act
404419
end
405420

421+
---@class fzf-lua.previewer.fzf.HelpTags : fzf-lua.previewer.Fzf,{}
422+
---@field super fzf-lua.previewer.fzf.HelpTags,{}
406423
Previewer.help_tags = Previewer.base:extend()
407424

408425
function Previewer.help_tags:fzf_delimiter()

lua/fzf-lua/previewer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Previewer.builtin.codeaction = function() return require "fzf-lua.previewer.code
3131
---Instantiate previewer from spec
3232
---@param spec table
3333
---@param opts table
34-
---@return fzf-lua.previewer?
34+
---@return fzf-lua.previewer.Fzf|fzf-lua.previewer.Builtin?
3535
Previewer.new = function(spec, opts)
3636
if not spec then return end
3737
local previewer, preview_opts = nil, nil

lua/fzf-lua/providers/nvim.lua

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,25 @@ M.commands = function(opts)
6363
end
6464
end
6565

66-
opts.flatten = opts.flatten or {}
67-
for k, _ in pairs(global_commands) do
68-
table.insert(entries, utils.ansi_codes.blue(k))
66+
local add_subcommand = function(k, ansi_color)
6967
local flattened = vim.is_callable(opts.flatten[k]) and opts.flatten[k](opts)
7068
or opts.flatten[k] and vim.fn.getcompletion(k .. " ", "cmdline")
7169
or {}
7270
vim.list_extend(entries,
73-
vim.tbl_map(function(cmd) return utils.ansi_codes.blue(k .. " " .. cmd) end,
71+
vim.tbl_map(function(cmd) return ansi_color(k .. " " .. cmd) end,
7472
flattened))
7573
end
7674

75+
opts.flatten = opts.flatten or {}
76+
for k, _ in pairs(global_commands) do
77+
table.insert(entries, utils.ansi_codes.blue(k))
78+
add_subcommand(k, utils.ansi_codes.blue)
79+
end
80+
7781
for k, v in pairs(buf_commands) do
7882
if type(v) == "table" then
7983
table.insert(entries, utils.ansi_codes.green(k))
80-
local flattened = vim.is_callable(opts.flatten[k]) and opts.flatten[k](opts)
81-
or opts.flatten[k] and vim.fn.getcompletion(k .. " ", "cmdline")
82-
or {}
83-
vim.list_extend(entries,
84-
vim.tbl_map(function(cmd) return utils.ansi_codes.green(k .. " " .. cmd) end,
85-
flattened))
84+
add_subcommand(k, utils.ansi_codes.green)
8685
end
8786
end
8887

lua/fzf-lua/shell.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ M.stringify_mt = function(cmd, opts)
239239
else -- if opts.multiprocess then
240240
for _, k in ipairs({ "fn_transform", "fn_preprocess", "fn_postprocess" }) do
241241
local v = opts[k]
242-
if type(v) == "function" and utils.__HAS_NVIM_010 then
242+
if type(v) == "function" then
243243
-- Attempt to convert function to its bytecode representation
244-
-- NOTE: limited to neovim >= 0.10 due to vim.base64
244+
-- TODO: can be replaced with vim.base64 after neovim >= 0.10
245245
v = string.format(
246-
[[return loadstring(vim.base64.decode("%s"))]],
247-
vim.base64.encode(string.dump(v, true)))
246+
[[return loadstring(require("fzf-lua.lib.base64").decode("%s"))]],
247+
require("fzf-lua.lib.base64").encode(string.dump(v, true)))
248248
-- Test the function once with nil value (imprefect?)
249249
-- to see if there's an issue with upvalue refs
250250
local f = loadstring(v)()

lua/fzf-lua/types.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ error("Cannot require a meta file")
33

44
_G.FzfLua = require("fzf-lua")
55

6-
---@class fzf-lua.previewer
6+
---@class fzf-lua.previewer.Fzf
77
---@field new function
88
---@field zero function?
99
---@field cmdline function?
@@ -37,13 +37,13 @@ _G.FzfLua = require("fzf-lua")
3737
---@field cached_bufnrs { [string]: fzf-lua.previewer.CursorPos? }
3838
---@field cached_buffers { [string]: fzf-lua.buffer_or_file.Bcache? }
3939
---@field listed_buffers { [string]: boolean? }
40+
---@field clear_on_redraw boolean?
4041
---
4142
---@field orig_pos fzf-lua.previewer.CursorPos
4243
---@alias fzf-lua.previewer.CursorPos (true|[integer, integer])
4344

4445
---@class fzf-lua.previewer.BufferOrFile
4546
---@field match_id integer?
46-
---@field clear_on_redraw boolean?
4747

4848
---@class fzf-lua.path.Entry
4949
---@field stripped string
@@ -84,9 +84,10 @@ _G.FzfLua = require("fzf-lua")
8484
---@alias fzf-lua.config.Actions { [string]: fzf-lua.config.Action }
8585

8686
---@class fzf-lua.ActionSpec
87-
---@field fn function
87+
---@field [1] function?
88+
---@field fn function?
8889
---@field exec_silent boolean?
89-
---@field reload boolean|fun(opts: fzf-lua.Config):content
90+
---@field reload boolean|fun(opts: fzf-lua.Config):content?
9091
---@field field_index string?
9192
---@field desc string?
9293
---@field prefix string?

lua/fzf-lua/win.lua

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ function TSInjector._attach_lang(buf, lang, regions)
9696
end
9797

9898
---@class fzf-lua.Win
99-
---@field _previewer fzf-lua.previewer.Builtin|fzf-lua.previewer.BufferOrFile?
99+
---@field _previewer fzf-lua.previewer.Builtin|fzf-lua.previewer.Fzf?
100100
---@field _preview_pos_force "up"|"down"
101+
---@field km_winid integer?
102+
---@field km_bufnr integer?
103+
---@field _hidden_save_size [integer, integer, integer]?
101104
local FzfWin = {}
102105

103106
-- singleton instance used in win_leave
107+
---@type fzf-lua.Win?
104108
local _self = nil
105109

106110
function FzfWin.__SELF()
@@ -1629,20 +1633,28 @@ function FzfWin.toggle_help()
16291633
end
16301634
end
16311635

1636+
---TODO: we can always parse the action into table to avoid this duplicated logic
1637+
---(e.g. profile/hide.lua, config.lua)
1638+
---@param v fzf-lua.ActionSpec
1639+
---@return string?
1640+
local get_desc = function(v)
1641+
if type(v) == "table" then
1642+
return v.desc or config.get_action_helpstr(v[1]) or config.get_action_helpstr(v.fn) or
1643+
tostring(v)
1644+
elseif v then
1645+
return config.get_action_helpstr(v) or tostring(v)
1646+
end
1647+
end
1648+
16321649
-- action keymaps
16331650
if self.actions then
16341651
for k, v in pairs(self.actions) do
1635-
if k == "default" then k = "enter" end
1636-
if type(v) == "table" then
1637-
v = v.desc or config.get_action_helpstr(v[1]) or config.get_action_helpstr(v.fn) or v
1638-
elseif v then
1639-
v = config.get_action_helpstr(v) or v
1640-
end
1641-
if v then
1642-
-- skips 'v == false'
1652+
if v then -- skips 'v == false'
1653+
if k == "default" then k = "enter" end
1654+
local desc = get_desc(v)
16431655
table.insert(keymaps,
16441656
format_bind("action", k,
1645-
("%s"):format(tostring(v)):gsub(" ", ""),
1657+
("%s"):format(desc):gsub(" ", ""),
16461658
opts.mode_width, opts.keybind_width, opts.name_width))
16471659
end
16481660
end

0 commit comments

Comments
 (0)