Skip to content

Commit dd90bfa

Browse files
authored
fix(#1671): split with no window picker will always find an available window (#1677)
1 parent cb98892 commit dd90bfa

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ local function get_user_input_char()
1515
return vim.fn.nr2char(c)
1616
end
1717

18-
---Get user to pick a window. Selectable windows are all windows in the current
19-
---tabpage that aren't NvimTree.
20-
---@return integer|nil -- If a valid window was picked, return its id. If an
21-
--- invalid window was picked / user canceled, return nil. If there are
22-
--- no selectable windows, return -1.
23-
local function pick_window()
18+
---Get all windows in the current tabpage that aren't NvimTree.
19+
---@return table with valid win_ids
20+
local function selectable_win_ids()
2421
local tabpage = api.nvim_get_current_tabpage()
2522
local win_ids = api.nvim_tabpage_list_wins(tabpage)
2623
local tree_winid = view.get_winnr(tabpage)
2724

28-
local selectable = vim.tbl_filter(function(id)
25+
return vim.tbl_filter(function(id)
2926
local bufid = api.nvim_win_get_buf(id)
3027
for option, v in pairs(M.window_picker.exclude) do
3128
local ok, option_value = pcall(api.nvim_buf_get_option, bufid, option)
@@ -37,6 +34,14 @@ local function pick_window()
3734
local win_config = api.nvim_win_get_config(id)
3835
return id ~= tree_winid and win_config.focusable and not win_config.external
3936
end, win_ids)
37+
end
38+
39+
---Get user to pick a selectable window.
40+
---@return integer|nil -- If a valid window was picked, return its id. If an
41+
--- invalid window was picked / user canceled, return nil. If there are
42+
--- no selectable windows, return -1.
43+
local function pick_window()
44+
local selectable = selectable_win_ids()
4045

4146
-- If there are no selectable windows: return. If there's only 1, return it without picking.
4247
if #selectable == 0 then
@@ -52,6 +57,9 @@ local function pick_window()
5257
local laststatus = vim.o.laststatus
5358
vim.o.laststatus = 2
5459

60+
local tabpage = api.nvim_get_current_tabpage()
61+
local win_ids = api.nvim_tabpage_list_wins(tabpage)
62+
5563
local not_selectable = vim.tbl_filter(function(id)
5664
return not vim.tbl_contains(selectable, id)
5765
end, win_ids)
@@ -147,10 +155,20 @@ local function on_preview(buf_loaded)
147155
view.focus()
148156
end
149157

150-
local function get_target_winid(mode)
158+
local function get_target_winid(mode, win_ids)
151159
local target_winid
152160
if not M.window_picker.enable or mode == "edit_no_picker" then
153161
target_winid = lib.target_winid
162+
163+
-- find the first available window
164+
if not vim.tbl_contains(win_ids, target_winid) then
165+
local selectable = selectable_win_ids()
166+
if #selectable > 0 then
167+
target_winid = selectable[1]
168+
else
169+
return
170+
end
171+
end
154172
else
155173
local pick_window_id = pick_window()
156174
if pick_window_id == nil then
@@ -179,7 +197,7 @@ local function open_in_new_window(filename, mode, win_ids)
179197
mode = ""
180198
end
181199

182-
local target_winid = get_target_winid(mode)
200+
local target_winid = get_target_winid(mode, win_ids)
183201
if not target_winid then
184202
return
185203
end
@@ -195,6 +213,9 @@ local function open_in_new_window(filename, mode, win_ids)
195213

196214
-- No need to split, as we created a new window.
197215
create_new_window = false
216+
if mode:match "split$" then
217+
mode = "edit"
218+
end
198219
elseif not vim.o.hidden then
199220
-- If `hidden` is not enabled, check if buffer in target window is
200221
-- modified, and create new split if it is.

0 commit comments

Comments
 (0)