added 'resume': will resume **any** list, custom or builtin (#241)

main
bhagwan 3 years ago
parent 18837e1174
commit 1ece1ab09a

@ -181,6 +181,7 @@ vim.api.nvim_set_keymap('n', '<c-P>',
### Misc
| Command | List |
| ---------------- | ------------------------------------------ |
| `resume` | resume last command/query |
| `builtin` | fzf-lua builtin commands |
| `help_tags` | help tags |
| `man_pages` | man pages |
@ -212,6 +213,7 @@ Consult the list below for available settings:
```lua
local actions = require "fzf-lua.actions"
require'fzf-lua'.setup {
global_resume = true, -- enable `resume` provider?
winopts = {
-- split = "belowright new",-- open in a split instead?
-- "belowright new" : split below

@ -211,6 +211,7 @@ MISC *fzf-lua-misc*
| Command | List |
| ---------------- | ------------------------------------------ |
| `resume` | resume last command/query |
| `builtin` | fzf-lua builtin commands |
| `help_tags` | help tags |
| `man_pages` | man pages |
@ -246,6 +247,7 @@ Consult the list below for available settings:
>
local actions = require "fzf-lua.actions"
require'fzf-lua'.setup {
global_resume = true, -- enable `resume` provider?
winopts = {
-- split = "belowright new",-- open in a split instead?
-- "belowright new" : split below

@ -20,7 +20,12 @@ function M._default_previewer_fn()
return M.globals.default_previewer or M.globals.winopts.preview.default
end
-- set this so that make_entry won't
-- get nil err when setting remotely
M.__resume_data = {}
M.globals = {
global_resume = true,
winopts = {
height = 0.85,
width = 0.80,

@ -1,4 +1,3 @@
-- local fzf = require "fzf"
local fzf = require "fzf-lua.fzf"
local path = require "fzf-lua.path"
local utils = require "fzf-lua.utils"
@ -11,7 +10,55 @@ local make_entry = require "fzf-lua.make_entry"
local M = {}
M.fzf_resume = function()
if not config.__resume_data or not config.__resume_data.opts then
utils.info("No resume data available, is 'global_resume' enabled?")
return
end
local opts = config.__resume_data.opts
local last_query = config.__resume_data.last_query
if last_query and #last_query>0 then
last_query = vim.fn.shellescape(last_query)
else
-- in case we continue from another resume
-- reset the previous query which was saved
-- inside "fzf_opts['--query']" argument
last_query = false
end
opts.fzf_opts['--query'] = last_query
if opts.__FNCREF__ then
-- HACK for 'live_grep' and 'lsp_live_workspace_symbols'
opts.__FNCREF__({ continue_last_search = true })
else
M.fzf_wrap(opts, config.__resume_data.contents)()
end
end
M.fzf_wrap = function(opts, contents, fn_post)
return coroutine.wrap(function()
opts.fn_post = opts.fn_post or fn_post
local selected = M.fzf(opts, contents)
if opts.fn_post then
opts.fn_post(selected)
end
end)
end
M.fzf = function(opts, contents)
-- support global resume?
if config.globals.global_resume and not opts.no_global_resume then
config.__resume_data = config.__resume_data or {}
config.__resume_data.opts = vim.deepcopy(opts)
config.__resume_data.contents = contents and vim.deepcopy(contents) or nil
if not opts._is_skim and not opts.no_global_resume_act then
local raw_act = shell.raw_action(function(args)
config.__resume_data.last_query = args[1]
end, "{q}")
opts._fzf_cli_args = ('--bind=change:execute-silent:%s'):
format(vim.fn.shellescape(raw_act))
end
config.__resume_data.last_query = nil
end
-- normalize with globals if not already normalized
if not opts._normalized then
opts = config.normalize_opts(opts, {})
@ -334,9 +381,8 @@ M.fzf_files = function(opts, contents)
if not opts then return end
coroutine.wrap(function ()
local selected = M.fzf(opts, contents or opts.fzf_fn)
M.fzf_wrap(opts, contents or opts.fzf_fn, function(selected)
if opts.post_select_cb then
opts.post_select_cb()

@ -79,6 +79,8 @@ function M.setup(opts)
globals = nil
end
M.resume = require'fzf-lua.core'.fzf_resume
M.files = require'fzf-lua.providers.files'.files
M.files_resume = require'fzf-lua.providers.files'.files_resume
M.args = require'fzf-lua.providers.files'.args
@ -155,6 +157,7 @@ end
-- API shortcuts
M.fzf = require'fzf-lua.core'.fzf
M.fzf_wrap = require'fzf-lua.core'.fzf_wrap
M.raw_fzf = require'fzf-lua.fzf'.raw_fzf
M.builtin = function(opts)
@ -163,6 +166,7 @@ M.builtin = function(opts)
opts.metatable_exclude = {
["setup"] = false,
["fzf"] = false,
["fzf_wrap"] = false,
["raw_fzf"] = false,
}
for _, m in ipairs(_modules) do

@ -188,8 +188,10 @@ M.preprocess = function(opts)
-- save our last search argument for resume
if opts.cmd:match("{argv1}") then
local query = argv(1)
set_config_section('globals.grep._last_search',
{ query = argv(1), no_esc = true })
{ query = query, no_esc = true })
set_config_section('__resume_data.last_query', query)
end
-- did the caller request rg with glob support?

@ -169,7 +169,6 @@ M.buffers = function(opts)
if not next(filtered) then return end
coroutine.wrap(function ()
local items = {}
local buffers, header_line = make_buffer_entries(opts, filtered, nil, opts.curbuf)
@ -182,7 +181,8 @@ M.buffers = function(opts)
opts.fzf_opts['--header-lines'] = '1'
end
local selected = core.fzf(opts, items)
core.fzf_wrap(opts, items, function(selected)
if not selected then return end
actions.act(opts.actions, selected, opts)
@ -209,7 +209,6 @@ M.buffer_lines = function(opts)
opts.current_buffer_only and { vim.api.nvim_get_current_buf() } or
vim.api.nvim_list_bufs())
coroutine.wrap(function()
local items = {}
for _, bufnr in ipairs(buffers) do
@ -251,7 +250,7 @@ M.buffer_lines = function(opts)
opts.fzf_opts['--query'] = vim.fn.shellescape(opts.search)
end
local selected = core.fzf(opts, items)
core.fzf_wrap(opts, items, function(selected)
if not selected then return end
-- get the line number
@ -300,7 +299,7 @@ M.tabs = function(opts)
bufnrs[b] = nil
end
end
coroutine.wrap(function ()
local items = {}
for t, bufnrs in pairs(opts._tab_to_buf) do
@ -327,7 +326,7 @@ M.tabs = function(opts)
opts.fzf_opts["--delimiter"] = vim.fn.shellescape('[\\)]')
opts.fzf_opts["--with-nth"] = '2'
local selected = core.fzf(opts, items)
core.fzf_wrap(opts, items, function(selected)
if not selected then return end

@ -18,7 +18,6 @@ M.colorschemes = function(opts)
opts = config.normalize_opts(opts, config.globals.colorschemes)
if not opts then return end
coroutine.wrap(function ()
local prev_act = shell.action(function (args)
if opts.live_preview and args then
local colorscheme = args[1]
@ -36,7 +35,7 @@ M.colorschemes = function(opts)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'nohidden:right:0'
local selected = core.fzf(opts, colors)
core.fzf_wrap(opts, colors, function(selected)
-- reset color scheme if live_preview is enabled
-- and nothing or non-default action was selected

@ -43,26 +43,8 @@ M.files = function(opts)
return core.fzf_files(opts, contents)
end
local last_query = ""
M.files_resume = function(opts)
opts = config.normalize_opts(opts, config.globals.files)
if not opts then return end
if opts._is_skim then
utils.info("'files_resume' is not supported with 'sk'")
return
end
local raw_act = shell.raw_action(function(args)
last_query = args[1]
end, "{q}")
opts.fzf_opts['--query'] = vim.fn.shellescape(last_query)
opts._fzf_cli_args = ('--bind=change:execute-silent:%s'):
format(vim.fn.shellescape(raw_act))
return M.files(opts)
M.files_resume = function(_)
utils.warn("'files_resume' was deprecated, please use global 'resume' instead.")
end
M.args = function(opts)

@ -48,9 +48,8 @@ end
local function git_cmd(opts)
opts.cwd = path.git_root(opts.cwd)
if not opts.cwd then return end
coroutine.wrap(function ()
opts = core.set_header(opts, 2)
local selected = core.fzf(opts, opts.cmd)
core.fzf_wrap(opts, opts.cmd, function(selected)
if not selected then return end
actions.act(opts.actions, selected, opts)
end)()

@ -13,6 +13,9 @@ local function set_last_search(query, no_esc)
query = query,
no_esc = no_esc
}
if config.__resume_data then
config.__resume_data.last_query = query
end
end
local M = {}
@ -147,6 +150,10 @@ M.live_grep_st = function(opts)
end
end
-- disable global resume
-- conflicts with 'change:reload' event
opts.no_global_resume_act = true
opts.__FNCREF__ = opts.__FNCREF__ or utils.__FNCREF__()
opts = core.set_fzf_line_args(opts)
opts = core.set_fzf_interactive_cmd(opts)
core.fzf_files(opts)
@ -259,6 +266,10 @@ M.live_grep_mt = function(opts)
("%s || true"):format(reload_command))))
end
-- disable global resume
-- conflicts with 'change:reload' event
opts.no_global_resume_act = true
opts.__FNCREF__ = opts.__FNCREF__ or utils.__FNCREF__()
opts = core.set_fzf_line_args(opts)
core.fzf_files(opts)
opts.search = nil
@ -374,6 +385,7 @@ end
M.lgrep_curbuf = function(opts)
if not opts then opts = {} end
opts.lgrep = true
opts.__FNCREF__ = utils.__FNCREF__()
return M.grep_curbuf(opts)
end

@ -96,15 +96,13 @@ M.helptags = function(opts)
opts = config.normalize_opts(opts, config.globals.helptags)
if not opts then return end
coroutine.wrap(function ()
-- local prev_act = action(function (args) end)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
opts.fzf_opts['--nth'] = '1'
local selected = core.fzf(opts, fzf_function)
core.fzf_wrap(opts, fzf_function, function(selected)
if not selected then return end

@ -219,6 +219,9 @@ local function set_lsp_fzf_fn(opts)
coroutine.wrap(function ()
local co = coroutine.running()
-- reset number of callbacks incase
-- we're being called from 'resume'
opts.num_callbacks = 0
-- cancel all currently running requests
-- can happen when using `live_ws_symbols`
@ -436,9 +439,7 @@ M.code_actions = function(opts)
-- error or no sync request no results
if not opts.fzf_fn then return end
coroutine.wrap(function ()
local selected = core.fzf(opts, opts.fzf_fn)
core.fzf_wrap(opts, opts.fzf_fn, function(selected)
if opts.post_select_cb then
opts.post_select_cb()
@ -627,6 +628,7 @@ M.live_workspace_symbols = function(opts)
if query and not (opts.save_last_search == false) then
last_search = {}
last_search.query = query
config.__resume_data.last_query = query
end
opts.sync = true
opts.async = false
@ -635,6 +637,10 @@ M.live_workspace_symbols = function(opts)
return opts.fzf_fn
end
-- disable global resume
-- conflicts with 'change:reload' event
opts.no_global_resume_act = true
opts.__FNCREF__ = M.live_workspace_symbols
opts = core.set_fzf_interactive_cb(opts)
opts = core.set_fzf_line_args(opts)
core.fzf_files(opts)

@ -27,8 +27,6 @@ M.manpages = function(opts)
opts = config.normalize_opts(opts, config.globals.manpages)
if not opts then return end
coroutine.wrap(function ()
local fzf_fn = libuv.spawn_nvim_fzf_cmd(
{ cmd = opts.cmd, cwd = opts.cwd, pid_cb = opts._pid_cb },
function(x)
@ -43,7 +41,7 @@ M.manpages = function(opts)
opts.fzf_opts['--tiebreak'] = 'begin'
opts.fzf_opts['--nth'] = '1,2'
local selected = core.fzf(opts, fzf_fn)
core.fzf_wrap(opts, fzf_fn, function(selected)
if not selected then return end

@ -12,8 +12,6 @@ M.metatable = function(opts)
if not opts.metatable then opts.metatable = getmetatable('').__index end
coroutine.wrap(function ()
local prev_act = shell.action(function (args)
-- TODO: retreive method help
local help = ''
@ -33,7 +31,7 @@ M.metatable = function(opts)
opts.fzf_opts['--preview-window'] = 'hidden:down:10'
opts.fzf_opts['--no-multi'] = ''
local selected = core.fzf(opts, methods)
core.fzf_wrap(opts, methods, function(selected)
if not selected then return end

@ -11,8 +11,6 @@ M.commands = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.commands)
if not opts then return end
coroutine.wrap(function ()
local commands = vim.api.nvim_get_commands {}
local prev_act = shell.action(function (args)
@ -33,7 +31,7 @@ M.commands = function(opts)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview'] = prev_act
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)
@ -43,7 +41,6 @@ M.commands = function(opts)
end
local history = function(opts, str)
coroutine.wrap(function ()
local history = vim.fn.execute("history " .. str)
history = vim.split(history, "\n")
@ -58,7 +55,7 @@ local history = function(opts, str)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)
@ -91,8 +88,6 @@ M.jumps = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.jumps)
if not opts then return end
coroutine.wrap(function ()
local jumps = vim.fn.execute("jumps")
jumps = vim.split(jumps, "\n")
@ -109,7 +104,7 @@ M.jumps = function(opts)
opts.fzf_opts['--no-multi'] = ''
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected, opts)
@ -121,8 +116,6 @@ M.marks = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.marks)
if not opts then return end
coroutine.wrap(function ()
local marks = vim.fn.execute("marks")
marks = vim.split(marks, "\n")
@ -155,7 +148,7 @@ M.marks = function(opts)
opts.fzf_opts['--preview'] = prev_act
opts.fzf_opts['--no-multi'] = ''
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)
@ -168,8 +161,6 @@ M.registers = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.registers)
if not opts then return end
coroutine.wrap(function ()
local registers = { '"', "_", "#", "=", "_", "/", "*", "+", ":", ".", "%" }
-- named
for i = 0, 9 do
@ -202,7 +193,7 @@ M.registers = function(opts)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview'] = prev_act
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)
@ -215,8 +206,6 @@ M.keymaps = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.keymaps)
if not opts then return end
coroutine.wrap(function ()
local modes = { "n", "i", "c" }
local keymaps = {}
@ -262,7 +251,7 @@ M.keymaps = function(opts)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview'] = prev_act
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)
@ -276,8 +265,6 @@ M.spell_suggest = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.spell_suggest)
if not opts then return end
coroutine.wrap(function ()
local cursor_word = vim.fn.expand "<cword>"
local entries = vim.fn.spellsuggest(cursor_word)
@ -286,7 +273,7 @@ M.spell_suggest = function(opts)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)
@ -300,15 +287,13 @@ M.filetypes = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.filetypes)
if not opts then return end
coroutine.wrap(function ()
local entries = vim.fn.getcompletion('', 'filetype')
if vim.tbl_isempty(entries) then return end
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)
@ -322,8 +307,6 @@ M.packadd = function(opts)
opts = config.normalize_opts(opts, config.globals.nvim.packadd)
if not opts then return end
coroutine.wrap(function ()
local entries = vim.fn.getcompletion('', 'packadd')
if vim.tbl_isempty(entries) then return end
@ -331,7 +314,7 @@ M.packadd = function(opts)
opts.fzf_opts['--no-multi'] = ''
opts.fzf_opts['--preview-window'] = 'hidden:right:0'
local selected = core.fzf(opts, entries)
core.fzf_wrap(opts, entries, function(selected)
if not selected then return end
actions.act(opts.actions, selected)

@ -8,6 +8,11 @@ end
local M = {}
function M.__FILE__() return debug.getinfo(2, 'S').source end
function M.__LINE__() return debug.getinfo(2, 'l').currentline end
function M.__FNC__() return debug.getinfo(2, 'n').name end
function M.__FNCREF__() return debug.getinfo(2, 'f').func end
-- sets an invisible unicode character as icon seaprator
-- the below was reached after many iterations, a short summary of everything
-- that was tried and why it failed:

Loading…
Cancel
Save