Skip to content

Commit c86697c

Browse files
authored
feat: DapViewToggle!, DapViewClose!, start_hidden option (#17)
* feat: DapViewHide command * prefer bang instead of new command * surprisingly, it's working now * simplify setup * feat: start_hidden feat: toggle with term * fix: config not being applied * docs: link issue about buftype * cleaner API and docs * refactor: minor cleanup * final cleanup
1 parent be71084 commit c86697c

File tree

10 files changed

+83
-51
lines changed

10 files changed

+83
-51
lines changed

README.md

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

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

101-
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.
101+
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 either completely [hide](#hide-terminal) it (if it's not being used at all) or hide it only during session initialization.
102102

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

@@ -122,8 +122,10 @@ return {
122122
terminal = {
123123
-- 'left'|'right': Terminal position in layout
124124
position = "left",
125-
-- List of adapters for which the terminal should be hidden
125+
-- List of debug adapters for which the terminal should be ALWAYS hidden
126126
hide = {},
127+
-- Hide the terminal when starting a new session
128+
start_hidden = false,
127129
},
128130
},
129131
}
@@ -159,13 +161,18 @@ In total, there are 4 commands:
159161
- `DapViewToggle`
160162
- `DapViewWatch`
161163

164+
Additionally, you can use `DapViewClose!` and `DapViewToggle!` to also hide the
165+
terminal window, if you prefer a tidy view.
166+
162167
If you prefer using lua functions, I got you covered! The following provide the
163168
same functionality as above:
164169

165170
```lua
166171
require("dap-view").open()
167172
require("dap-view").close()
173+
require("dap-view").close(true) -- Same as `DapViewClose!`
168174
require("dap-view").toggle()
175+
require("dap-view").toggle(true) -- Same as `DapViewToggle!`
169176
require("dap-view").add_expr()
170177
```
171178

lua/dap-view.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ M.open = function()
1515
actions.open()
1616
end
1717

18-
M.close = function()
19-
actions.close()
18+
---@param hide_terminal? boolean
19+
M.close = function(hide_terminal)
20+
actions.close(hide_terminal)
2021
end
2122

22-
M.toggle = function()
23-
actions.toggle()
23+
---@param hide_terminal? boolean
24+
M.toggle = function(hide_terminal)
25+
actions.toggle(hide_terminal)
2426
end
2527

2628
M.add_expr = function()

lua/dap-view/actions.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ local api = vim.api
1313

1414
local M = {}
1515

16-
M.toggle = function()
16+
---@param hide_terminal? boolean
17+
M.toggle = function(hide_terminal)
1718
if state.bufnr then
18-
M.close()
19+
M.close(hide_terminal)
1920
else
2021
M.open()
2122
end
2223
end
2324

24-
M.close = function()
25+
---@param hide_terminal? boolean
26+
M.close = function(hide_terminal)
2527
if vim.tbl_contains(setup.config.winbar.sections, "repl") then
2628
dap.repl.close()
2729
end
@@ -33,6 +35,9 @@ M.close = function()
3335
api.nvim_buf_delete(state.bufnr, { force = true })
3436
state.bufnr = nil
3537
end
38+
if hide_terminal then
39+
term.hide_term_buf()
40+
end
3641
end
3742

3843
M.open = function()

lua/dap-view/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local M = {}
99
---@class TerminalConfig
1010
---@field hide string[] Hide the terminal for listed adapters.
1111
---@field position 'right' | 'left'
12+
---@field start_hidden boolean
1213

1314
---@class WindowsConfig
1415
---@field height integer
@@ -30,6 +31,7 @@ M.config = {
3031
terminal = {
3132
position = "left",
3233
hide = {},
34+
start_hidden = false,
3335
},
3436
},
3537
}

lua/dap-view/events.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ local watches = require("dap-view.watches.view")
66
local exceptions = require("dap-view.exceptions.view")
77
local term = require("dap-view.term.init")
88
local eval = require("dap-view.watches.eval")
9+
local setup = require("dap-view.setup")
910

1011
local SUBSCRIPTION_ID = "dap-view"
1112

1213
dap.listeners.before.initialize[SUBSCRIPTION_ID] = function(session, _)
14+
local adapter = session.config.type
1315
-- When initializing a new session, there might a leftover terminal buffer
1416
-- Usually, this wouldn't be a problem, but it can cause inconsistencies when starting a session that
1517
--
@@ -25,12 +27,15 @@ dap.listeners.before.initialize[SUBSCRIPTION_ID] = function(session, _)
2527
--
2628
-- To address that, we only close the terminal if the new session has a different adapter
2729
-- (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+
if state.last_active_adapter ~= adapter then
31+
term.delete_term_buf()
3032
end
31-
state.last_active_adapter = session.config.type
33+
state.last_active_adapter = adapter
3234

33-
term.open_term_buf_win()
35+
term.setup_term()
36+
if not setup.config.windows.terminal.start_hidden then
37+
term.open_term_buf_win()
38+
end
3439
end
3540

3641
dap.listeners.after.setBreakpoints[SUBSCRIPTION_ID] = function()

lua/dap-view/setup/validate/windows.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function M.validate(config)
1212
validate("windows.terminal", {
1313
position = { config.terminal.position, "string" },
1414
hide = { config.terminal.hide, "table" },
15+
start_hidden = { config.terminal.start_hidden, "boolean" },
1516
}, config.terminal)
1617
end
1718

lua/dap-view/term/init.lua

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,71 @@ local term_winnr = nil
1313
---@type integer?
1414
local term_bufnr = nil
1515

16-
M.clear_term_bufnr = function()
16+
---@param callback? fun(): nil
17+
local create_term_buf = function(callback)
18+
if not term_bufnr then
19+
term_bufnr = api.nvim_create_buf(true, false)
20+
21+
assert(term_bufnr ~= 0, "Failed to create nvim-dap-view buffer")
22+
23+
util_buf.quit_buf_autocmd(term_bufnr, M.reset_term_buf)
24+
25+
if callback then
26+
callback()
27+
end
28+
end
29+
end
30+
31+
M.hide_term_buf = function()
32+
if term_winnr and api.nvim_win_is_valid(term_winnr) then
33+
api.nvim_win_hide(term_winnr)
34+
end
35+
end
36+
37+
M.delete_term_buf = function()
1738
if term_bufnr then
1839
api.nvim_buf_delete(term_bufnr, { force = true })
1940
term_bufnr = nil
2041
end
2142
end
2243

23-
M.close_term_buf_win = function()
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
27-
end
28-
-- Only delete the buffer if there's no active session
44+
M.reset_term_buf = function()
45+
-- Only reset the buffer if there's no active session
2946
if term_bufnr and not dap.session() then
30-
api.nvim_buf_delete(term_bufnr, { force = true })
3147
term_bufnr = nil
3248
end
3349
end
3450

3551
---@return integer?
3652
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
40-
41-
if not term_bufnr then
42-
term_bufnr = api.nvim_create_buf(true, false)
43-
44-
assert(term_bufnr ~= 0, "Failed to create nvim-dap-view buffer")
45-
46-
util_buf.quit_buf_autocmd(term_bufnr, M.close_term_buf_win)
47-
end
48-
49-
local config = setup.config
50-
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
53+
create_term_buf()
5754

58-
dap.defaults.fallback.terminal_win_cmd = function()
55+
local is_term_hidden = vim.tbl_contains(setup.config.windows.terminal.hide, state.last_active_adapter)
56+
if dap.session() and term_bufnr and not is_term_hidden then
57+
if term_winnr == nil or term_winnr and not api.nvim_win_is_valid(term_winnr) then
5958
local is_win_valid = state.winnr ~= nil and api.nvim_win_is_valid(state.winnr)
59+
6060
term_winnr = api.nvim_open_win(term_bufnr, false, {
6161
split = is_win_valid and "left" or "below",
6262
win = is_win_valid and state.winnr or -1,
63-
height = config.windows.height,
63+
height = setup.config.windows.height,
6464
})
6565

66-
assert(term_winnr ~= 0, "Failed to create nvim-dap-view terminal window")
67-
6866
require("dap-view.term.options").set_options(term_winnr, term_bufnr)
69-
70-
return term_bufnr, term_winnr
7167
end
7268
end
7369

7470
return term_winnr
7571
end
7672

73+
M.setup_term = function()
74+
create_term_buf(function()
75+
dap.defaults.fallback.terminal_win_cmd = function()
76+
assert(term_bufnr, "Failed to get term bufnr")
77+
78+
return term_bufnr
79+
end
80+
end)
81+
end
82+
7783
return M

lua/dap-view/term/options.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ M.set_options = function(winnr, bufnr)
1515

1616
local buf = vim.bo[bufnr]
1717
buf.filetype = "dap-view-term"
18-
buf.buftype = "nofile"
18+
-- Can't set the buftype here, see https://github.com/neovim/neovim/issues/31457
1919
end
2020

2121
return M

lua/dap-view/util/buffer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local api = vim.api
55
---@param bufnr integer
66
---@param callback fun(): nil
77
M.quit_buf_autocmd = function(bufnr, callback)
8-
api.nvim_create_autocmd({ "BufDelete", "WinClosed" }, {
8+
api.nvim_create_autocmd("BufDelete", {
99
buffer = bufnr,
1010
once = true,
1111
callback = callback,

plugin/dap-view.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
local command = vim.api.nvim_create_user_command
22

33
command("DapViewOpen", require("dap-view").open, {})
4-
command("DapViewClose", require("dap-view").close, {})
5-
command("DapViewToggle", require("dap-view").toggle, {})
4+
command("DapViewClose", function(opts)
5+
require("dap-view").close(opts.bang)
6+
end, { bang = true })
7+
command("DapViewToggle", function(opts)
8+
require("dap-view").toggle(opts.bang)
9+
end, { bang = true })
610
command("DapViewWatch", require("dap-view").add_expr, {})

0 commit comments

Comments
 (0)