Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ The plugin provides 4 "views" that share the same window (so there's clutter)

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

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.
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.

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

Expand All @@ -122,8 +122,10 @@ return {
terminal = {
-- 'left'|'right': Terminal position in layout
position = "left",
-- List of adapters for which the terminal should be hidden
-- List of debug adapters for which the terminal should be ALWAYS hidden
hide = {},
-- Hide the terminal when starting a new session
start_hidden = false,
},
},
}
Expand Down Expand Up @@ -159,13 +161,18 @@ In total, there are 4 commands:
- `DapViewToggle`
- `DapViewWatch`

Additionally, you can use `DapViewClose!` and `DapViewToggle!` to also hide the
terminal window, if you prefer a tidy view.

If you prefer using lua functions, I got you covered! The following provide the
same functionality as above:

```lua
require("dap-view").open()
require("dap-view").close()
require("dap-view").close(true) -- Same as `DapViewClose!`
require("dap-view").toggle()
require("dap-view").toggle(true) -- Same as `DapViewToggle!`
require("dap-view").add_expr()
```

Expand Down
10 changes: 6 additions & 4 deletions lua/dap-view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ M.open = function()
actions.open()
end

M.close = function()
actions.close()
---@param hide_terminal? boolean
M.close = function(hide_terminal)
actions.close(hide_terminal)
end

M.toggle = function()
actions.toggle()
---@param hide_terminal? boolean
M.toggle = function(hide_terminal)
actions.toggle(hide_terminal)
end

M.add_expr = function()
Expand Down
11 changes: 8 additions & 3 deletions lua/dap-view/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ local api = vim.api

local M = {}

M.toggle = function()
---@param hide_terminal? boolean
M.toggle = function(hide_terminal)
if state.bufnr then
M.close()
M.close(hide_terminal)
else
M.open()
end
end

M.close = function()
---@param hide_terminal? boolean
M.close = function(hide_terminal)
if vim.tbl_contains(setup.config.winbar.sections, "repl") then
dap.repl.close()
end
Expand All @@ -33,6 +35,9 @@ M.close = function()
api.nvim_buf_delete(state.bufnr, { force = true })
state.bufnr = nil
end
if hide_terminal then
term.hide_term_buf()
end
end

M.open = function()
Expand Down
2 changes: 2 additions & 0 deletions lua/dap-view/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local M = {}
---@class TerminalConfig
---@field hide string[] Hide the terminal for listed adapters.
---@field position 'right' | 'left'
---@field start_hidden boolean

---@class WindowsConfig
---@field height integer
Expand All @@ -30,6 +31,7 @@ M.config = {
terminal = {
position = "left",
hide = {},
start_hidden = false,
},
},
}
Expand Down
13 changes: 9 additions & 4 deletions lua/dap-view/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ local watches = require("dap-view.watches.view")
local exceptions = require("dap-view.exceptions.view")
local term = require("dap-view.term.init")
local eval = require("dap-view.watches.eval")
local setup = require("dap-view.setup")

local SUBSCRIPTION_ID = "dap-view"

dap.listeners.before.initialize[SUBSCRIPTION_ID] = function(session, _)
local adapter = session.config.type
-- When initializing a new session, there might a leftover terminal buffer
-- Usually, this wouldn't be a problem, but it can cause inconsistencies when starting a session that
--
Expand All @@ -25,12 +27,15 @@ dap.listeners.before.initialize[SUBSCRIPTION_ID] = function(session, _)
--
-- To address that, we only close the terminal if the new session has a different adapter
-- (which should cover most scenarios where the flickering would occur)
if state.last_active_adapter ~= session.config.type then
term.clear_term_bufnr()
if state.last_active_adapter ~= adapter then
term.delete_term_buf()
end
state.last_active_adapter = session.config.type
state.last_active_adapter = adapter

term.open_term_buf_win()
term.setup_term()
if not setup.config.windows.terminal.start_hidden then
term.open_term_buf_win()
end
end

dap.listeners.after.setBreakpoints[SUBSCRIPTION_ID] = function()
Expand Down
1 change: 1 addition & 0 deletions lua/dap-view/setup/validate/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function M.validate(config)
validate("windows.terminal", {
position = { config.terminal.position, "string" },
hide = { config.terminal.hide, "table" },
start_hidden = { config.terminal.start_hidden, "boolean" },
}, config.terminal)
end

Expand Down
74 changes: 40 additions & 34 deletions lua/dap-view/term/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,71 @@ local term_winnr = nil
---@type integer?
local term_bufnr = nil

M.clear_term_bufnr = function()
---@param callback? fun(): nil
local create_term_buf = function(callback)
if not term_bufnr then
term_bufnr = api.nvim_create_buf(true, false)

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

util_buf.quit_buf_autocmd(term_bufnr, M.reset_term_buf)

if callback then
callback()
end
end
end

M.hide_term_buf = function()
if term_winnr and api.nvim_win_is_valid(term_winnr) then
api.nvim_win_hide(term_winnr)
end
end

M.delete_term_buf = function()
if term_bufnr then
api.nvim_buf_delete(term_bufnr, { force = true })
term_bufnr = nil
end
end

M.close_term_buf_win = function()
if term_winnr and api.nvim_win_is_valid(term_winnr) then
api.nvim_win_close(term_winnr, true)
term_winnr = nil
end
-- Only delete the buffer if there's no active session
M.reset_term_buf = function()
-- Only reset the buffer if there's no active session
if term_bufnr and not dap.session() then
api.nvim_buf_delete(term_bufnr, { force = true })
term_bufnr = nil
end
end

---@return integer?
M.open_term_buf_win = function()
-- When (re)opening the terminal we should NOT close it,
-- since it's the default standard output for most adapters
-- Therefore, closing it could delete useful information from the last session

if not term_bufnr then
term_bufnr = api.nvim_create_buf(true, false)

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

util_buf.quit_buf_autocmd(term_bufnr, M.close_term_buf_win)
end

local config = setup.config

if not term_winnr then
for _, adapter in ipairs(config.windows.terminal.hide) do
dap.defaults[adapter].terminal_win_cmd = function()
return term_bufnr
end
end
create_term_buf()

dap.defaults.fallback.terminal_win_cmd = function()
local is_term_hidden = vim.tbl_contains(setup.config.windows.terminal.hide, state.last_active_adapter)
if dap.session() and term_bufnr and not is_term_hidden then
if term_winnr == nil or term_winnr and not api.nvim_win_is_valid(term_winnr) then
local is_win_valid = state.winnr ~= nil and api.nvim_win_is_valid(state.winnr)

term_winnr = api.nvim_open_win(term_bufnr, false, {
split = is_win_valid and "left" or "below",
win = is_win_valid and state.winnr or -1,
height = config.windows.height,
height = setup.config.windows.height,
})

assert(term_winnr ~= 0, "Failed to create nvim-dap-view terminal window")

require("dap-view.term.options").set_options(term_winnr, term_bufnr)

return term_bufnr, term_winnr
end
end

return term_winnr
end

M.setup_term = function()
create_term_buf(function()
dap.defaults.fallback.terminal_win_cmd = function()
assert(term_bufnr, "Failed to get term bufnr")

return term_bufnr
end
end)
end

return M
2 changes: 1 addition & 1 deletion lua/dap-view/term/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ M.set_options = function(winnr, bufnr)

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

return M
2 changes: 1 addition & 1 deletion lua/dap-view/util/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local api = vim.api
---@param bufnr integer
---@param callback fun(): nil
M.quit_buf_autocmd = function(bufnr, callback)
api.nvim_create_autocmd({ "BufDelete", "WinClosed" }, {
api.nvim_create_autocmd("BufDelete", {
buffer = bufnr,
once = true,
callback = callback,
Expand Down
8 changes: 6 additions & 2 deletions plugin/dap-view.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local command = vim.api.nvim_create_user_command

command("DapViewOpen", require("dap-view").open, {})
command("DapViewClose", require("dap-view").close, {})
command("DapViewToggle", require("dap-view").toggle, {})
command("DapViewClose", function(opts)
require("dap-view").close(opts.bang)
end, { bang = true })
command("DapViewToggle", function(opts)
require("dap-view").toggle(opts.bang)
end, { bang = true })
command("DapViewWatch", require("dap-view").add_expr, {})