Skip to content

Commit 33ce8e3

Browse files
authored
fix(#1711): open in a new window when no window picker and no available window (#1715)
1 parent 6ca6f99 commit 33ce8e3

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ end
1717

1818
---Get all windows in the current tabpage that aren't NvimTree.
1919
---@return table with valid win_ids
20-
local function selectable_win_ids()
20+
local function usable_win_ids()
2121
local tabpage = api.nvim_get_current_tabpage()
2222
local win_ids = api.nvim_tabpage_list_wins(tabpage)
2323
local tree_winid = view.get_winnr(tabpage)
@@ -36,12 +36,23 @@ local function selectable_win_ids()
3636
end, win_ids)
3737
end
3838

39-
---Get user to pick a selectable window.
39+
---Find the first window in the tab that is not NvimTree.
40+
---@return integer -1 if none available
41+
local function first_win_id()
42+
local selectable = usable_win_ids()
43+
if #selectable > 0 then
44+
return selectable[1]
45+
else
46+
return -1
47+
end
48+
end
49+
50+
---Get user to pick a window in the tab that is not NvimTree.
4051
---@return integer|nil -- If a valid window was picked, return its id. If an
4152
--- invalid window was picked / user canceled, return nil. If there are
4253
--- no selectable windows, return -1.
43-
local function pick_window()
44-
local selectable = selectable_win_ids()
54+
local function pick_win_id()
55+
local selectable = usable_win_ids()
4556

4657
-- If there are no selectable windows: return. If there's only 1, return it without picking.
4758
if #selectable == 0 then
@@ -160,21 +171,17 @@ local function get_target_winid(mode, win_ids)
160171
if not M.window_picker.enable or mode == "edit_no_picker" then
161172
target_winid = lib.target_winid
162173

163-
-- find the first available window
174+
-- first available window
164175
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
176+
target_winid = first_win_id()
171177
end
172178
else
173-
local pick_window_id = pick_window()
174-
if pick_window_id == nil then
179+
-- pick a window
180+
target_winid = pick_win_id()
181+
if target_winid == nil then
182+
-- pick failed/cancelled
175183
return
176184
end
177-
target_winid = pick_window_id
178185
end
179186

180187
if target_winid == -1 then

0 commit comments

Comments
 (0)