Skip to content

Commit 80381ef

Browse files
igorlfstljgithub-actions[bot]catgoose
authored
feat!: specify adapters for which the terminal should be hidden (#10)
* feat!: disable terminal for selected adapters * feat: validate user config for hidden adapters * docs: how to disable the terminal --------- Co-authored-by: tlj <tlj@tlj.no> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Joshua Tye <21010072+catgoose@users.noreply.github.com>
1 parent 7d75930 commit 80381ef

File tree

8 files changed

+125
-54
lines changed

8 files changed

+125
-54
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ The plugin provides 4 "views" that share the same window (so there's clutter)
9595

9696
![repl view](https://github.com/user-attachments/assets/43caeb02-ff9e-47ea-a4c1-ab5dd30d8a3c)
9797

98-
You can also interact with the console provided by `nvim-dap` (though, arguably, that's not a feature from `nvim-dap-view`). The console has its own window. However, its default size (height) is resized to match you `nvim-dap-view` configuration.
98+
You can also interact with the console provided by `nvim-dap` (though, arguably, that's not a feature from `nvim-dap-view`). The console has its own window. However, its default size (height) is resized to match your `nvim-dap-view` configuration. You can also completely [hide](#hide-terminal) it, if it's not being used.
9999

100100
![console](https://github.com/user-attachments/assets/0980962c-e3da-4f16-af4c-786ef7fa4b18)
101101

@@ -116,6 +116,10 @@ return {
116116
},
117117
windows = {
118118
height = 12,
119+
terminal = {
120+
-- List of adapters for which the terminal should be hidden
121+
hide = {},
122+
},
119123
},
120124
}
121125
```
@@ -169,11 +173,32 @@ vim.keymap.set("n", "<leader>v", function()
169173
end, { desc = "Toggle nvim-dap-view" })
170174
```
171175

176+
### Recommended Setup
177+
178+
#### Hide Terminal
179+
180+
Some debug adapters don't use the integrated terminal (console). To avoid having a useless window lying around, you can completely hide the terminal for them. To achieve that, add the following snippet to your `nvim-dap-view` setup:
181+
182+
```lua
183+
-- Goes into your opts table (if using lazy.nvim), otherwise goes into the setup function
184+
-- No need to include the "return" statement (or the outer curly braces)
185+
return {
186+
windows = {
187+
terminal = {
188+
-- NOTE Don't copy paste this snippet
189+
-- Use the actual names for the adapters you want to hide
190+
-- `delve` is known to not use the terminal
191+
hide = { "delve", "some-other-adapter" },
192+
},
193+
},
194+
}
195+
```
196+
172197
### Highlight Groups
173198

174199
`nvim-dap-view` defines 8 highlight groups:
175200

176-
```lua
201+
```
177202
NvimDapViewMissingData
178203
NvimDapViewWatchText
179204
NvimDapViewWatchTextChanged

lua/dap-view/actions.lua

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ M.toggle = function()
2222
end
2323

2424
M.close = function()
25-
if not dap.session() then
26-
term.close_term_buf_win()
27-
if vim.tbl_contains(setup.config.winbar.sections, "repl") then
28-
dap.repl.close()
29-
end
25+
if vim.tbl_contains(setup.config.winbar.sections, "repl") then
26+
dap.repl.close()
3027
end
3128
if state.winnr and api.nvim_win_is_valid(state.winnr) then
3229
api.nvim_win_close(state.winnr, true)
@@ -43,7 +40,7 @@ M.open = function()
4340

4441
local bufnr = api.nvim_create_buf(false, false)
4542

46-
assert(bufnr ~= 0, "Failed to create dap-view buffer")
43+
assert(bufnr ~= 0, "Failed to create nvim-dap-view buffer")
4744

4845
state.bufnr = bufnr
4946

@@ -54,17 +51,19 @@ M.open = function()
5451

5552
api.nvim_buf_set_name(bufnr, globals.MAIN_BUF_NAME)
5653

57-
local term_winnr = term.term_buf_win_init()
54+
local term_winnr = term.open_term_buf_win()
5855

5956
local config = setup.config
6057

58+
local is_term_win_valid = term_winnr ~= nil and api.nvim_win_is_valid(term_winnr)
59+
6160
local winnr = api.nvim_open_win(bufnr, false, {
62-
split = "right",
63-
win = term_winnr,
61+
split = is_term_win_valid and "right" or "below",
62+
win = is_term_win_valid and term_winnr or -1,
6463
height = config.windows.height,
6564
})
6665

67-
assert(winnr ~= 0, "Failed to create dap-view window")
66+
assert(winnr ~= 0, "Failed to create nvim-dap-view window")
6867

6968
state.winnr = winnr
7069

lua/dap-view/config.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ local M = {}
66
---@field default_section SectionType
77
---@field show boolean
88

9+
---@class TerminalConfig
10+
---@field hide string[] Hide the terminal for listed adapters.
11+
912
---@class WindowsConfig
1013
---@field height integer
14+
---@field terminal TerminalConfig
1115

1216
---@alias SectionType '"breakpoints"' | '"exceptions"' | '"watches"' | '"repl"'
1317

@@ -22,6 +26,9 @@ M.config = {
2226
},
2327
windows = {
2428
height = 12,
29+
terminal = {
30+
hide = {},
31+
},
2532
},
2633
}
2734

lua/dap-view/events.lua

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,28 @@ local eval = require("dap-view.watches.eval")
99

1010
local SUBSCRIPTION_ID = "dap-view"
1111

12-
dap.listeners.before.initialize[SUBSCRIPTION_ID] = function()
13-
term.term_buf_win_init()
12+
dap.listeners.before.initialize[SUBSCRIPTION_ID] = function(session, _)
13+
-- When initializing a new session, there might a leftover terminal buffer
14+
-- Usually, this wouldn't be a problem, but it can cause inconsistencies when starting a session that
15+
--
16+
-- (A) Doesn't use the terminal, after a session that does
17+
-- The problem here is that the terminal could be used if it was left open from the earlier session
18+
--
19+
-- (B) Uses the terminal, after a session that doesn't
20+
-- The terminal wouldn't show up, since it's hidden
21+
--
22+
-- To handle these scenarios, we have to close the terminal buffer
23+
-- However, if we always close the terminal, dap-view will be shifted very quickly (if open),
24+
-- causing a flickering effect.
25+
--
26+
-- To address that, we only close the terminal if the new session has a different adapter
27+
-- (which should cover most scenarios where the flickering would occur)
28+
if state.last_active_adapter ~= session.config.type then
29+
term.clear_term_bufnr()
30+
end
31+
state.last_active_adapter = session.config.type
32+
33+
term.open_term_buf_win()
1434
end
1535

1636
dap.listeners.after.setBreakpoints[SUBSCRIPTION_ID] = function()
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
local M = {}
22

3+
local validate = require("dap-view.setup.validate.util").validate
4+
35
---@param config WindowsConfig
46
function M.validate(config)
5-
require("dap-view.setup.validate.util").validate("windows", {
7+
validate("windows", {
68
height = { config.height, "number" },
9+
terminal = { config.terminal, "table" },
710
}, config)
11+
12+
validate("windows.terminal", {
13+
hide = { config.terminal.hide, "table" },
14+
}, config.terminal)
815
end
916

1017
return M

lua/dap-view/state.lua

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@
55
---@class State
66
---@field bufnr? integer
77
---@field winnr? integer
8-
---@field term_bufnr? integer
9-
---@field term_winnr? integer
8+
---@field last_active_adapter? string
109
---@field current_section? SectionType
1110
---@field exceptions_options? ExceptionsOption[]
1211
---@field watched_expressions string[]
1312
---@field expression_results string[]
1413
---@field updated_evaluations boolean[]
1514
local M = {
16-
bufnr = nil,
17-
winnr = nil,
18-
term_bufnr = nil,
19-
term_winnr = nil,
20-
current_section = nil,
21-
exceptions_options = nil,
2215
watched_expressions = {},
2316
expression_results = {},
2417
updated_evaluations = {},

lua/dap-view/term/init.lua

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,70 @@ local api = vim.api
88

99
local M = {}
1010

11+
---@type integer?
12+
local term_winnr = nil
13+
---@type integer?
14+
local term_bufnr = nil
15+
16+
M.clear_term_bufnr = function()
17+
if term_bufnr then
18+
api.nvim_buf_delete(term_bufnr, { force = true })
19+
term_bufnr = nil
20+
end
21+
end
22+
1123
M.close_term_buf_win = function()
12-
if state.term_winnr then
13-
api.nvim_win_close(state.term_winnr, true)
14-
state.term_winnr = nil
24+
if term_winnr and api.nvim_win_is_valid(term_winnr) then
25+
api.nvim_win_close(term_winnr, true)
26+
term_winnr = nil
1527
end
16-
if state.term_bufnr then
17-
api.nvim_buf_delete(state.term_bufnr, { force = true })
18-
state.term_bufnr = nil
28+
-- Only delete the buffer if there's no active session
29+
if term_bufnr and not dap.session() then
30+
api.nvim_buf_delete(term_bufnr, { force = true })
31+
term_bufnr = nil
1932
end
2033
end
2134

22-
M.term_buf_win_init = function()
23-
-- Should NOT close the term buffer, since it contains the data from the session
35+
---@return integer?
36+
M.open_term_buf_win = function()
37+
-- When (re)opening the terminal we should NOT close it,
38+
-- since it's the default standard output for most adapters
39+
-- Therefore, closing it could delete useful information from the last session
2440

25-
if not state.term_bufnr then
26-
local term_bufnr = api.nvim_create_buf(true, false)
41+
if not term_bufnr then
42+
term_bufnr = api.nvim_create_buf(true, false)
2743

28-
assert(term_bufnr ~= 0, "Failed to create dap-view buffer")
44+
assert(term_bufnr ~= 0, "Failed to create nvim-dap-view buffer")
2945

30-
state.term_bufnr = term_bufnr
46+
util_buf.quit_buf_autocmd(term_bufnr, M.close_term_buf_win)
3147
end
3248

33-
if not state.term_winnr then
34-
local config = setup.config
35-
local term_winnr = api.nvim_open_win(state.term_bufnr, false, {
36-
split = "below",
37-
win = -1,
38-
height = config.windows.height,
39-
})
49+
local config = setup.config
4050

41-
assert(term_winnr ~= 0, "Failed to create dap-view terminal window")
51+
if not term_winnr then
52+
for _, adapter in ipairs(config.windows.terminal.hide) do
53+
dap.defaults[adapter].terminal_win_cmd = function()
54+
return term_bufnr
55+
end
56+
end
4257

43-
state.term_winnr = term_winnr
58+
dap.defaults.fallback.terminal_win_cmd = function()
59+
local is_win_valid = state.winnr ~= nil and api.nvim_win_is_valid(state.winnr)
60+
term_winnr = api.nvim_open_win(term_bufnr, false, {
61+
split = is_win_valid and "left" or "below",
62+
win = is_win_valid and state.winnr or -1,
63+
height = config.windows.height,
64+
})
4465

45-
require("dap-view.term.options").set_options()
66+
assert(term_winnr ~= 0, "Failed to create nvim-dap-view terminal window")
4667

47-
util_buf.quit_buf_autocmd(state.term_bufnr, M.close_term_buf_win)
68+
require("dap-view.term.options").set_options(term_winnr, term_bufnr)
4869

49-
dap.defaults.fallback.terminal_win_cmd = function()
50-
return state.term_bufnr, state.term_winnr
70+
return term_bufnr, term_winnr
5171
end
5272
end
5373

54-
return state.term_winnr
74+
return term_winnr
5575
end
5676

5777
return M

lua/dap-view/term/options.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local M = {}
22

3-
local state = require("dap-view.state")
4-
5-
M.set_options = function()
6-
local win = vim.wo[state.term_winnr][0]
3+
---@param winnr integer
4+
---@param bufnr integer
5+
M.set_options = function(winnr, bufnr)
6+
local win = vim.wo[winnr][0]
77
win.scrolloff = 0
88
win.wrap = false
99
win.number = false
@@ -13,7 +13,7 @@ M.set_options = function()
1313
win.foldcolumn = "0"
1414
win.winfixbuf = true
1515

16-
local buf = vim.bo[state.term_bufnr]
16+
local buf = vim.bo[bufnr]
1717
buf.filetype = "dap-view-term"
1818
buf.buftype = "nofile"
1919
end

0 commit comments

Comments
 (0)