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

This commit is contained in:
Alexander Courtis 2022-10-29 13:42:56 +11:00 committed by GitHub
parent cb98892dea
commit dd90bfa155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,17 +15,14 @@ local function get_user_input_char()
return vim.fn.nr2char(c) return vim.fn.nr2char(c)
end end
---Get user to pick a window. Selectable windows are all windows in the current ---Get all windows in the current tabpage that aren't NvimTree.
---tabpage that aren't NvimTree. ---@return table with valid win_ids
---@return integer|nil -- If a valid window was picked, return its id. If an local function selectable_win_ids()
--- invalid window was picked / user canceled, return nil. If there are
--- no selectable windows, return -1.
local function pick_window()
local tabpage = api.nvim_get_current_tabpage() local tabpage = api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage) local win_ids = api.nvim_tabpage_list_wins(tabpage)
local tree_winid = view.get_winnr(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) local bufid = api.nvim_win_get_buf(id)
for option, v in pairs(M.window_picker.exclude) do for option, v in pairs(M.window_picker.exclude) do
local ok, option_value = pcall(api.nvim_buf_get_option, bufid, option) local ok, option_value = pcall(api.nvim_buf_get_option, bufid, option)
@ -37,6 +34,14 @@ local function pick_window()
local win_config = api.nvim_win_get_config(id) local win_config = api.nvim_win_get_config(id)
return id ~= tree_winid and win_config.focusable and not win_config.external return id ~= tree_winid and win_config.focusable and not win_config.external
end, win_ids) 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 there are no selectable windows: return. If there's only 1, return it without picking.
if #selectable == 0 then if #selectable == 0 then
@ -52,6 +57,9 @@ local function pick_window()
local laststatus = vim.o.laststatus local laststatus = vim.o.laststatus
vim.o.laststatus = 2 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) local not_selectable = vim.tbl_filter(function(id)
return not vim.tbl_contains(selectable, id) return not vim.tbl_contains(selectable, id)
end, win_ids) end, win_ids)
@ -147,10 +155,20 @@ local function on_preview(buf_loaded)
view.focus() view.focus()
end end
local function get_target_winid(mode) local function get_target_winid(mode, win_ids)
local target_winid local target_winid
if not M.window_picker.enable or mode == "edit_no_picker" then if not M.window_picker.enable or mode == "edit_no_picker" then
target_winid = lib.target_winid 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 else
local pick_window_id = pick_window() local pick_window_id = pick_window()
if pick_window_id == nil then if pick_window_id == nil then
@ -179,7 +197,7 @@ local function open_in_new_window(filename, mode, win_ids)
mode = "" mode = ""
end end
local target_winid = get_target_winid(mode) local target_winid = get_target_winid(mode, win_ids)
if not target_winid then if not target_winid then
return return
end end
@ -195,6 +213,9 @@ local function open_in_new_window(filename, mode, win_ids)
-- No need to split, as we created a new window. -- No need to split, as we created a new window.
create_new_window = false create_new_window = false
if mode:match "split$" then
mode = "edit"
end
elseif not vim.o.hidden then elseif not vim.o.hidden then
-- If `hidden` is not enabled, check if buffer in target window is -- If `hidden` is not enabled, check if buffer in target window is
-- modified, and create new split if it is. -- modified, and create new split if it is.