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
30 changes: 17 additions & 13 deletions lua/opencode/ui/dialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

---@class Dialog
---@field private _config DialogConfig
---@field private _keymaps integer[] List of keymap IDs for cleanup
---@field private _keymaps string[] List of key bindings for cleanup
---@field private _key_capture_ns integer? Namespace for vim.on_key
---@field private _selected_index integer Currently selected option index
---@field private _active boolean Whether dialog is currently active
Expand Down Expand Up @@ -306,59 +306,63 @@ function Dialog:_setup_keymaps()
if keymaps.up then
for _, key in ipairs(keymaps.up) do
if key and key ~= '' then
local id = vim.keymap.set('n', key, function()
vim.keymap.set('n', key, function()
self:navigate(-1)
end, keymap_opts)
table.insert(self._keymaps, id)
table.insert(self._keymaps, key)
end
end
end

if keymaps.down then
for _, key in ipairs(keymaps.down) do
if key and key ~= '' then
local id = vim.keymap.set('n', key, function()
vim.keymap.set('n', key, function()
self:navigate(1)
end, keymap_opts)
table.insert(self._keymaps, id)
table.insert(self._keymaps, key)
end
end
end

if keymaps.select and keymaps.select ~= '' then
local id = vim.keymap.set('n', keymaps.select, function()
vim.keymap.set('n', keymaps.select, function()
self:select()
end, keymap_opts)
table.insert(self._keymaps, id)
table.insert(self._keymaps, keymaps.select)
end

if keymaps.dismiss and keymaps.dismiss ~= '' then
local id = vim.keymap.set('n', keymaps.dismiss, function()
vim.keymap.set('n', keymaps.dismiss, function()
self:dismiss()
end, keymap_opts)
table.insert(self._keymaps, id)
table.insert(self._keymaps, keymaps.dismiss)
end

if keymaps.number_shortcuts then
local option_count = self._config.get_option_count()
local number_keymap_opts = vim.tbl_extend('force', keymap_opts, { nowait = true })
for i = 1, math.min(option_count, 9) do
local id = vim.keymap.set('n', tostring(i), function()
local key = tostring(i)
vim.keymap.set('n', key, function()
if not self._active or not self._config.check_focused() then
return
end
self._selected_index = i
self._config.on_select(i)
end, number_keymap_opts)
table.insert(self._keymaps, id)
table.insert(self._keymaps, key)
end
end
end

---Clear all buffer-scoped keymaps
function Dialog:_clear_keymaps()
for _, keymap_id in ipairs(self._keymaps) do
pcall(vim.keymap.del, 'n', keymap_id, { buffer = self._config.buffer })
local buf = self._config.buffer
if buf and vim.api.nvim_buf_is_valid(buf) then
for _, key in ipairs(self._keymaps) do
pcall(vim.keymap.del, 'n', key, { buffer = buf })
end
end
self._keymaps = {}
end
Expand Down