fix: file selector handler

This commit is contained in:
yetone 2025-01-24 12:47:26 +08:00
parent 5cf70aab95
commit 5b83c48fce
2 changed files with 17 additions and 10 deletions

View File

@ -16,4 +16,5 @@ ignore = {
-- Global objects defined by the C code -- Global objects defined by the C code
read_globals = { read_globals = {
'vim', 'vim',
'Snacks',
} }

View File

@ -14,7 +14,7 @@ local FileSelector = {}
--- @field file_cache string[] --- @field file_cache string[]
--- @field event_handlers table<string, function[]> --- @field event_handlers table<string, function[]>
---@alias FileSelectorHandler fun(self: FileSelector, on_select: fun(on_select: fun(filepath: string)|nil)): nil ---@alias FileSelectorHandler fun(self: FileSelector, on_select: fun(filepaths: string[] | nil)): nil
---@param id integer ---@param id integer
---@return FileSelector ---@return FileSelector
@ -144,13 +144,14 @@ function FileSelector:fzf_ui(handler)
actions = { actions = {
["default"] = function(selected) ["default"] = function(selected)
if not selected or #selected == 0 then return close_action() end if not selected or #selected == 0 then return close_action() end
---@type string[]
local selections = {} local selections = {}
for _, entry in ipairs(selected) do for _, entry in ipairs(selected) do
local file = fzf_lua.path.entry_to_file(entry) local file = fzf_lua.path.entry_to_file(entry)
if file and file.path then table.insert(selections, file.path) end if file and file.path then table.insert(selections, file.path) end
end end
if #selections > 0 then handler(#selections == 1 and selections[1] or selections) end handler(selections)
end, end,
["esc"] = close_action, ["esc"] = close_action,
["ctrl-c"] = close_action, ["ctrl-c"] = close_action,
@ -168,7 +169,6 @@ function FileSelector:mini_pick_ui(handler)
end end
function FileSelector:snacks_picker_ui(handler) function FileSelector:snacks_picker_ui(handler)
---@diagnostic disable-next-line: undefined-global
Snacks.picker.files({ Snacks.picker.files({
exclude = self.selected_filepaths, exclude = self.selected_filepaths,
confirm = function(picker) confirm = function(picker)
@ -213,11 +213,11 @@ function FileSelector:telescope_ui(handler)
action_utils.map_selections(prompt_bufnr, function(selection) table.insert(selections, selection[1]) end) action_utils.map_selections(prompt_bufnr, function(selection) table.insert(selections, selection[1]) end)
if #selections > 0 then handler(selections) end handler(selections)
else else
local selection = action_state.get_selected_entry() local selections = action_state.get_selected_entry()
if selection and #selection > 0 then handler(selection[1]) end handler(selections)
end end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
@ -238,17 +238,23 @@ function FileSelector:native_ui(handler)
vim.ui.select(filepaths, { vim.ui.select(filepaths, {
prompt = string.format("%s:", PROMPT_TITLE), prompt = string.format("%s:", PROMPT_TITLE),
format_item = function(item) return item end, format_item = function(item) return item end,
}, handler) }, function(item)
if item then
handler({ item })
else
handler(nil)
end
end)
end end
---@return nil ---@return nil
function FileSelector:show_select_ui() function FileSelector:show_select_ui()
---@param filepaths string[] | nil
---@return nil
local handler = function(filepaths) local handler = function(filepaths)
if not filepaths then return end if not filepaths then return end
-- Convert single filepath to array for unified handling
local paths = type(filepaths) == "string" and { filepaths } or filepaths
for _, filepath in ipairs(paths) do for _, filepath in ipairs(filepaths) do
local uniform_path = Utils.uniform_path(filepath) local uniform_path = Utils.uniform_path(filepath)
if Config.file_selector.provider == "native" then if Config.file_selector.provider == "native" then
table.insert(self.selected_filepaths, uniform_path) table.insert(self.selected_filepaths, uniform_path)