diff --git a/lua/dap/repl.lua b/lua/dap/repl.lua index 6dadd4e6..223cac4b 100644 --- a/lua/dap/repl.lua +++ b/lua/dap/repl.lua @@ -303,6 +303,13 @@ end --- Add and execute text as if entered directly function M.execute(text) M.append("dap> " .. text .. "\n", "$", { newline = true }) + local numlines = api.nvim_buf_line_count(repl.buf) + if repl.win and api.nvim_win_is_valid(repl.win) then + pcall(api.nvim_win_set_cursor, repl.win, { numlines, 0 }) + api.nvim_win_call(repl.win, function() + vim.cmd.normal({"zt", bang = true }) + end) + end execute(text) end @@ -344,8 +351,10 @@ local function select_history(delta) local text = history.entries[history.idx] if text then local lnum = vim.fn.line('$') - api.nvim_buf_set_lines(repl.buf, lnum - 1, lnum, true, {'dap> ' .. text }) - vim.fn.setcursorcharpos({ lnum, vim.fn.col('$') }) -- move cursor to the end of line + local lines = vim.split(text, "\n", { plain = true }) + lines[1] = "dap> " .. lines[1] + api.nvim_buf_set_lines(repl.buf, lnum - 1, -1, true, lines) + vim.fn.setcursorcharpos({ vim.fn.line("$"), vim.fn.col('$') }) -- move cursor to the end of line end end diff --git a/plugin/dap.lua b/plugin/dap.lua index bc6f46b4..350554bc 100644 --- a/plugin/dap.lua +++ b/plugin/dap.lua @@ -29,10 +29,10 @@ cmd('DapRestartFrame', function() require('dap').restart_frame() end, { nargs = if api.nvim_create_autocmd then - local group = api.nvim_create_augroup('dap-launch.json', { clear = true }) + local launchjson_group = api.nvim_create_augroup('dap-launch.json', { clear = true }) local pattern = '*/.vscode/launch.json' api.nvim_create_autocmd('BufNewFile', { - group = group, + group = launchjson_group, pattern = pattern, callback = function(args) local lines = { @@ -51,10 +51,41 @@ if api.nvim_create_autocmd then end }) api.nvim_create_autocmd('BufWritePost', { - group = group, + group = launchjson_group, pattern = pattern, callback = function(args) require('dap.ext.vscode').load_launchjs(args.file) end }) + + api.nvim_create_autocmd("BufReadCmd", { + group = api.nvim_create_augroup("dap-readcmds", { clear = true }), + pattern = "dap-eval://*", + callback = function() + local bufnr = api.nvim_get_current_buf() + local fname = api.nvim_buf_get_name(bufnr) + vim.bo[bufnr].swapfile = false + vim.bo[bufnr].buftype = "acwrite" + vim.bo[bufnr].bufhidden = "wipe" + local ft = fname:match("dap%-eval://(%w+)(.*)") + if ft and ft ~= "" then + vim.bo[bufnr].filetype = ft + else + local altbuf = vim.fn.bufnr("#", false) + if altbuf then + vim.bo[bufnr].filetype = vim.bo[altbuf].filetype + end + end + api.nvim_create_autocmd("BufWriteCmd", { + buffer = bufnr, + callback = function(args) + vim.bo[args.buf].modified = false + local repl = require("dap.repl") + local lines = api.nvim_buf_get_lines(args.buf, 0, -1, true) + repl.execute(table.concat(lines, "\n")) + repl.open() + end, + }) + end, + }) end