internal: added 'win.save_query', rename internal selection callback

This commit is contained in:
bhagwan 2021-12-20 21:26:31 -08:00
parent 21f19cac1a
commit f0927af072
3 changed files with 32 additions and 13 deletions

View File

@ -35,12 +35,12 @@ M.fzf_resume = function()
end
end
M.fzf_wrap = function(opts, contents, fn_post)
M.fzf_wrap = function(opts, contents, fn_selected)
return coroutine.wrap(function()
opts.fn_post = opts.fn_post or fn_post
opts.fn_selected = opts.fn_selected or fn_selected
local selected = M.fzf(opts, contents)
if opts.fn_post then
opts.fn_post(selected)
if opts.fn_selected then
opts.fn_selected(selected)
end
end)
end
@ -50,6 +50,9 @@ M.fzf = function(opts, contents)
if not opts._normalized then
opts = config.normalize_opts(opts, {})
end
if opts.fn_pre_win then
opts.fn_pre_win(opts)
end
-- support global resume?
if opts.global_resume then
config.__resume_data = config.__resume_data or {}
@ -430,10 +433,6 @@ M.fzf_files = function(opts, contents)
local idx = utils.tbl_length(opts.actions)>1 and 2 or 1
for i = idx, #selected do
selected[i] = path.entry_to_file(selected[i], opts.cwd).stripped
if opts.cb_selected then
local cb_ret = opts.cb_selected(opts, selected[i])
if cb_ret then selected[i] = cb_ret end
end
end
end

View File

@ -31,10 +31,6 @@ local quickfix_run = function(opts, cfg, locations)
utils.delayed_cb(cb)
end
--[[ opts.cb_selected = function(_, x)
return x
end ]]
opts = core.set_fzf_line_args(opts)
return core.fzf_files(opts, contents)
end

View File

@ -301,7 +301,8 @@ function FzfWin:new(o)
self.preview_border = not opt_matches(o, 'border', 'noborder')
self.keymap = o.keymap
self.previewer = o.previewer
self.previewer_type = o.previewer_type
self.prompt = o.prompt
self.fn_save_query = o.fn_save_query
self._orphaned_bufs = {}
self:_set_autoclose(o.autoclose)
_self = self
@ -568,11 +569,34 @@ function FzfWin:redraw()
end
end
function FzfWin.save_query()
local self = _self
if not self then return end
local lines = vim.api.nvim_buf_get_lines(self.fzf_bufnr, 0, 1, false)
if not lines or vim.tbl_isempty(lines) then return end
local query = lines[1]:gsub("^"..self.prompt, ""):match("[^<]+")
-- trim whitespaces at the end
query = query and query:gsub("%s*$", "")
if self.fn_save_query then
self.fn_save_query(query)
end
-- print("query:", query)
end
function FzfWin:set_winleave_autocmd()
vim.cmd("augroup FzfLua")
vim.cmd("au!")
vim.cmd(('au WinLeave <buffer> %s'):format(
[[lua require('fzf-lua.win').win_leave()]]))
--[[ if self.fn_save_query then
-- if resume is enabled fire an event for every
-- key pressed so we can retreieve the typed query
-- TODO: InsertCharPre does not work for terminals
-- find another way to trigger save_query()
-- https://github.com/neovim/neovim/issues/5018
vim.cmd(('au InsertCharPre <buffer> %s'):format(
"lua require('fzf-lua.win').save_query()"))
end ]]
vim.cmd("augroup END")
end