nvim-dap-ui
nvim-dap-ui copied to clipboard
Split window messes with dap-ui layout
I have dap-ui
layout setup like this:
require('dapui').setup({
layouts = {
{
elements = {
{
id = 'scopes',
size = 0.25,
},
{
id = 'watches',
size = 0.25,
},
{
id = 'breakpoints',
size = 0.25,
},
{
id = 'stacks',
size = 0.25,
},
},
position = 'left',
size = 0.28,
},
{
elements = {
{
id = 'repl',
size = 0.5,
},
{
id = 'console',
size = 0.5,
},
},
position = 'bottom',
size = 0.3,
},
},
})
Now, whenever I split a window to the left and close it, it messes with the layouts. For example:
Is there anyway to prevent this?
+1
Hi, I use nvim-tree and had the same issue as you. When opening and closing it, my UI would be all the way messed up. I found a solution though, and a way to automate it.
You should be able to paste this in your nvim-dap.lua config. One thing to note though, in my code below, for the variables dap and dapui, dap = require("dap") and dapui = require("dapui").
Let me know if it works for yall :)
--Below works for when buffers are created and destroyed but not for buffers that are:
-- --> hidden or (aka ToggleTerm)
-- --> resized! (When resizing splits)
vim.api.nvim_create_augroup("DAP_UI_RESET", {clear = true})
local bufferNames = {}
vim.api.nvim_create_autocmd( {"BufWinEnter"},{
group = "DAP_UI_RESET",
pattern = "*",
callback = function()
local bufferName = vim.fn.expand("%")
if dap.session() and bufferName ~= "DAP Watches" and bufferName ~= "DAP Stacks" and bufferName ~= "DAP Breakpoints" and bufferName ~= "DAP Scopes" and bufferName ~= "DAP Console" and not string.find(bufferName, "%[dap%-repl%-", 1) and bufferName ~= "DAP Hover" then
table.insert(bufferNames, vim.fn.expand("%:p"))
dapui.open({reset = true})
end
end
})
vim.api.nvim_create_autocmd({"BufUnload"}, {
group = "DAP_UI_RESET",
pattern = "*",
callback = function ()
if dap.session() then
for i = 1, #bufferNames do
local index = -1
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if bufferNames[i] == vim.api.nvim_buf_get_name(buf) then
index = vim.api.nvim_buf_get_number(buf)
break
end
end
vim.schedule(function ()
if not vim.api.nvim_buf_is_loaded(index) then
table.remove(bufferNames, i)
dapui.open({reset = true})
end
end)
end
end
end
})
Thanks for the suggestion! But it doesn't work for me.
Ah, that's unfortunate.
+1
Hi,
Just a quick update on the code above.
local dap = require("dap")
local dapui = require("dapui")
--events to correctly resize window after opening/closing split
vim.api.nvim_create_augroup("DAP_UI_RESET", {clear = true})
local bufferNames = {}
vim.api.nvim_create_autocmd( {"BufWinEnter"},{ --onsplit event
group = "DAP_UI_RESET",
pattern = "*",
callback = function()
local bufferName = vim.fn.expand("%")
if dap.session() and bufferName ~= "DAP Watches" and bufferName ~= "DAP Stacks" and bufferName ~= "DAP Breakpoints" and bufferName ~= "DAP Scopes" and bufferName ~= "DAP Console" and not string.find(bufferName, "%[dap%-repl%-", 1) and bufferName ~= "DAP Hover" and bufferName ~= "[dap-terminal] Launch file" then
table.insert(bufferNames, vim.fn.expand("%:p")) --This takes the full path-"name" of each buffer.
print("Buffer Name - " .. vim.fn.expand("%"))
dapui.open({reset = true})
end
end
})
vim.api.nvim_create_autocmd({"BufUnload", "BufHidden"}, {
group = "DAP_UI_RESET",
pattern = "*",
callback = function (args)
if dap.session() then --First we find the Buffer nbr of each previously created split-Buffers. Second, we circle back and check that each of these buffers are still visible, and if not, we resize the window.
for i = 1, #bufferNames do --First
local index = -1
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if bufferNames[i] == vim.api.nvim_buf_get_name(buf) then
index = vim.api.nvim_buf_get_number(buf)
break
end
end
vim.schedule(function () --Second
if vim.fn.bufwinid(index) == -1 then --checks to see if window still exists in current tab, -1 means no.
table.remove(bufferNames, i)
dapui.open({reset = true})
end
end)
end
end
end
})
This is my current set-up, inside of the lua config file. Seems to work well with both nvim-tree and toggleterm plugins.
Not sure if there's an easier fix to this issue inside of the codebase, but this seems to work for me.