Skip to content

Commit

Permalink
feat: multiple layouts (#105)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Switches from tray/sidebar in config to layouts

Closes #86
  • Loading branch information
rcarriga committed Jun 18, 2022
1 parent 85b266e commit 923eb14
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 103 deletions.
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,31 @@ require("dapui").setup({
-- Expand lines larger than the window
-- Requires >= 0.7
expand_lines = vim.fn.has("nvim-0.7"),
sidebar = {
-- You can change the order of elements in the sidebar
elements = {
-- Provide as ID strings or tables with "id" and "size" keys
{
id = "scopes",
size = 0.25, -- Can be float or integer > 1
-- Layouts define sections of the screen to place windows.
-- The position can be "left", "right", "top" or "bottom".
-- The size specifies the height/width depending on position.
-- Elements are the elements shown in the layout (in order).
-- Layouts are opened in order so that earlier layouts take priority in window sizing.
layouts = {
{
elements = {
-- Elements can be strings or table with id and size keys.
{ id = "scopes", size = 0.25 },
"breakpoints",
"stacks",
"watches",
},
{ id = "breakpoints", size = 0.25 },
{ id = "stacks", size = 0.25 },
{ id = "watches", size = 00.25 },
size = 40,
position = "left",
},
{
elements = {
"repl",
"console",
},
size = 10,
position = "bottom",
},
size = 40,
position = "left", -- Can be "left", "right", "top", "bottom"
},
tray = {
elements = { "repl", "console" },
size = 10,
position = "bottom", -- Can be "left", "right", "top", "bottom"
},
floating = {
max_height = nil, -- These can be integers or a float between 0 and 1.
Expand All @@ -95,7 +101,7 @@ require("dapui").setup({
},
},
windows = { indent = 1 },
render = {
render = {
max_type_length = nil, -- Can be integer or nil.
}
})
Expand Down
96 changes: 75 additions & 21 deletions lua/dapui/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,30 @@ local default_config = {
[M.actions.TOGGLE] = "t",
},
expand_lines = vim.fn.has("nvim-0.7") == 1,
sidebar = {
-- You can change the order of elements in the sidebar
elements = {
-- Provide IDs as strings or tables with "id" and "size" keys
{
id = M.elements.SCOPES,
size = 0.25, -- Can be float or integer > 1
layouts = {
{
-- You can change the order of elements in the sidebar
elements = {
-- Provide IDs as strings or tables with "id" and "size" keys
{
id = M.elements.SCOPES,
size = 0.25, -- Can be float or integer > 1
},
{ id = M.elements.BREAKPOINTS, size = 0.25 },
{ id = M.elements.STACKS, size = 0.25 },
{ id = M.elements.WATCHES, size = 0.25 },
},
{ id = M.elements.BREAKPOINTS, size = 0.25 },
{ id = M.elements.STACKS, size = 0.25 },
{ id = M.elements.WATCHES, size = 0.25 },
size = 40,
position = "left", -- Can be "left" or "right"
},
{
elements = {
M.elements.REPL,
M.elements.CONSOLE,
},
size = 10,
position = "bottom", -- Can be "bottom" or "top"
},
size = 40,
position = "left", -- Can be "left" or "right"
},
tray = {
elements = { M.elements.REPL, M.elements.CONSOLE },
size = 10,
position = "bottom", -- Can be "bottom" or "top"
},
floating = {
max_height = nil, -- These can be integers or a float between 0 and 1.
Expand All @@ -74,6 +79,11 @@ local user_config = {}
local function fill_elements(area)
area = vim.deepcopy(area)
local filled = {}
vim.validate({
size = { area.size, "number" },
elements = { area.elements, "table" },
position = { area.position, "string" },
})
for i, element in ipairs(area.elements) do
if type(element) == "string" then
filled[i] = { id = element, size = 1 / #area.elements }
Expand All @@ -91,7 +101,12 @@ local function dep_warning(message)
vim.schedule(function()
if not dep_warnings[message] then
dep_warnings[message] = true
vim.notify(message, "warn", { title = "nvim-dap-ui" })
vim.notify(message, "warn", {
title = "nvim-dap-ui",
on_open = function(win)
vim.api.nvim_buf_set_option(vim.api.nvim_win_get_buf(win), "filetype", "markdown")
end,
})
end
end)
end
Expand All @@ -105,11 +120,46 @@ local function fill_mappings(mappings)
end

function M.setup(config)
local filled = vim.tbl_deep_extend("keep", config or {}, default_config)
config = config or {}
local filled = vim.tbl_deep_extend("keep", config, default_config)

if config.sidebar or config.tray then
dep_warning([[The 'sidebar' and 'tray' options are deprecated. Please use 'layouts' instead.
To replicate previous default behaviour, provide the following
```lua
require('dapui').setup(
layouts = {
{
elements = {
'scopes',
'breakpoints',
'stacks',
'watches',
},
size = 40,
position = 'left',
},
{
elements = {
'repl',
'console',
},
size = 10,
position = 'bottom',
},
},
)
```]])
end

if config.layouts then
filled.layouts = config.layouts
end
filled.mappings = fill_mappings(filled.mappings)
filled.floating.mappings = fill_mappings(filled.floating.mappings)
filled.sidebar = fill_elements(filled.sidebar)
filled.tray = fill_elements(filled.tray)
for i, layout in ipairs(filled.layouts) do
filled.layouts[i] = fill_elements(layout)
end

user_config = filled
require("dapui.config.highlights").setup()
Expand All @@ -131,6 +181,10 @@ function M.tray()
return user_config.tray
end

function M.layouts()
return user_config.layouts
end

function M.floating()
return user_config.floating
end
Expand Down
122 changes: 78 additions & 44 deletions lua/dapui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,63 +135,97 @@ function dapui.setup(user_config)
end)
end

---Close either or both the tray and sidebar
---@param component string: "tray" or "sidebar"
function dapui.close(component)
windows.tray:update_sizes()
windows.sidebar:update_sizes()
if not component or component == "tray" then
windows.tray:update_sizes()
windows.tray:close()
end
if not component or component == "sidebar" then
windows.sidebar:update_sizes()
windows.sidebar:close()
---Close one or all of the window layouts
---@param layout number|nil: Index of layout in config
function dapui.close(layout)
for _, win_layout in ipairs(windows.layouts) do
win_layout:update_sizes()
end
for i, win_layout in ipairs(windows.layouts) do
if not layout or layout == i then
win_layout:update_sizes()
win_layout:close()
end
end
end

---@generic T
---@param list T[]
---@return fun(): number, T
local function reverse(list)
local i = #list + 1
return function()
i = i - 1
if i <= 0 then
return nil
end
return i, list[i]
end
end

---Open either or both the tray and sidebar
---@param component string: "tray" or "sidebar"
function dapui.open(component)
windows.tray:update_sizes()
windows.sidebar:update_sizes()
local open_sidebar = false
if component == "tray" and windows.sidebar:is_open() then
windows.sidebar:close()
open_sidebar = true
---Open one or all of the window layouts
---@param layout number|nil: Index of layout in config
function dapui.open(layout)
for _, win_layout in ipairs(windows.layouts) do
win_layout:update_sizes()
end
if not component or component == "tray" then
windows.tray:open()
local closed = {}
if layout then
for i = 1, (layout and layout - 1) or #windows.layouts, 1 do
if windows.layouts[i]:is_open() then
closed[#closed + 1] = i
windows.layouts[i]:close()
end
end
end

for i, win_layout in reverse(windows.layouts) do
if not layout or layout == i then
win_layout:open()
end
end
if not component or component == "sidebar" then
windows.sidebar:open()

if #closed > 0 then
for _, i in ipairs(closed) do
windows.layouts[i]:open()
end
end
if open_sidebar then
windows.sidebar:open()

for _, win_layout in ipairs(windows.layouts) do
win_layout:resize()
end
windows.tray:resize()
end

---Toggle either or both the tray and sidebar
---@param component string: "tray" or "sidebar"
function dapui.toggle(component)
windows.tray:update_sizes()
windows.sidebar:update_sizes()
local open_sidebar = false
if component == "tray" and windows.sidebar:is_open() then
windows.sidebar:close()
open_sidebar = true
---Toggle one or all of the window layouts.
---@param layout number|nil: Index of layout in config
function dapui.toggle(layout)
for _, win_layout in reverse(windows.layouts) do
win_layout:update_sizes()
end
if not component or component == "tray" then
windows.tray:toggle()

local closed = {}
if layout then
for i = 1, (layout and layout - 1) or #windows.layouts, 1 do
if windows.layouts[i]:is_open() then
closed[#closed + 1] = i
windows.layouts[i]:close()
end
end
end
if not component or component == "sidebar" then
windows.sidebar:toggle()

for i, win_layout in reverse(windows.layouts) do
if not layout or layout == i then
win_layout:toggle()
end
end
if open_sidebar then
windows.sidebar:open()

for _, i in reverse(closed) do
windows.layouts[i]:open()
end

for _, win_layout in ipairs(windows.layouts) do
win_layout:resize()
end
windows.tray:resize()
end

return dapui
Loading

0 comments on commit 923eb14

Please sign in to comment.