Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autostart setting to implicitly create sessions in REPL #1385

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
108 changes: 71 additions & 37 deletions lua/dap/repl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local function new_buf()
local prev_buf = api.nvim_get_current_buf()
local buf = api.nvim_create_buf(true, true)
api.nvim_buf_set_name(buf, string.format('[dap-repl-%d]', buf))
vim.b[buf]["dap-srcft"] = vim.bo[prev_buf].filetype
vim.bo[buf].buftype = "prompt"
vim.bo[buf].omnifunc = "v:lua.require'dap.repl'.omnifunc"
vim.bo[buf].buflisted = false
Expand Down Expand Up @@ -218,45 +219,40 @@ 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 ft = vim.b["dap-srcft"] or vim.bo.filetype
local autostart = require("dap").defaults[ft].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 +277,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 +297,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
Loading