Skip to content

Commit 960e219

Browse files
committed
feat: improve multitab support
Previously, toggling ndv would be a noop if it was opened on another tab (it would close that instance with no effect on the current tab). That means you had to toggle twice for it to show up, which is lame. To handle that, we can track the windows instances only for the current tab. There are some downsides, which could be addressed with "proper" multitab support (having ndv actually show up in multiples tabs at once), but tracking state gets cumbersome.
1 parent a18618e commit 960e219

File tree

9 files changed

+61
-9
lines changed

9 files changed

+61
-9
lines changed

docs/src/posts/api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The API offer the same functionality as the [commands](commands).
88
require("dap-view").open()
99
```
1010

11-
Opens both `nvim-dap-view` windows: views + console.
11+
Opens both `nvim-dap-view` windows[^1]: views + console.
1212

1313
---
1414

@@ -54,3 +54,5 @@ require("dap-view").show_view(view)
5454
```
5555

5656
Shows a given view. If the specified view is already the current one, jumps to its window.
57+
58+
[^1]: In the current tab. May close the views window in another tab.

docs/src/posts/commands.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Commands offer the same functionality as the [API](api).
66

77
## `DapViewOpen`
88

9-
Opens both `nvim-dap-view` windows: views + console.
9+
Opens both `nvim-dap-view` windows[^1]: views + console.
1010

1111
## `DapViewClose`
1212

@@ -27,3 +27,5 @@ Shows a given view and jumps to its window. For instance, to jump to the REPL, y
2727
## `DapViewShow [view]`
2828

2929
Shows a given view. If the specified view is already the current one, jumps to its window.
30+
31+
[^1]: In the current tab. May close the views window in another tab.

lua/dap-view.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require("dap-view.highlight")
2+
require("dap-view.autocmds")
23
-- Connect hooks to listen to DAP events
34
require("dap-view.events")
45

lua/dap-view/autocmds.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local state = require("dap-view.state")
2+
local winbar = require("dap-view.options.winbar")
3+
4+
local api = vim.api
5+
6+
api.nvim_create_autocmd("TabEnter", {
7+
callback = function()
8+
local winnr = nil
9+
local term_winnr = nil
10+
11+
for _, win in ipairs(api.nvim_tabpage_list_wins(0)) do
12+
local bufnr = api.nvim_win_get_buf(win)
13+
local ft = vim.bo[bufnr].filetype
14+
15+
if ft == "dap-view" then
16+
if winnr == nil then
17+
winnr = win
18+
end
19+
end
20+
21+
if ft == "dap-view-term" then
22+
if state.current_section == "console" then
23+
if winnr == nil then
24+
winnr = win
25+
end
26+
elseif term_winnr == nil then
27+
term_winnr = win
28+
end
29+
end
30+
31+
if ft == "dap-repl" and state.current_section == "repl" then
32+
if winnr == nil then
33+
winnr = win
34+
end
35+
end
36+
end
37+
38+
state.winnr = winnr
39+
state.term_winnr = term_winnr
40+
41+
if winnr ~= nil then
42+
winbar.show_content(state.current_section)
43+
end
44+
end,
45+
})

lua/dap-view/breakpoints/view.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local api = vim.api
1313
M.show = function()
1414
winbar.update_section("breakpoints")
1515

16-
if state.bufnr then
16+
if state.bufnr and state.winnr then
1717
local breakpoints = vendor.get()
1818

1919
local line = 0

lua/dap-view/exceptions/view.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local api = vim.api
1212
M.show = function()
1313
winbar.update_section("exceptions")
1414

15-
if state.bufnr then
15+
if state.bufnr and state.winnr then
1616
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
1717

1818
if views.cleanup_view(not dap.session(), "No active session") then

lua/dap-view/threads/actions.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ M.jump_or_noop = function()
1212
local line = vim.fn.getline(".")
1313

1414
if string.find(line, "\t") then
15-
util.jump_to_location("^\t(.-)|(%d+)|")
16-
1715
-- Set frame
1816
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
1917
local frame = state.frames_by_line[cursor_line]
2018
if frame then
2119
local session = assert(dap.session(), "has active session")
2220
session:_frame_set(frame)
2321
end
22+
23+
-- Jump after switching the frame, since we need the state.winnr for the switch
24+
-- (which could be missing after a jump)
25+
util.jump_to_location("^\t(.-)|(%d+)|")
2426
else
2527
vim.notify("Can't jump to a thread", log.INFO)
2628
end

lua/dap-view/threads/view.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local api = vim.api
1212
M.show = function()
1313
winbar.update_section("threads")
1414

15-
if state.bufnr then
15+
if state.bufnr and state.winnr then
1616
local session = dap.session()
1717
-- Redundant check to appease the type checker
1818
if views.cleanup_view(session == nil, "No active session") or session == nil then

lua/dap-view/views/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ local api = vim.api
88
---@param cond boolean
99
---@param message string
1010
M.cleanup_view = function(cond, message)
11-
if cond then
11+
if cond and state.winnr then
1212
vim.wo[state.winnr][0].cursorline = false
1313

1414
api.nvim_buf_set_lines(state.bufnr, 0, -1, false, { message })
1515

1616
hl.hl_range("MissingData", { 0, 0 }, { 0, #message })
17-
else
17+
elseif state.winnr then
1818
vim.wo[state.winnr][0].cursorline = true
1919
end
2020

0 commit comments

Comments
 (0)