Skip to content

Commit c1b9568

Browse files
committed
fix(files/snacks): restore win/mode/cursor on cancel
Snacks doesn't seem to restore the window/mode/cursor position when you close the picker. We're already handling the case when you pick a file but we weren't handling the case when you close the picker without picking a file. Now we save the window, mode, and cursor position and restore those if we didn't pick a file
1 parent 4f1d172 commit c1b9568

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

lua/opencode/ui/file_picker.lua

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ end
101101
local function snacks_picker_ui(callback, path)
102102
local Snacks = require('snacks')
103103

104+
local origin_win = vim.api.nvim_get_current_win()
105+
local origin_mode = vim.fn.mode()
106+
local origin_pos = vim.api.nvim_win_get_cursor(origin_win)
107+
108+
local confirmed = false
109+
104110
local opts = {
105-
confirm = function(picker)
106-
local items = picker:selected({ fallback = true })
107-
picker:close()
111+
confirm = function(picker_obj)
112+
local items = picker_obj:selected({ fallback = true })
113+
confirmed = true
114+
picker_obj:close()
108115

109116
if items and callback then
110117
for _, it in ipairs(items) do
@@ -114,6 +121,20 @@ local function snacks_picker_ui(callback, path)
114121
end
115122
end
116123
end,
124+
on_close = function(obj)
125+
vim.notify(vim.inspect(obj))
126+
-- snacks doesn't seem to restore window / mode / cursor position when you
127+
-- cancel the picker. if we pick a file, we're already handling that case elsewhere
128+
if confirmed or not vim.api.nvim_win_is_valid(origin_win) then
129+
return
130+
end
131+
132+
vim.api.nvim_set_current_win(origin_win)
133+
if origin_mode:match('i') then
134+
vim.cmd('startinsert')
135+
end
136+
vim.api.nvim_win_set_cursor(origin_win, origin_pos)
137+
end,
117138
}
118139

119140
if path then

0 commit comments

Comments
 (0)