Skip to content

Commit

Permalink
feat: reuse chat_confirm_delete shortcut in chat picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Robitx committed Jul 29, 2024
1 parent 5ebccef commit 919fdd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
12 changes: 12 additions & 0 deletions lua/gp/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ end
---@param callback function | string # callback or string to set keymap
---@param desc string | nil # optional description for keymap
_H.set_keymap = function(buffers, mode, key, callback, desc)
logger.debug(
"registering shortcut:"
.. " mode: "
.. vim.inspect(mode)
.. " key: "
.. key
.. " buffers: "
.. vim.inspect(buffers)
.. " callback: "
.. vim.inspect(callback)
)
for _, buf in ipairs(buffers) do
vim.keymap.set(mode, key, callback, {
noremap = true,
Expand Down Expand Up @@ -64,6 +75,7 @@ end

---@param file string | nil # name of the file to delete
_H.delete_file = function(file)
logger.debug("deleting file: " .. vim.inspect(file))
if file == nil then
return
end
Expand Down
43 changes: 24 additions & 19 deletions lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ M.cmd.ChatFinder = function()
local right = M.config.style_chat_finder_margin_right or 2
local picker_buf, picker_win, picker_close, picker_resize = M.render.popup(
nil,
"Picker: j/k <Esc>|exit <Enter>|open dd|del i|srch",
"Picker: j/k <Esc>|exit <Enter>|open " .. M.config.chat_shortcut_delete.shortcut .. "|del i|srch",
function(w, h)
local wh = h - top - bottom - 2
local ww = w - left - right - 2
Expand All @@ -1185,7 +1185,7 @@ M.cmd.ChatFinder = function()
local command_buf, command_win, command_close, command_resize = M.render.popup(
nil,
"Search: <Tab>/<Shift+Tab>|navigate <Esc>|picker <C-c>|exit "
.. "<Enter>/<C-f>/<C-x>/<C-v>/<C-t>/<C-g>|open/float/split/vsplit/tab/toggle",
.. "<Enter>/<C-f>/<C-x>/<C-v>/<C-t>/<C-g>t|open/float/split/vsplit/tab/toggle",
function(w, h)
return w - left - right, 1, h - bottom, left
end,
Expand Down Expand Up @@ -1380,7 +1380,7 @@ M.cmd.ChatFinder = function()
M.helpers.set_keymap({ picker_buf, preview_buf, command_buf }, { "i", "n", "v" }, "<C-t>", function()
open_chat(M.BufTarget.tabnew, false)
end)
M.helpers.set_keymap({ picker_buf, preview_buf, command_buf }, { "i", "n", "v" }, "<C-g>", function()
M.helpers.set_keymap({ picker_buf, preview_buf, command_buf }, { "i", "n", "v" }, "<C-g>t", function()
local target = M.resolve_buf_target(M.config.toggle_target)
open_chat(target, true)
end)
Expand Down Expand Up @@ -1408,25 +1408,30 @@ M.cmd.ChatFinder = function()
end)

-- dd on picker or preview window will delete file
M.helpers.set_keymap({ picker_buf, preview_buf }, "n", "dd", function()
local index = vim.api.nvim_win_get_cursor(picker_win)[1]
local file = picker_files[index]

-- delete without confirmation
if not M.config.chat_confirm_delete then
M.helpers.delete_file(file)
refresh_picker()
return
end

-- ask for confirmation
vim.ui.input({ prompt = "Delete " .. file .. "? [y/N] " }, function(input)
if input and input:lower() == "y" then
M.helpers.set_keymap(
{ command_buf, picker_buf, preview_buf },
{ "i", "n", "v" },
M.config.chat_shortcut_delete.shortcut,
function()
local index = vim.api.nvim_win_get_cursor(picker_win)[1]
local file = picker_files[index]

-- delete without confirmation
if not M.config.chat_confirm_delete then
M.helpers.delete_file(file)
refresh_picker()
return
end
end)
end)

-- ask for confirmation
vim.ui.input({ prompt = "Delete " .. file .. "? [y/N] " }, function(input)
if input and input:lower() == "y" then
M.helpers.delete_file(file)
refresh_picker()
end
end)
end
)
end

--------------------------------------------------------------------------------
Expand Down

0 comments on commit 919fdd4

Please sign in to comment.