Skip to content

Commit

Permalink
feat: element specific mappings
Browse files Browse the repository at this point in the history
See #158
  • Loading branch information
rcarriga committed Oct 26, 2022
1 parent 31a0668 commit 111236e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ require("dapui").setup({
repl = "r",
toggle = "t",
},
-- Use this to override mappings for specific elements
element_mappings = {
-- Example:
-- stacks = {
-- open = "<CR>",
-- expand = "o",
-- }
},
-- Expand lines larger than the window
-- Requires >= 0.7
expand_lines = vim.fn.has("nvim-0.7") == 1,
Expand Down
13 changes: 13 additions & 0 deletions lua/dapui/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ local default_config = {
[M.actions.REPL] = "r",
[M.actions.TOGGLE] = "t",
},
element_mappings = {},
expand_lines = vim.fn.has("nvim-0.7") == 1,
layouts = {
{
Expand Down Expand Up @@ -171,6 +172,13 @@ require('dapui').setup(
filled.layouts = config.layouts
end
filled.mappings = fill_mappings(filled.mappings)

local element_mappings = {}
for elem, mappings in pairs(filled.element_mappings) do
element_mappings[elem] = fill_mappings(mappings)
end

filled.element_mappings = element_mappings
filled.floating.mappings = fill_mappings(filled.floating.mappings)
for i, layout in ipairs(filled.layouts) do
filled.layouts[i] = fill_elements(layout)
Expand Down Expand Up @@ -216,6 +224,11 @@ function M.expand_lines()
return user_config.expand_lines
end

function M.element_mapping(element, action)
return (user_config.element_mappings[element] and user_config.element_mappings[element][action])
or user_config.mappings[action]
end

setmetatable(M, {
__index = function(_, key)
return user_config[key]
Expand Down
4 changes: 2 additions & 2 deletions lua/dapui/render/canvas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ end
---Apply a render.canvas to a buffer
---@param state dapui.Canvas
---@param buffer number
function M.render_buffer(state, buffer)
function M.render_buffer(state, buffer, element)
local success, _ = pcall(api.nvim_buf_set_option, buffer, "modifiable", true)
if not success then
return false
Expand All @@ -151,7 +151,7 @@ function M.render_buffer(state, buffer)
_mappings[buffer] = state.mappings
for action, _ in pairs(state.mappings) do
util.apply_mapping(
config.mappings()[action],
config.element_mapping(element, action),
"<cmd>lua require('dapui.render.canvas')._mapping('" .. action .. "')<CR>",
buffer
)
Expand Down
3 changes: 2 additions & 1 deletion lua/dapui/render/loop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ end
function M.run(element_names)
element_names = get_elements(element_names)
for _, elem_name in pairs(element_names) do
local elem_id = string.gsub(string.lower(elem_name), "dap ", "")
local canvas_state = canvas_states[elem_name]
if not vim.tbl_isempty(canvas_state.buffers) then
local canvas = Canvas.new()
canvas:set_expand_lines(config.expand_lines())
canvas_state.element.render(canvas)
if canvas.valid then
for _, buf in pairs(canvas_state.buffers) do
local rendered = Canvas.render_buffer(canvas, buf)
local rendered = Canvas.render_buffer(canvas, buf, elem_id)
if rendered then
for _, listener in pairs(canvas_state.listeners[M.EVENTS.RENDER] or {}) do
listener(buf, canvas)
Expand Down

0 comments on commit 111236e

Please sign in to comment.