Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 69 additions & 9 deletions lua/opencode/ui/base_picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local Promise = require('opencode.promise')
---@field multi_selection? table<string, boolean> Actions that support multi-selection
---@field preview? "file"|"none"|false Preview mode: "file" for file preview, "none" or false to disable
---@field layout_opts? OpencodeUIPickerConfig
---@field close? fun() Close the picker programmatically (set by the backend)

---@class TelescopeEntry
---@field value any
Expand Down Expand Up @@ -147,6 +148,11 @@ local function telescope_ui(opts)
width = opts.width + 7, -- extra space for telescope UI
} or nil,
attach_mappings = function(prompt_bufnr, map)
opts.close = function()
selection_made = true
actions.close(prompt_bufnr)
end

actions.select_default:replace(function()
selection_made = true
local selection = action_state.get_selected_entry()
Expand Down Expand Up @@ -197,8 +203,12 @@ local function telescope_ui(opts)
local new_items = action.fn(items_to_process, opts)
Promise.wrap(new_items):and_then(function(resolved_items)
if action.reload and resolved_items then
opts.items = resolved_items
refresh_picker()
if #resolved_items == 0 and opts.close then
opts.close()
else
opts.items = resolved_items
refresh_picker()
end
end
end)
end
Expand Down Expand Up @@ -295,9 +305,33 @@ local function fzf_ui(opts)
end)
end

local closed = false

opts.close = function()
if closed then
return
end
closed = true
vim.schedule(function()
local ok, fzf_win = pcall(require, 'fzf-lua.win')
if ok and fzf_win.__SELF then
local win = fzf_win.__SELF()
if win then
win:close()
end
end
if opts.callback then
opts.callback(nil)
end
end)
end

---@type FzfLuaActions
local actions_config = {
['default'] = function(selected, fzf_opts)
if closed then
return
end
if not selected or #selected == 0 then
if opts.callback then
opts.callback(nil)
Expand All @@ -310,6 +344,9 @@ local function fzf_ui(opts)
end
end,
['esc'] = function()
if closed then
return
end
if opts.callback then
opts.callback(nil)
end
Expand Down Expand Up @@ -346,8 +383,12 @@ local function fzf_ui(opts)
Promise.wrap(new_items):and_then(function(resolved_items)
if action.reload and resolved_items then
---@cast resolved_items any[]
opts.items = resolved_items
refresh_fzf()
if #resolved_items == 0 and opts.close then
opts.close()
else
opts.items = resolved_items
refresh_fzf()
end
end
end)
end
Expand Down Expand Up @@ -376,6 +417,10 @@ local function mini_pick_ui(opts)

local mappings = {}

opts.close = function()
mini_pick.stop()
end

for action_name, action in pairs(opts.actions) do
if action.key and action.key[1] then
mappings[action_name] = {
Expand All @@ -387,8 +432,12 @@ local function mini_pick_ui(opts)
local new_items = action.fn(current.item, opts)
Promise.wrap(new_items):and_then(function(resolved_items)
if action.reload and resolved_items then
opts.items = resolved_items
mini_pick_ui(opts)
if #resolved_items == 0 and opts.close then
opts.close()
else
opts.items = resolved_items
mini_pick_ui(opts)
end
end
end)
end
Expand Down Expand Up @@ -522,6 +571,13 @@ local function snacks_picker_ui(opts)
snack_opts.win.input.keys[action.key[1]] = { action_name, mode = action.key.mode or 'i' }

snack_opts.actions[action_name] = function(_picker, item)
if not opts.close then
opts.close = function()
selection_made = true
_picker:close()
end
end

if item then
local items_to_process
if action.multi_selection then
Expand All @@ -540,9 +596,13 @@ local function snacks_picker_ui(opts)
local new_items = action.fn(items_to_process, opts)
Promise.wrap(new_items):and_then(function(resolved_items)
if action.reload and resolved_items then
opts.items = resolved_items
_picker:refresh()
_picker:find()
if #resolved_items == 0 and opts.close then
opts.close()
else
opts.items = resolved_items
_picker:refresh()
_picker:find()
end
end
end)
end)
Expand Down
Loading