@@ -41,10 +41,7 @@ M.close_floating_window = function(source_name)
41
41
local valid_window_was_closed = false
42
42
for _ , win in ipairs (found_windows ) do
43
43
if not valid_window_was_closed then
44
- local winid = win .winid
45
- if type (winid ) == " number" and winid > 0 then
46
- valid_window_was_closed = vim .api .nvim_win_is_valid (winid )
47
- end
44
+ valid_window_was_closed = M .is_window_valid (win .winid )
48
45
end
49
46
-- regardless of whether the window is valid or not, nui will cleanup
50
47
win :unmount ()
@@ -413,6 +410,20 @@ local create_window = function(state)
413
410
return win
414
411
end
415
412
413
+ --- Determines is the givin winid is valid and the window still exists.
414
+ --- @param winid any
415
+ --- @return boolean
416
+ M .is_window_valid = function (winid )
417
+ if winid == nil then
418
+ return false
419
+ end
420
+ if type (winid ) == " number" and winid > 0 then
421
+ return vim .api .nvim_win_is_valid (winid )
422
+ else
423
+ return false
424
+ end
425
+ end
426
+
416
427
--- Determines if the window exists and is valid.
417
428
--- @param state table The current state of the plugin.
418
429
--- @return boolean True if the window exists and is valid , false otherwise.
@@ -421,9 +432,8 @@ M.window_exists = function(state)
421
432
if state .winid == nil then
422
433
window_exists = false
423
434
else
424
- local winid = utils .get_value (state , " winid" , 0 , true )
425
- local isvalid = winid > 0 and vim .api .nvim_win_is_valid (winid )
426
- window_exists = isvalid and (vim .api .nvim_win_get_number (winid ) > 0 )
435
+ local isvalid = M .is_window_valid (state .winid )
436
+ window_exists = isvalid and (vim .api .nvim_win_get_number (state .winid ) > 0 )
427
437
if not window_exists then
428
438
local bufnr = utils .get_value (state , " bufnr" , 0 , true )
429
439
if bufnr > 0 and vim .api .nvim_buf_is_valid (bufnr ) then
0 commit comments