Skip to content

Commit a0c5f9f

Browse files
authored
feat: expose terminal bufnr/winnr in state module (#19)
* feat: creates windows.terminal.ignore_session configuration option allows for creating terminal window before dap session has started * fix: correctly position terminal if ignoring dap session * Update README.md Co-authored-by: Igor Lacerda <igorlfs@ufmg.br> ref: rename config.windows.terminal.ignore_session to ...bootstrap chore: adds comment about checking state.last_active_adapter for nil * ref: move terminal creation logic concerning terminal edge cases to state module ref: removes bootstrap configuration option * chore: removes unused import and converts function to local * ref: simplify logic for opening terminal window
1 parent dd02cfc commit a0c5f9f

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ return {
199199
terminal = {
200200
-- NOTE Don't copy paste this snippet
201201
-- Use the actual names for the adapters you want to hide
202-
-- `delve` is known to not use the terminal
203-
hide = { "delve", "some-other-adapter" },
202+
-- `go` is known to not use the terminal.
203+
hide = { "go", "some-other-adapter" },
204204
},
205205
},
206206
}
@@ -217,9 +217,10 @@ require("dap").defaults.fallback.switchbuf = "useopen"
217217
```
218218

219219
If you are using an adapter that does not natively support the `nvim-dap` integrated
220-
terminal, you can get the `winnr` and `bufnr` of the `nvim-dap-view` terminal via
221-
`dap-view.state` and use `vim.fn.jobstart` to start your debugger in the `nvim-dap-view`
222-
terminal! An example can be found [here](https://github.com/catgoose/nvim/blob/a783e0fe931a5e417c4e3cc7e964793d895862e6/lua/config/dap/go.lua)
220+
terminal, but you want to use the `nvim-dap-view` terminal anyway, you can get
221+
the `winnr` and `bufnr` of the `nvim-dap-view` terminal via `dap-view.state` and
222+
use `vim.fn.jobstart` to start your debug adapter in the `nvim-dap-view` terminal!
223+
An example can be found [here](https://github.com/catgoose/nvim/blob/ffd88fd66ade9cad0da934e10308dbbfc76b9540/lua/config/dap/go.lua#L19-L48)
223224

224225
### Highlight Groups
225226

lua/dap-view/events.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ dap.listeners.before.initialize[SUBSCRIPTION_ID] = function(session, _)
2727
--
2828
-- To address that, we only close the terminal if the new session has a different adapter
2929
-- (which should cover most scenarios where the flickering would occur)
30-
if state.last_active_adapter ~= adapter then
30+
--
31+
-- Since there has not been a `last_active_adapter` yet, we don't need to
32+
-- call `delete_term_buf`, which conflicts with bootstrapping the terminal
33+
-- window. See: https://github.com/igorlfs/nvim-dap-view/issues/18
34+
if state.last_active_adapter and state.last_active_adapter ~= adapter then
3135
term.delete_term_buf()
3236
end
3337
state.last_active_adapter = adapter

lua/dap-view/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
---@class State
66
---@field bufnr? integer
77
---@field winnr? integer
8+
---@field term_bufnr? integer
9+
---@field term_winnr? integer
810
---@field last_active_adapter? string
911
---@field current_section? SectionType
1012
---@field exceptions_options? ExceptionsOption[]

lua/dap-view/term/init.lua

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
local dap = require("dap")
22

33
local state = require("dap-view.state")
4-
local setup = require("dap-view.setup")
54
local util_buf = require("dap-view.util.buffer")
5+
local setup = require("dap-view.setup")
66

77
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-
1611
---@param callback? fun(): nil
1712
local create_term_buf = function(callback)
18-
if not term_bufnr then
19-
term_bufnr = api.nvim_create_buf(true, false)
13+
if not state.term_bufnr then
14+
state.term_bufnr = api.nvim_create_buf(true, false)
2015

21-
assert(term_bufnr ~= 0, "Failed to create nvim-dap-view buffer")
16+
assert(state.term_bufnr ~= 0, "Failed to create nvim-dap-view buffer")
2217

23-
util_buf.quit_buf_autocmd(term_bufnr, M.reset_term_buf)
18+
util_buf.quit_buf_autocmd(state.term_bufnr, M.reset_term_buf)
2419

2520
if callback then
2621
callback()
@@ -29,22 +24,22 @@ local create_term_buf = function(callback)
2924
end
3025

3126
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)
27+
if state.term_winnr and api.nvim_win_is_valid(state.term_winnr) then
28+
api.nvim_win_hide(state.term_winnr)
3429
end
3530
end
3631

3732
M.delete_term_buf = function()
38-
if term_bufnr then
39-
api.nvim_buf_delete(term_bufnr, { force = true })
40-
term_bufnr = nil
33+
if state.term_bufnr then
34+
api.nvim_buf_delete(state.term_bufnr, { force = true })
35+
state.term_bufnr = nil
4136
end
4237
end
4338

4439
M.reset_term_buf = function()
4540
-- Only reset the buffer if there's no active session
46-
if term_bufnr and not dap.session() then
47-
term_bufnr = nil
41+
if state.term_bufnr and not dap.session() then
42+
state.term_bufnr = nil
4843
end
4944
end
5045

@@ -53,30 +48,30 @@ M.open_term_buf_win = function()
5348
create_term_buf()
5449

5550
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
51+
if dap.session() and state.term_bufnr and not is_term_hidden then
52+
if state.term_winnr == nil or state.term_winnr and not api.nvim_win_is_valid(state.term_winnr) then
5853
local is_win_valid = state.winnr ~= nil and api.nvim_win_is_valid(state.winnr)
5954
local term_position = setup.config.windows.terminal.position
6055

61-
term_winnr = api.nvim_open_win(term_bufnr, false, {
56+
state.term_winnr = api.nvim_open_win(state.term_bufnr, false, {
6257
split = is_win_valid and term_position or "below",
6358
win = is_win_valid and state.winnr or -1,
6459
height = setup.config.windows.height,
6560
})
6661

67-
require("dap-view.term.options").set_options(term_winnr, term_bufnr)
62+
require("dap-view.term.options").set_options(state.term_winnr, state.term_bufnr)
6863
end
6964
end
7065

71-
return term_winnr
66+
return state.term_winnr
7267
end
7368

7469
M.setup_term = function()
7570
create_term_buf(function()
7671
dap.defaults.fallback.terminal_win_cmd = function()
77-
assert(term_bufnr, "Failed to get term bufnr")
72+
assert(state.term_bufnr, "Failed to get term bufnr")
7873

79-
return term_bufnr
74+
return state.term_bufnr
8075
end
8176
end)
8277
end

0 commit comments

Comments
 (0)