Skip to content

Commit

Permalink
feat(UI): better experience using fzf's reload
Browse files Browse the repository at this point in the history
similar to recent changes to `git_status` (#730), all other providers
that have a content modify action, namely `buffers`, `tabs`, `args` and
`git_stash` which requires reloading the interface (buffer wipe, arg
delete, etc) will use fzf's native `reload` mechanism resulting in a
better UI experience (cursor remains in place and no UI "flash")
  • Loading branch information
ibhagwan committed Apr 20, 2023
1 parent 44de5b6 commit 8cee7f5
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 179 deletions.
2 changes: 1 addition & 1 deletion lua/fzf-lua/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ M.buf_del = function(selected, opts)
local vimcmd = "bd"
local bufnrs = vim.tbl_filter(function(line)
local b = tonumber(line:match("%[(%d+)"))
return not utils.buffer_is_dirty(b, true, false)
return b and not utils.buffer_is_dirty(b, true, false)
end, selected)
M.vimcmd_buf(vimcmd, bufnrs, opts)
end
Expand Down
26 changes: 26 additions & 0 deletions lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ local ACTION_DEFINITIONS = {
[actions.grep_lgrep] = { "Regex Search", fn_reload = "Fuzzy Search" },
[actions.sym_lsym] = { "Live Query", fn_reload = "Fuzzy Search" },
[actions.buf_del] = { "close" },
[actions.arg_del] = { "delete" },
[actions.git_reset] = { "reset" },
[actions.git_stage] = { "stage", pos = 1 },
[actions.git_unstage] = { "unstage", pos = 2 },
[actions.git_stage_unstage] = { "[un-]stage", pos = 1 },
[actions.git_stash_drop] = { "drop a stash" },
}

-- converts contents array sent to `fzf_exec` into a single contents
Expand Down Expand Up @@ -740,6 +742,30 @@ M.set_header = function(opts, hdr_tbl)
return opts
end

-- converts actions defined inside 'reload_actions' to use fzf's 'reload'
-- bind, provides a better UI experience without a visible interface refresh
M.convert_reload_actions = function(reload_cmd, opts)
assert(type(reload_cmd) == "string")
if opts._is_skim or not opts.reload_actions then
return opts
end
for k, v in pairs(opts.actions) do
local action = type(v) == "function" and v or type(v) == "table" and v[1]
if type(action) == "function" and opts.reload_actions[action] then
-- replace the action with shell cmd proxy to the original action
local shell_action = shell.raw_action(function(items, _, _)
action(items, opts)
end, "{+}", opts.debug)
opts.keymap.fzf[k] = {
string.format("execute-silent(%s)+reload(%s)", shell_action, reload_cmd),
desc = config.get_action_helpstr(action)
}
opts.actions[k] = nil
end
end
return opts
end

M.setup_fzf_interactive_flags = function(command, fzf_field_expression, opts)
-- query cannot be 'nil'
opts.query = opts.query or ""
Expand Down
77 changes: 42 additions & 35 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,27 @@ M.defaults.git = {
_actions = function() return M.globals.actions.files end,
},
status = {
prompt = "GitStatus> ",
prompt = "GitStatus> ",
-- override `color.status=always`, techincally not required
-- since we now also call `utils.strip_ansi_coloring` (#706)
cmd = "git -c color.status=false status -s",
previewer = "git_diff",
file_icons = true and M._has_devicons,
color_icons = true,
git_icons = true,
_actions = function() return M.globals.actions.files end,
actions = {
cmd = "git -c color.status=false status -s",
previewer = "git_diff",
file_icons = true and M._has_devicons,
color_icons = true,
git_icons = true,
_actions = function() return M.globals.actions.files end,
actions = {
["right"] = { actions.git_unstage, actions.resume },
["left"] = { actions.git_stage, actions.resume },
["ctrl-x"] = { actions.git_reset, actions.resume },
},
-- actions listed below will be converted to fzf's 'reload'
reload_actions = {
[actions.git_reset] = true,
[actions.git_stage] = true,
[actions.git_unstage] = true,
[actions.git_stage_unstage] = true,
},
},
commits = {
prompt = "Commits> ",
Expand Down Expand Up @@ -276,14 +283,15 @@ M.defaults.git = {
},
},
stash = {
prompt = "Stash> ",
cmd = "git --no-pager stash list",
preview = "git --no-pager stash show --patch --color {1}",
actions = {
prompt = "Stash> ",
cmd = "git --no-pager stash list",
preview = "git --no-pager stash show --patch --color {1}",
actions = {
["default"] = actions.git_stash_apply,
["ctrl-x"] = { actions.git_stash_drop, actions.resume },
},
fzf_opts = {
reload_actions = { [actions.git_stash_drop] = true },
fzf_opts = {
-- TODO: multiselect requires more work as dropping
-- a stash changes the stash index, causing an error
-- when the next stash is attempted
Expand Down Expand Up @@ -324,16 +332,15 @@ M.defaults.grep = {
}

M.defaults.args = {
previewer = M._default_previewer_fn,
prompt = "Args> ",
files_only = true,
file_icons = true and M._has_devicons,
color_icons = true,
git_icons = true,
_actions = function() return M.globals.actions.files end,
actions = {
["ctrl-x"] = { actions.arg_del, actions.resume }
},
previewer = M._default_previewer_fn,
prompt = "Args> ",
files_only = true,
file_icons = true and M._has_devicons,
color_icons = true,
git_icons = true,
_actions = function() return M.globals.actions.files end,
actions = { ["ctrl-x"] = { actions.arg_del, actions.resume } },
reload_actions = { [actions.arg_del] = true },
}

M.defaults.oldfiles = {
Expand Down Expand Up @@ -394,24 +401,24 @@ M.defaults.buffers = {
cwd = nil,
fzf_opts = { ["--tiebreak"] = "index", },
_actions = function() return M.globals.actions.buffers end,
actions = {
["ctrl-x"] = { actions.buf_del, actions.resume },
},
actions = { ["ctrl-x"] = { actions.buf_del, actions.resume } },
reload_actions = { [actions.buf_del] = true },
}

M.defaults.tabs = {
previewer = M._default_previewer_fn,
prompt = "Tabs> ",
tab_title = "Tab",
tab_marker = "<<",
file_icons = true and M._has_devicons,
color_icons = true,
_actions = function() return M.globals.actions.buffers end,
actions = {
previewer = M._default_previewer_fn,
prompt = "Tabs> ",
tab_title = "Tab",
tab_marker = "<<",
file_icons = true and M._has_devicons,
color_icons = true,
_actions = function() return M.globals.actions.buffers end,
actions = {
["default"] = actions.buf_switch,
["ctrl-x"] = { actions.buf_del, actions.resume },
},
fzf_opts = {
reload_actions = { [actions.buf_del] = true },
fzf_opts = {
["--delimiter"] = "'[\\):]'",
["--with-nth"] = "2..",
},
Expand Down

0 comments on commit 8cee7f5

Please sign in to comment.