-
|
I'am writting a plugin and I am having a problem related to fzf-lua. At some point, a command of my plugin open fzf-lua to allow the user to selected one or more entry. What I'd like to do is having all the entry selected by default. I'd also like that when enter is pressed with no entry selected, an empty array is returned. I tried to find a solution but I'am still clueless on how to do it. Do you have any solution ? Here is a simplified version of my code: local fzf_lua = require("fzf-lua")
local builtin = require("fzf-lua.previewer.builtin")
spec = spec or {}
local display_items = {}
local display_to_processed_item = {}
if spec.items then
local entry_maker_to_use = spec.entry_maker
or function(item)
local value, display, filename, lnum, col
if type(item) == "table" then
value = item.value or item
display = item.display or item.label or item.name or tostring(value)
filename = item.filename or item.file_path
lnum = item.lnum or item.line or item.row
col = item.col
else
value = item
display = tostring(item)
filename = tostring(item)
end
if type(value) == "table" and string.match(display, "^table: 0x") then
display = value.display or value.label or value.name or display
end
if lnum then
lnum = tonumber(lnum)
end
if col then
col = tonumber(col)
end
return { value = value, display = display, filename = filename, lnum = lnum, col = col }
end
for _, item in ipairs(spec.items) do
local processed = entry_maker_to_use(item)
local display_key = processed.display or ""
table.insert(display_items, display_key)
display_to_processed_item[display_key] = processed
end
end
local fzf_opts = {
prompt = spec.title or "Select Item> ",
cwd = spec.cwd or vim.loop.cwd(),
fzf_opts = { ["--multi"] = true },
actions = {
["default"] = function(selected_list)
if not selected_list then
selected_list = {}
end
local results = {}
for _, display_key in ipairs(selected_list) do
local item = display_to_processed_item[display_key]
if item then
table.insert(results, item.value)
end
end
if spec.on_submit then
vim.schedule(function()
spec.on_submit(results)
end)
end
end,
["ctrl-c"] = function()
if spec.on_cancel then
vim.schedule(spec.on_cancel)
end
end,
},
}
if spec.preview_enabled ~= false then
local GenericFzfPreviewer = builtin.buffer_or_file:extend()
function GenericFzfPreviewer:new(o, opts, fzf_win)
GenericFzfPreviewer.super.new(self, o, opts, fzf_win)
setmetatable(self, GenericFzfPreviewer)
return self
end
function GenericFzfPreviewer:parse_entry(entry_str)
local item = display_to_processed_item[entry_str]
if item and item.filename and type(item.filename) == "string" then
return {
path = item.filename,
line = item.lnum,
col = item.col,
}
end
return {}
end
fzf_opts.previewer = GenericFzfPreviewer
end
if #display_items > 0 then
fzf_lua.fzf_exec(display_items, fzf_opts)
elseif spec.exec_cmd then
fzf_lua.fzf_exec(spec.exec_cmd, fzf_opts)
end |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
|
On mobile so haven’t seen all the code yet but for pre select you can use |
Beta Was this translation helpful? Give feedback.
This is how the fzf field index {+} works, might be solveable using field_index with {n} as well, I’ll test this when I get a chance.
you did good, maybe use
loadevent instead of start? I’ll have to test this when not AFK as well.