Skip to content

fix(#1671): split with no window picker will always find an available window #1677

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
merged 1 commit into from
Oct 29, 2022
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
39 changes: 30 additions & 9 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ local function get_user_input_char()
return vim.fn.nr2char(c)
end

---Get user to pick a window. Selectable windows are all windows in the current
---tabpage that aren't 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()
---Get all windows in the current tabpage that aren't NvimTree.
---@return table with valid win_ids
local function selectable_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)

local selectable = vim.tbl_filter(function(id)
return vim.tbl_filter(function(id)
local bufid = api.nvim_win_get_buf(id)
for option, v in pairs(M.window_picker.exclude) do
local ok, option_value = pcall(api.nvim_buf_get_option, bufid, option)
Expand All @@ -37,6 +34,14 @@ local function pick_window()
local win_config = api.nvim_win_get_config(id)
return id ~= tree_winid and win_config.focusable and not win_config.external
end, win_ids)
end

---Get user to pick a selectable window.
---@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()

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

local tabpage = api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage)

local not_selectable = vim.tbl_filter(function(id)
return not vim.tbl_contains(selectable, id)
end, win_ids)
Expand Down Expand Up @@ -147,10 +155,20 @@ local function on_preview(buf_loaded)
view.focus()
end

local function get_target_winid(mode)
local function get_target_winid(mode, win_ids)
local target_winid
if not M.window_picker.enable or mode == "edit_no_picker" then
target_winid = lib.target_winid

-- find the 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
end
else
local pick_window_id = pick_window()
if pick_window_id == nil then
Expand Down Expand Up @@ -179,7 +197,7 @@ local function open_in_new_window(filename, mode, win_ids)
mode = ""
end

local target_winid = get_target_winid(mode)
local target_winid = get_target_winid(mode, win_ids)
if not target_winid then
return
end
Expand All @@ -195,6 +213,9 @@ local function open_in_new_window(filename, mode, win_ids)

-- No need to split, as we created a new window.
create_new_window = false
if mode:match "split$" then
mode = "edit"
end
elseif not vim.o.hidden then
-- If `hidden` is not enabled, check if buffer in target window is
-- modified, and create new split if it is.
Expand Down