Skip to content

Commit

Permalink
refactor(formatter): make API more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed May 14, 2024
1 parent 7109a18 commit e412b1b
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 66 deletions.
8 changes: 4 additions & 4 deletions lua/fzf-lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ function M.normalize_opts(opts, globals, __resume_key)
opts._fmt._to = opts._fmt.to
opts._fmt.to = loadstring(tostring(opts._fmt.to))()
end
-- no support for `bat_native` with a formatter
if opts.previewer == "bat_native" then opts.previewer = "bat" end
-- no support of searching file begin (we can't guarantee no. of nbsp's)
opts._fzf_nth_devicons = false
if type(_fmt.enrich) == "function" then
-- formatter requires enriching the config (fzf_opts, etc)
opts = _fmt.enrich(opts) or opts
end
else
utils.warn(("Invalid formatter '%s', ignoring."):format(opts.formatter))
end
Expand Down
39 changes: 21 additions & 18 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ M.defaults = {
formatters = {
path = {
filename_first = {
-- <Tab> is used as the invisible space between the parent and the file part
enrich = function(o)
o.fzf_opts = vim.tbl_extend("keep", o.fzf_opts or {}, { ["--tabstop"] = 1 })
return o
end,
-- underscore `_to` returns a custom to function when options could
-- affect the transformation, here we create a different function
-- base on the dir part highlight group.
Expand All @@ -211,34 +216,32 @@ M.defaults = {
local parent = _path.parent(s)
if parent then
parent = _path.remove_trailing(parent)
return string.format("%s%s%s", _path.tail(s), _utils.nbsp,
#_escseq == 0 and parent
or string.format("%s%s%s", _escseq, parent, _utils.ansi_escseq.clear))
if #_escseq > 0 then
parent = _escseq .. parent .. _utils.ansi_escseq.clear
end
return _path.tail(s) .. "\t" .. parent
else
return s
end
end
]]
end,
from = function(s, o)
-- Which part should contain the parent folder?
-- files that are directly under cwd it will have no parent folder part
local parent_idx = (o._parent_idx or 2)
+ (o.file_icons and 1 or 0)
+ (o.git_icons and 1 or 0)
from = function(s, _)
local parts = utils.strsplit(s, utils.nbsp)
if #parts == parent_idx then
local last = parts[parent_idx]
local filename = parts[parent_idx - 1]
local parent = last:match("^[^:]+")
if utils.__IS_WINDOWS and last:match("%a:") then
parent = last:sub(1, 2) .. (#last > 2 and last:sub(3):match("^[^:]+") or "")
local last = parts[#parts]
-- Lines from grep, lsp, tags are formatted <file>:<line>:<col>:<text>
-- the pattern below makes sure tab doesn't come from the line text
local filename, rest = last:match("^([^:]-)\t(.+)$")
if filename and rest then
local parent
if utils.__IS_WINDOWS and path.is_absolute(rest) then
parent = rest:sub(1, 2) .. (#rest > 2 and rest:sub(3):match("^[^:]+") or "")
else
parent = rest:match("^[^:]+")
end
local fullpath = path.join({ parent, filename })
-- remove the last part (parent + rest of line)
table.remove(parts, parent_idx)
-- overwrite last part with restored fullpath + rest of line
parts[#parts] = fullpath .. last:sub(#parent + 1)
parts[#parts] = fullpath .. rest:sub(#parent + 1)
s = table.concat(parts, utils.nbsp)
end
return s
Expand Down
4 changes: 2 additions & 2 deletions lua/fzf-lua/lib/base64.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ if not extract then
end
else -- Lua 5.3+
extract = load [[return function( v, from, width )
return ( v >> from ) & ((1 << width) - 1)
end]] ()
return ( v >> from ) & ((1 << width) - 1)
end]] ()
end
end

Expand Down
8 changes: 2 additions & 6 deletions lua/fzf-lua/providers/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ end
M.buffer_lines = function(opts)
if not opts then return end

-- set parent_idx base for `formatter=path.filename_first`
opts._parent_idx = 3

opts.fn_pre_fzf = function() core.CTX(true) end
opts.fn_pre_fzf()

Expand Down Expand Up @@ -369,8 +366,7 @@ M.tabs = function(opts)
local title, fn_title_hl = opt_hl("tab_title",
function(s)
return string.format("%s%s#%d%s", s, utils.nbsp, t,
(uv.cwd() == tab_cwd and ""
or string.format(": %s", tab_cwd_tilde)))
(uv.cwd() == tab_cwd and "" or string.format(": %s", tab_cwd_tilde)))
end,
utils.ansi_codes[opts.hls.tab_title])

Expand All @@ -380,7 +376,7 @@ M.tabs = function(opts)

local tab_cwd_tilde_base64 = base64.encode(tab_cwd_tilde)
if not opts.current_tab_only then
cb(string.format("%s:%d)%s%s\t%s",
cb(string.format("%s:%d)%s%s %s",
tab_cwd_tilde_base64,
t,
utils.nbsp,
Expand Down
3 changes: 0 additions & 3 deletions lua/fzf-lua/providers/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ M.breakpoints = function(opts)
-- display relative paths by default
if opts.cwd == nil then opts.cwd = uv.cwd() end

-- set parent_idx base for `formatter=path.filename_first`
opts._parent_idx = 3

opts.func_async_callback = false
opts.__fn_reload = opts.__fn_reload or function(_)
return function(cb)
Expand Down
3 changes: 0 additions & 3 deletions lua/fzf-lua/providers/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ M.diagnostics = function(opts)
opts.cwd_only = true
end

-- set parent_idx base for `formatter=path.filename_first`
opts._parent_idx = 3

if not vim.diagnostic then
local lsp_clients = vim.lsp.buf_get_clients(0)
if utils.tbl_isempty(lsp_clients) then
Expand Down
3 changes: 0 additions & 3 deletions lua/fzf-lua/providers/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ M.status = function(opts)
-- as part of our `git status -s`
opts.git_icons = false

-- set parent_idx base for `formatter=path.filename_first`
opts._parent_idx = 5

-- we always require processing (can't send the raw command to fzf)
opts.requires_processing = true

Expand Down
2 changes: 0 additions & 2 deletions lua/fzf-lua/providers/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,6 @@ M.finder = function(opts)
return
end
opts = core.set_fzf_field_index(opts)
-- set parent_idx base for `formatter=path.filename_first`
opts._parent_idx = 3
return core.fzf_exec(contents, opts)
end

Expand Down
14 changes: 7 additions & 7 deletions lua/fzf-lua/shell_helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ local function rpc_nvim_exec_lua(opts)
end)

if not success or opts.debug then
io.stderr:write(("[DEBUG]\tdebug = %s\n"):format(opts.debug))
io.stderr:write(("[DEBUG]\tfunction ID = %d\n"):format(opts.fnc_id))
io.stderr:write(("[DEBUG]\tfzf_lua_server = %s\n"):format(opts.fzf_lua_server))
io.stderr:write(("[DEBUG] debug = %s\n"):format(opts.debug))
io.stderr:write(("[DEBUG] function ID = %d\n"):format(opts.fnc_id))
io.stderr:write(("[DEBUG] fzf_lua_server = %s\n"):format(opts.fzf_lua_server))
for i = 1, #vim.v.argv do
io.stderr:write(("[DEBUG]\targv[%d] = %s\n"):format(i, vim.v.argv[i]))
io.stderr:write(("[DEBUG] argv[%d] = %s\n"):format(i, vim.v.argv[i]))
end
local nargs = vim.fn.argc()
for i = 0, nargs - 1 do
io.stderr:write(("[DEBUG]\targv[%d] = %s\n"):format(i, vim.fn.argv(i)))
io.stderr:write(("[DEBUG] argv[%d] = %s\n"):format(i, vim.fn.argv(i)))
end
for i = 0, nargs - 1 do
local argv_idx = #vim.v.argv - nargs + 1 + i
io.stderr:write(("[DEBUG]\tv:arg[%d:%d] = %s\n"):format(i, argv_idx, vim.v.argv[argv_idx]))
io.stderr:write(("[DEBUG] v:arg[%d:%d] = %s\n"):format(i, argv_idx, vim.v.argv[argv_idx]))
end
for _, var in ipairs({ "LINES", "COLUMNS" }) do
io.stderr:write(("[DEBUG]\t$%s = %s\n"):format(var, os.getenv(var) or "<null>"))
io.stderr:write(("[DEBUG] $%s = %s\n"):format(var, os.getenv(var) or "<null>"))
end
end

Expand Down
34 changes: 17 additions & 17 deletions scripts/headless_fd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [ $? -ne 0 ]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
usage;
usage;
exit 2
fi
# read getopt’s output this way to handle the quoting right:
Expand All @@ -50,10 +50,10 @@ color_icons="true"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-h|--help)
usage;
exit 0
;;
-h|--help)
usage;
exit 0
;;
-d|--debug)
case $2 in
"2"|"v"|"verbose")
Expand All @@ -76,7 +76,7 @@ while true; do
cmd="$2"
shift 2
;;
-f|--file-icons)
-f|--file-icons)
case $2 in
"0"|"false")
file_icons="false"
Expand All @@ -85,9 +85,9 @@ while true; do
file_icons="true"
;;
esac
shift 2
;;
-g|--git-icons)
shift 2
;;
-g|--git-icons)
case $2 in
"0"|"false")
git_icons="false"
Expand All @@ -96,9 +96,9 @@ while true; do
git_icons="true"
;;
esac
shift 2
;;
--color)
shift 2
;;
--color)
case $2 in
"0"|"false")
color_icons="false"
Expand All @@ -107,16 +107,16 @@ while true; do
color_icons="true"
;;
esac
shift 2
;;
shift 2
;;
--)
shift
break
;;
*)
# never get here!
# never get here!
echo "error: error while parsing command line arguments"
usage;
usage;
exit 3
;;
esac
Expand All @@ -125,7 +125,7 @@ done
# handle non-option arguments
if [ $# -gt 0 ]; then
echo "error: unrecgonized option"
usage;
usage;
exit 4
fi

Expand Down
2 changes: 1 addition & 1 deletion tests/path_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe("Testing path module", function()
{ [[C:\a/\/b///\\\c]], [[c:\/\a\/b]], { true, "c" } },
{ [[C:\a/b\c\d\e]], [[c:\A\/b\]], { true, [[c\d\e]] } },
{ [[C:\Users]], [[c:\]], { true, [[Users]] } },
{ [[C:\Users\test]], [[c:\]], { true, [[Users\test]] } },
{ [[C:\Users\foo]], [[c:\]], { true, [[Users\foo]] } },
{ [[C:\]], [[C:\Users]], { false, nil } },
{ [[C:]], [[C:\Users]], { false, nil } },
}
Expand Down

0 comments on commit e412b1b

Please sign in to comment.