Skip to content

Commit 0e938db

Browse files
committed
feat: Automatically open files in file buffers
1 parent 61c3cc7 commit 0e938db

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

lua/fff/picker_ui.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,42 @@ 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
1196+
(buftype == '' or buftype == 'acwrite')
1197+
and modifiable
1198+
and not is_picker_window
1199+
and filetype ~= 'undotree'
1200+
then
1201+
return win
1202+
end
1203+
end
1204+
end
1205+
end
1206+
1207+
return nil
1208+
end
1209+
11741210
function M.select(action)
11751211
if not M.state.active then return end
11761212

@@ -1187,6 +1223,21 @@ function M.select(action)
11871223
M.close()
11881224

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

0 commit comments

Comments
 (0)