Skip to content

Commit

Permalink
Add autostart setting to implicitly create sessions in REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Dec 5, 2024
1 parent ab55b6c commit 310ca72
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 37 deletions.
6 changes: 6 additions & 0 deletions doc/dap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ The configuration values are set via `dap.defaults.fallback` (for global) or
dap.defaults.fallback.on_output = function(session, output_event)
-- ignore all outputs
end


- `autostart`. Name of a configuration to start implicitly if evaluating an
expression in the REPL without active session.
<

Some more examples:
Expand All @@ -613,6 +617,8 @@ Some more examples:
dap.defaults.python.terminal_win_cmd = 'belowright new'

dap.defaults.java.auto_continue_if_many_stopped = false

dap.defaults.fallback.autostart = "nluarepl"
<

==============================================================================
Expand Down
106 changes: 69 additions & 37 deletions lua/dap/repl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,45 +218,39 @@ local function print_threads(threads)
end


function execute(text)
if text == '' then
if history.last then
text = history.last
else
return
end
else
history.last = text
if #history.entries == history.max_size then
table.remove(history.entries, 1)
---@param confname string
---@return dap.Session?
local function trystart(confname)
assert(coroutine.running() ~= nil, "Must run in coroutine")
local dap = require("dap")
local bufnr = api.nvim_get_current_buf()
for _, get_configs in pairs(dap.providers.configs) do
local configs = get_configs(bufnr)
for _, config in ipairs(configs) do
if confname == config.name then
dap.run(config)
end
end
table.insert(history.entries, text)
history.idx = #history.entries + 1
end
return dap.session()
end


local function coexecute(text)
assert(coroutine.running() ~= nil, "Must run in coroutine")

local splitted_text = vim.split(text, ' ')
local session = get_session()
if vim.tbl_contains(M.commands.exit, text) then
if session then
-- Should result in a `terminated` event which closes the session and sets it to nil
session:disconnect()
if not session then
local autostart = require("dap").defaults[vim.bo.filetype].autostart
if autostart then
session = trystart(autostart)
end
api.nvim_command('close')
return
end
if vim.tbl_contains(M.commands.help, text) then
print_commands()
return
elseif vim.tbl_contains(M.commands.clear, text) then
if repl.buf and api.nvim_buf_is_loaded(repl.buf) then
M.clear()
if not session then
M.append('No active debug session')
return
end
return
end
if not session then
M.append('No active debug session')
return
end
local words = vim.split(text, ' ', { plain = true })
if vim.tbl_contains(M.commands.continue, text) then
require('dap').continue()
elseif vim.tbl_contains(M.commands.next_, text) then
Expand All @@ -281,18 +275,18 @@ function execute(text)
elseif vim.tbl_contains(M.commands.down, text) then
session:_frame_delta(-1)
M.print_stackframes()
elseif vim.tbl_contains(M.commands.goto_, splitted_text[1]) then
if splitted_text[2] then
session:_goto(tonumber(splitted_text[2]))
elseif vim.tbl_contains(M.commands.goto_, words[1]) then
if words[2] then
session:_goto(tonumber(words[2]))
end
elseif vim.tbl_contains(M.commands.scopes, text) then
print_scopes(session.current_frame)
elseif vim.tbl_contains(M.commands.threads, text) then
print_threads(vim.tbl_values(session.threads))
elseif vim.tbl_contains(M.commands.frames, text) then
M.print_stackframes()
elseif M.commands.custom_commands[splitted_text[1]] then
local command = splitted_text[1]
elseif M.commands.custom_commands[words[1]] then
local command = words[1]
local args = string.sub(text, string.len(command)+2)
M.commands.custom_commands[command](args)
else
Expand All @@ -301,6 +295,44 @@ function execute(text)
end


function execute(text)
if text == '' then
if history.last then
text = history.last
else
return
end
else
history.last = text
if #history.entries == history.max_size then
table.remove(history.entries, 1)
end
table.insert(history.entries, text)
history.idx = #history.entries + 1
end
if vim.tbl_contains(M.commands.exit, text) then
local session = get_session()
if session then
-- Should result in a `terminated` event which closes the session and sets it to nil
session:disconnect()
end
api.nvim_command('close')
return
end
if vim.tbl_contains(M.commands.help, text) then
print_commands()
elseif vim.tbl_contains(M.commands.clear, text) then
if repl.buf and api.nvim_buf_is_loaded(repl.buf) then
M.clear()
end
else
require("dap.async").run(function()
coexecute(text)
end)
end
end


--- Add and execute text as if entered directly
function M.execute(text)
M.append("dap> " .. text .. "\n", "$", { newline = true })
Expand Down

0 comments on commit 310ca72

Please sign in to comment.