Skip to content

Commit ebd87bd

Browse files
committed
Don't add extra newlines to REPL on output events
Closes #845 and #854
1 parent 0878669 commit ebd87bd

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

lua/dap.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ function M.run_to_cursor()
850850
end
851851

852852

853-
---@param opts table<string, any>
853+
---@param opts? table<string, any>
854854
function M.continue(opts)
855855
if not session then
856856
session = first_stopped_session()

lua/dap/repl.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ end
351351

352352
---@param line string
353353
---@param lnum (integer|string)?
354-
function M.append(line, lnum)
354+
---@param opts? {newline: boolean}
355+
function M.append(line, lnum, opts)
356+
opts = opts or {}
355357
local buf = repl._init_buf()
356358
if api.nvim_get_current_win() == repl.win and lnum == '$' then
357359
lnum = nil
@@ -360,12 +362,17 @@ function M.append(line, lnum)
360362
line = line:gsub('\r\n', '\n')
361363
end
362364
local lines = vim.split(line, '\n')
363-
if #lines > 1 and lines[#lines] == '' then
364-
table.remove(lines)
365-
end
366365
if lnum == '$' or not lnum then
367366
lnum = api.nvim_buf_line_count(buf) - 1
368-
api.nvim_buf_set_lines(buf, -1, -1, true, lines)
367+
if opts.newline == false then
368+
local last_line = api.nvim_buf_get_lines(buf, -2, -1, true)[1]
369+
if vim.startswith(last_line, 'dap> ') then
370+
table.insert(lines, 1, '')
371+
end
372+
api.nvim_buf_set_text(buf, lnum, #last_line, lnum, #last_line, lines)
373+
else
374+
api.nvim_buf_set_lines(buf, -1, -1, true, lines)
375+
end
369376
else
370377
api.nvim_buf_set_lines(buf, lnum, lnum, true, lines)
371378
end
@@ -438,6 +445,7 @@ do
438445
return {}
439446
end
440447
end
448+
assert(session, 'Session must exist if supportsCompletionsRequest is true')
441449
session:request('completions', {
442450
frameId = (session.current_frame or {}).id;
443451
text = line_to_cursor;

lua/dap/session.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ function Session.event_output(_, body)
710710
if body.category == 'telemetry' then
711711
log.info('Telemetry', body.output)
712712
else
713-
repl.append(body.output, '$')
713+
repl.append(body.output, '$', { newline = false })
714714
end
715715
end
716716

lua/dap/ui.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ function M.layer(buf)
455455
---
456456
---@generic T
457457
---@param xs T[]
458-
---@param render_fn fun(T):string
458+
---@param render_fn? fun(T):string
459459
---@param context table|nil
460460
---@param start nil|number 0-indexed
461461
---@param end_ nil|number 0-indexed exclusive

tests/repl_spec.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local api = vim.api
2+
3+
describe('dap.repl', function()
4+
it("append doesn't add newline with newline = false", function()
5+
local repl = require('dap.repl')
6+
local buf = repl.open()
7+
repl.append('foo', nil, { newline = false })
8+
repl.append('bar', nil, { newline = false })
9+
repl.append('\nbaz\n', nil, { newline = false })
10+
11+
local lines = api.nvim_buf_get_lines(buf, 0, -1, true)
12+
assert.are.same({'foobar', 'baz', ''}, lines)
13+
end)
14+
end)

0 commit comments

Comments
 (0)