Skip to content

Commit 2e0927e

Browse files
committed
feat: Automatically open files in file buffers
1 parent 61c3cc7 commit 2e0927e

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

lua/fff/picker_ui.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,40 @@ function M.scroll_preview_down()
11711171
preview.scroll(scroll_lines)
11721172
end
11731173

1174+
--- Find the first visible window with a normal file buffer
1175+
--- @return number|nil Window ID of the first suitable window, or nil if none found
1176+
local function find_suitable_window()
1177+
local current_tabpage = vim.api.nvim_get_current_tabpage()
1178+
local windows = vim.api.nvim_tabpage_list_wins(current_tabpage)
1179+
1180+
for _, win in ipairs(windows) do
1181+
if vim.api.nvim_win_is_valid(win) then
1182+
local buf = vim.api.nvim_win_get_buf(win)
1183+
if vim.api.nvim_buf_is_valid(buf) then
1184+
local buftype = vim.api.nvim_buf_get_option(buf, 'buftype')
1185+
local modifiable = vim.api.nvim_buf_get_option(buf, 'modifiable')
1186+
local filetype = vim.api.nvim_buf_get_option(buf, 'filetype')
1187+
1188+
local is_picker_window = (
1189+
win == M.state.input_win
1190+
or win == M.state.list_win
1191+
or win == M.state.preview_win
1192+
or win == M.state.file_info_win
1193+
)
1194+
1195+
if (buftype == '' or buftype == 'acwrite') and
1196+
modifiable and
1197+
not is_picker_window and
1198+
filetype ~= 'undotree' then
1199+
return win
1200+
end
1201+
end
1202+
end
1203+
end
1204+
1205+
return nil
1206+
end
1207+
11741208
function M.select(action)
11751209
if not M.state.active then return end
11761210

@@ -1187,6 +1221,21 @@ function M.select(action)
11871221
M.close()
11881222

11891223
if action == 'edit' then
1224+
local current_buf = vim.api.nvim_get_current_buf()
1225+
local current_buftype = vim.api.nvim_buf_get_option(current_buf, 'buftype')
1226+
local current_buf_modifiable = vim.api.nvim_buf_get_option(current_buf, 'modifiable')
1227+
1228+
-- If current active buffer is not a normal buffer we find a suitable window with a tab otherwise opening a new split
1229+
if current_buftype ~= '' or not current_buf_modifiable then
1230+
local suitable_win = find_suitable_window()
1231+
if suitable_win then
1232+
vim.api.nvim_set_current_win(suitable_win)
1233+
else
1234+
vim.cmd('split ' .. vim.fn.fnameescape(relative_path))
1235+
return
1236+
end
1237+
end
1238+
11901239
vim.cmd('edit ' .. vim.fn.fnameescape(relative_path))
11911240
elseif action == 'split' then
11921241
vim.cmd('split ' .. vim.fn.fnameescape(relative_path))

0 commit comments

Comments
 (0)