Skip to content

Commit

Permalink
feat(float): max height/width control
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarriga committed Apr 14, 2021
1 parent b333d8c commit 8007152
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 83 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ require("dapui").setup({
height = 10,
position = "bottom" -- Can be "bottom" or "top"
}
floating = {
max_height = nil, -- These can be integers or a float between 0 and 1.
max_width = nil -- Floats will be treated as percentage of your screen.
}
})
```

Expand Down
87 changes: 47 additions & 40 deletions lua/dapui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,57 @@ local M = {}

local listener_id = "dapui"

require("dapui.highlights")

local elements = {
STACKS = "stacks",
SCOPES = "scopes",
REPL = "repl",
WATCHES = "watches"
}

local user_config = {
icons = {
expanded = "",
collapsed = "",
circular = ""
},
mappings = {
expand = "<CR>",
open = "o",
remove = "d"
},
sidebar = {
elements = {
elements.SCOPES,
elements.STACKS,
elements.WATCHES
},
width = 40,
position = "left"
},
tray = {
elements = {
elements.REPL
},
height = 10,
position = "bottom"
},
floating = {
max_height = nil,
max_width = nil
},
windows = {
indent = 1
}
}

local function element(name)
return require("dapui.elements." .. name)
end

local open_float = nil

local function fill_config(config)
return vim.tbl_deep_extend(
"keep",
config,
{
icons = {
expanded = "",
collapsed = "",
circular = ""
},
mappings = {
expand = "<CR>",
open = "o",
remove = "d"
},
sidebar = {
elements = {
elements.SCOPES,
elements.STACKS,
elements.WATCHES
},
width = 40,
position = "left"
},
tray = {
elements = {
elements.REPL
},
height = 10,
position = "bottom"
}
}
)
return vim.tbl_deep_extend("keep", config, user_config)
end

local function query_elem_name()
Expand Down Expand Up @@ -99,24 +102,28 @@ function M.eval(expr)
end

function M.setup(config)
config = fill_config(config or {})
user_config = fill_config(config or {})

require("dapui.highlights").setup()
require("dapui.windows.float").setup(user_config.floating)

for _, module in pairs(elements) do
element(module).setup(config)
element(module).setup(user_config)
end

local sidebar_elems = {}
for _, module in pairs(config.sidebar.elements) do
for _, module in pairs(user_config.sidebar.elements) do
sidebar_elems[#sidebar_elems + 1] = element(module)
end
local tray_elems = {}
for _, module in pairs(config.tray.elements) do
for _, module in pairs(user_config.tray.elements) do
tray_elems[#tray_elems + 1] = element(module)
end

local dap = require("dap")
dap.listeners.after.event_initialized[listener_id] = function()
require("dapui.windows").open_tray(tray_elems, config.tray.position, config.tray.height)
require("dapui.windows").open_sidebar(sidebar_elems, config.sidebar.position, config.sidebar.width)
require("dapui.windows").open_tray(tray_elems, user_config.tray.position, user_config.tray.height)
require("dapui.windows").open_sidebar(sidebar_elems, user_config.sidebar.position, user_config.sidebar.width)
end

dap.listeners.before.event_terminated[listener_id] = function()
Expand Down
4 changes: 2 additions & 2 deletions lua/dapui/elements/scopes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function Element:render_variables(reference, render_state, indent, expanded)
end

if self.expanded_references[variable.variablesReference] and not expanded[variable.variablesReference] then
self:render_variables(variable.variablesReference, render_state, indent + 1, expanded)
self:render_variables(variable.variablesReference, render_state, indent + self.config.windows.indent, expanded)
end
end
end
Expand All @@ -62,7 +62,7 @@ function Element:render_scopes(render_state)
for i, scope in pairs(self.scopes or {}) do
render_state:add_match("DapUIScope", render_state:length() + 1, 1, #scope.name)
render_state:add_line(scope.name .. ":")
self:render_variables(scope.variablesReference, render_state, 1, expanded)
self:render_variables(scope.variablesReference, render_state, self.config.windows.indent, expanded)
if i < #self.scopes then
render_state:add_line()
end
Expand Down
2 changes: 1 addition & 1 deletion lua/dapui/elements/stacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function Element:render_threads(match_group, threads, render_state)
local thread = threads[ordered_keys[i]]
render_state:add_match(match_group, render_state:length() + 1, 1, #thread.name)
render_state:add_line(thread.name .. ":")
self:render_frames(self.thread_frames[thread.id], render_state, 1)
self:render_frames(self.thread_frames[thread.id], render_state, self.config.windows.indent)
if i < #ordered_keys then
render_state:add_line()
end
Expand Down
32 changes: 10 additions & 22 deletions lua/dapui/elements/watches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function Element:fill_render_state(render_state)
self.line_expr_map[line_no] = i

local prefix = self.config.icons[expr.expanded and "expanded" or "collapsed"]
local new_line = " " .. prefix
render_state:add_match("DapUIDecoration", line_no, 1, 3)
local new_line = string.rep(" ", self.config.windows.indent) .. prefix
render_state:add_match("DapUIDecoration", line_no, self.config.windows.indent, 3)

new_line = new_line .. " " .. expr.value

Expand All @@ -46,18 +46,19 @@ function Element:fill_render_state(render_state)
if expr.expanded then
local frame_line = line_no + 1
self.line_expr_map[frame_line] = i
local indent = string.rep(" ", self.config.windows.indent * 2)

local frame_prefix = " Frame: "
local frame_prefix = indent.."Frame: "
render_state:add_match("DapUIWatchesFrame", frame_line, 1, #frame_prefix)
render_state:add_line(frame_prefix .. expr.frame.name)

local val_line = frame_line + 1
local val_prefix
if expr.error then
val_prefix = " Error: "
if expr.error then
val_prefix = indent .. "Error: "
render_state:add_match("DapUIWatchesError", val_line, 1, #val_prefix)
else
val_prefix = " Value: "
val_prefix = indent .. "Value: "
render_state:add_match("DapUIWatchesValue", val_line, 1, #val_prefix)
end
for j, line in pairs(vim.split(expr.evaluated, "\n")) do
Expand Down Expand Up @@ -146,7 +147,6 @@ function _G.watches_open_expr_frame()
end
local current_expr = Element.expressions[current_expr_i]
require("dapui.util").jump_to_frame(current_expr.frame)

end

function _G.watches_remove_expr()
Expand Down Expand Up @@ -190,22 +190,10 @@ function M.on_open(buf, render_receiver)
vim.fn.prompt_setcallback(buf, add_watch)
vim.api.nvim_buf_set_option(buf, "filetype", "dapui_watches")
vim.api.nvim_buf_set_option(buf, "buftype", "prompt")
vim.api.nvim_buf_set_option(buf, 'omnifunc', "v:lua.require'dap'.omnifunc")
vim.api.nvim_buf_set_option(buf, "omnifunc", "v:lua.require'dap'.omnifunc")
pcall(vim.api.nvim_buf_set_name, buf, M.name)
vim.api.nvim_buf_set_keymap(
buf,
"n",
Element.config.mappings.expand,
"<Cmd>call v:lua.watches_toggle_expr()<CR>",
{}
)
vim.api.nvim_buf_set_keymap(
buf,
"n",
Element.config.mappings.remove,
"<Cmd>call v:lua.watches_remove_expr()<CR>",
{}
)
vim.api.nvim_buf_set_keymap(buf, "n", Element.config.mappings.expand, "<Cmd>call v:lua.watches_toggle_expr()<CR>", {})
vim.api.nvim_buf_set_keymap(buf, "n", Element.config.mappings.remove, "<Cmd>call v:lua.watches_remove_expr()<CR>", {})
vim.api.nvim_buf_set_keymap(
buf,
"n",
Expand Down
36 changes: 21 additions & 15 deletions lua/dapui/highlights.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
vim.cmd("hi default link DapUIVariable Normal")
vim.cmd("hi default DapUIScope guifg=#00F1F5")
vim.cmd("hi default DapUIType guifg=#D484FF")
vim.cmd("hi default DapUIDecoration guifg=#00F1F5")
vim.cmd("hi default DapUIThread guifg=#A9FF68")
vim.cmd("hi default DapUIStoppedThread guifg=#00f1f5")
vim.cmd("hi default link DapUIFrameName Normal")
vim.cmd("hi default DapUIFrameSource guifg=#D484FF")
vim.cmd("hi default DapUILineNumber guifg=#00f1f5")
vim.cmd("hi default DapUIFloatBorder guifg=#00F1F5")
vim.cmd("hi default DapUIWatchesHeader guifg=#00F1F5")
vim.cmd("hi default DapUIWatchesEmpty guifg=#F70067")
vim.cmd("hi default DapUIWatchesValue guifg=#A9FF68")
vim.cmd("hi default DapUIWatchesError guifg=#F70067")
vim.cmd("hi default DapUIWatchesFrame guifg=#D484FF")
local M = {}

function M.setup()
vim.cmd("hi default link DapUIVariable Normal")
vim.cmd("hi default DapUIScope guifg=#00F1F5")
vim.cmd("hi default DapUIType guifg=#D484FF")
vim.cmd("hi default DapUIDecoration guifg=#00F1F5")
vim.cmd("hi default DapUIThread guifg=#A9FF68")
vim.cmd("hi default DapUIStoppedThread guifg=#00f1f5")
vim.cmd("hi default link DapUIFrameName Normal")
vim.cmd("hi default DapUIFrameSource guifg=#D484FF")
vim.cmd("hi default DapUILineNumber guifg=#00f1f5")
vim.cmd("hi default DapUIFloatBorder guifg=#00F1F5")
vim.cmd("hi default DapUIWatchesHeader guifg=#00F1F5")
vim.cmd("hi default DapUIWatchesEmpty guifg=#F70067")
vim.cmd("hi default DapUIWatchesValue guifg=#A9FF68")
vim.cmd("hi default DapUIWatchesError guifg=#F70067")
vim.cmd("hi default DapUIWatchesFrame guifg=#D484FF")
end

return M
2 changes: 1 addition & 1 deletion lua/dapui/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local function init_win_settings(win)
relativenumber = false,
number = false,
winfixwidth = true,
wrap = false,
wrap = false
}
for key, val in pairs(win_settings) do
vim.api.nvim_win_set_option(win, key, val)
Expand Down
20 changes: 18 additions & 2 deletions lua/dapui/windows/float.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
local M = {}
local api = vim.api
local config = {
max_height = nil,
max_width = nil
}

local Float = {ids = {}, listeners = {close = {}}, position = {}}

function M.setup(float_config)
config = vim.tbl_deep_extend("keep", float_config, config)
end

local function create_border_lines(border_opts)
local width = border_opts.width
local height = border_opts.height
Expand All @@ -21,8 +29,16 @@ local function create_border_opts(content_width, content_height, position)
local vert_anchor = "N"
local hor_anchor = "W"

local height = math.min(content_height + 2, vim.o.lines - 2)
local width = math.min(content_width + 4, vim.o.columns - 2)
local max_height = config.max_height or vim.o.lines
local max_width = config.max_width or vim.o.columns
if 0 < max_height and max_height < 1 then
max_height = math.floor(vim.o.lines * max_height)
end
if 0 < max_width and max_width < 1 then
max_width = math.floor(vim.o.columns * max_width)
end
local height = math.min(content_height + 2, max_height - 2)
local width = math.min(content_width + 4, max_width - 2)

local row = line_no + math.min(0, vim.o.lines - (height + line_no + 2))
local col = col_no + math.min(0, vim.o.columns - (width + col_no + 2))
Expand Down

0 comments on commit 8007152

Please sign in to comment.