@@ -15,17 +15,14 @@ local function get_user_input_char()
15
15
return vim .fn .nr2char (c )
16
16
end
17
17
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 ()
24
21
local tabpage = api .nvim_get_current_tabpage ()
25
22
local win_ids = api .nvim_tabpage_list_wins (tabpage )
26
23
local tree_winid = view .get_winnr (tabpage )
27
24
28
- local selectable = vim .tbl_filter (function (id )
25
+ return vim .tbl_filter (function (id )
29
26
local bufid = api .nvim_win_get_buf (id )
30
27
for option , v in pairs (M .window_picker .exclude ) do
31
28
local ok , option_value = pcall (api .nvim_buf_get_option , bufid , option )
@@ -37,6 +34,14 @@ local function pick_window()
37
34
local win_config = api .nvim_win_get_config (id )
38
35
return id ~= tree_winid and win_config .focusable and not win_config .external
39
36
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 ()
40
45
41
46
-- If there are no selectable windows: return. If there's only 1, return it without picking.
42
47
if # selectable == 0 then
@@ -52,6 +57,9 @@ local function pick_window()
52
57
local laststatus = vim .o .laststatus
53
58
vim .o .laststatus = 2
54
59
60
+ local tabpage = api .nvim_get_current_tabpage ()
61
+ local win_ids = api .nvim_tabpage_list_wins (tabpage )
62
+
55
63
local not_selectable = vim .tbl_filter (function (id )
56
64
return not vim .tbl_contains (selectable , id )
57
65
end , win_ids )
@@ -147,10 +155,20 @@ local function on_preview(buf_loaded)
147
155
view .focus ()
148
156
end
149
157
150
- local function get_target_winid (mode )
158
+ local function get_target_winid (mode , win_ids )
151
159
local target_winid
152
160
if not M .window_picker .enable or mode == " edit_no_picker" then
153
161
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
154
172
else
155
173
local pick_window_id = pick_window ()
156
174
if pick_window_id == nil then
@@ -179,7 +197,7 @@ local function open_in_new_window(filename, mode, win_ids)
179
197
mode = " "
180
198
end
181
199
182
- local target_winid = get_target_winid (mode )
200
+ local target_winid = get_target_winid (mode , win_ids )
183
201
if not target_winid then
184
202
return
185
203
end
@@ -195,6 +213,9 @@ local function open_in_new_window(filename, mode, win_ids)
195
213
196
214
-- No need to split, as we created a new window.
197
215
create_new_window = false
216
+ if mode :match " split$" then
217
+ mode = " edit"
218
+ end
198
219
elseif not vim .o .hidden then
199
220
-- If `hidden` is not enabled, check if buffer in target window is
200
221
-- modified, and create new split if it is.
0 commit comments