Skip to content

fix(#1711): open in a new window when no window picker and no available window #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end

---Get all windows in the current tabpage that aren't NvimTree.
---@return table with valid win_ids
local function selectable_win_ids()
local function usable_win_ids()
local tabpage = api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage)
local tree_winid = view.get_winnr(tabpage)
Expand All @@ -36,12 +36,23 @@ local function selectable_win_ids()
end, win_ids)
end

---Get user to pick a selectable window.
---Find the first window in the tab that is not NvimTree.
---@return integer -1 if none available
local function first_win_id()
local selectable = usable_win_ids()
if #selectable > 0 then
return selectable[1]
else
return -1
end
end

---Get user to pick a window in the tab that is not NvimTree.
---@return integer|nil -- If a valid window was picked, return its id. If an
--- invalid window was picked / user canceled, return nil. If there are
--- no selectable windows, return -1.
local function pick_window()
local selectable = selectable_win_ids()
local function pick_win_id()
local selectable = usable_win_ids()

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

-- find the first available window
-- first available window
if not vim.tbl_contains(win_ids, target_winid) then
local selectable = selectable_win_ids()
if #selectable > 0 then
target_winid = selectable[1]
else
return
end
target_winid = first_win_id()
end
else
local pick_window_id = pick_window()
if pick_window_id == nil then
-- pick a window
target_winid = pick_win_id()
if target_winid == nil then
-- pick failed/cancelled
return
end
target_winid = pick_window_id
end

if target_winid == -1 then
Expand Down