From 9d835d34e928fcf05b985a18a301736f2f055916 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Sat, 1 Jun 2024 14:41:43 +0200 Subject: [PATCH] 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 https://github.com/mfussenegger/nvim-dap/issues/665 --- plugin/dap.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/plugin/dap.lua b/plugin/dap.lua index 20106cee..9607870e 100644 --- a/plugin/dap.lua +++ b/plugin/dap.lua @@ -68,6 +68,41 @@ cmd("DapNew", dapnew, { end }) +cmd("DapEval", function(params) + local oldbuf = api.nvim_get_current_buf() + local name = string.format("dap-eval://%s", vim.bo[oldbuf].filetype) + if params.smods.vertical then + vim.cmd.vsplit({name}) + elseif params.smods.tab == 1 then + vim.cmd.tabedit(name) + else + vim.cmd.split({name, mods = params.smods}) + end + local newbuf = api.nvim_get_current_buf() + if params.range ~= 0 then + local lines = api.nvim_buf_get_lines(oldbuf, params.line1 -1 , params.line2, true) + local indent = math.huge + for _, line in ipairs(lines) do + indent = math.min(line:find("[^ ]") or math.huge, indent) + end + if indent ~= math.huge and indent > 0 then + for i, line in ipairs(lines) do + lines[i] = line:sub(indent) + end + end + api.nvim_buf_set_lines(newbuf, 0, -1, true, lines) + vim.bo[newbuf].modified = false + end + if params.bang then + vim.cmd.w() + end +end, { + nargs = 0, + range = "%", + bang = true, + desc = "Create a new window & buffer to evaluate expressions", +}) + if api.nvim_create_autocmd then local launchjson_group = api.nvim_create_augroup('dap-launch.json', { clear = true })