1+ local dap = require (" dap" )
2+
13local util = require (" dap-view.util" )
4+ local window = require (" dap-view.util.window" )
25local state = require (" dap-view.state" )
36local setup = require (" dap-view.setup" )
47local globals = require (" dap-view.globals" )
@@ -18,7 +21,40 @@ api.nvim_create_autocmd({ "WinClosed", "WinNew" }, {
1821
1922api .nvim_create_autocmd (" TabEnter" , {
2023 callback = function ()
21- if util .is_win_valid (state .winnr ) and setup .config .follow_tab then
24+ local session = dap .session ()
25+ local adapter = session and session .config .type
26+
27+ local follow_tab = setup .config .follow_tab
28+ local follow_tab_ = (type (follow_tab ) == " function" and follow_tab (adapter ))
29+ or (type (follow_tab ) == " boolean" and follow_tab )
30+
31+ local open_winnr = window .fetch_window ()
32+
33+ -- When follow_tab is a function, we have to "restore" what otherwise would be a leftover window
34+ --
35+ -- Consider the following scenario:
36+ -- - Tab 1 is active and has a dap-view window
37+ -- - User switches to tab 2, which is not eligible by `follow_tab`
38+ -- - `state.winnr` is now set to `nil`, but the window still exists on tab 1 (intended)
39+ -- - User switches to tab 3, which is eligible by `follow_tab`
40+ -- - Since there's a dap-view window elsewhere, we have to "reopen" to "follow the tab"
41+ -- (otherwise, visiting a non eligible tab wouldn't make much sense - afterwards, we'd keep the "closed" state)
42+ -- - We can do that just fine, since now we track the correct window with a (window) variable
43+ -- - Buf if the user closes the newly opened dap-view window on tab 3, and switches back to tab 1
44+ -- The original window will still be there! Since we closed, we don't want that!
45+ --
46+ -- This happens because the "close" function does not close all the the valid windows, only the one tracked by
47+ -- `state.winnr`, which is `nil` by then (massive oversight, I know)
48+ --
49+ -- This never came up before the dynamic `follow_tab` because we could always assume `state.winnr` was either
50+ -- from the current tab or the previous tab (which would then be handle by this very own autocmd)
51+ --
52+ -- By assigning `state.winnr` to the open window (which may be anywhere), `open`'s call to `close` will clean it
53+ if open_winnr and state .winnr == nil then
54+ state .winnr = open_winnr
55+ end
56+
57+ if util .is_win_valid (state .winnr ) and follow_tab_ then
2258 require (" dap-view.actions" ).open (state .term_winnr ~= nil )
2359 end
2460
0 commit comments