Skip to content

Commit 9d835d3

Browse files
committed
Add DapEval command
Creates a new window with a `dap-eval://` scratch buffer Supports range mode to pre-fill it with the selected content. Supports bang to immediately evaluate the content This is a variant addressing #665
1 parent fc1f795 commit 9d835d3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

plugin/dap.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,41 @@ cmd("DapNew", dapnew, {
6868
end
6969
})
7070

71+
cmd("DapEval", function(params)
72+
local oldbuf = api.nvim_get_current_buf()
73+
local name = string.format("dap-eval://%s", vim.bo[oldbuf].filetype)
74+
if params.smods.vertical then
75+
vim.cmd.vsplit({name})
76+
elseif params.smods.tab == 1 then
77+
vim.cmd.tabedit(name)
78+
else
79+
vim.cmd.split({name, mods = params.smods})
80+
end
81+
local newbuf = api.nvim_get_current_buf()
82+
if params.range ~= 0 then
83+
local lines = api.nvim_buf_get_lines(oldbuf, params.line1 -1 , params.line2, true)
84+
local indent = math.huge
85+
for _, line in ipairs(lines) do
86+
indent = math.min(line:find("[^ ]") or math.huge, indent)
87+
end
88+
if indent ~= math.huge and indent > 0 then
89+
for i, line in ipairs(lines) do
90+
lines[i] = line:sub(indent)
91+
end
92+
end
93+
api.nvim_buf_set_lines(newbuf, 0, -1, true, lines)
94+
vim.bo[newbuf].modified = false
95+
end
96+
if params.bang then
97+
vim.cmd.w()
98+
end
99+
end, {
100+
nargs = 0,
101+
range = "%",
102+
bang = true,
103+
desc = "Create a new window & buffer to evaluate expressions",
104+
})
105+
71106

72107
if api.nvim_create_autocmd then
73108
local launchjson_group = api.nvim_create_augroup('dap-launch.json', { clear = true })

0 commit comments

Comments
 (0)