Skip to content

Add DAP repl integration #427

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ iron.setup {
-- or return a string name such as the following
-- return "iron"
end,
-- Send selections to the DAP repl if an nvim-dap session is running.
dap_integration = true
-- How the repl window will be displayed
-- See below for more information
repl_open_cmd = view.bottom(40),
Expand Down
16 changes: 16 additions & 0 deletions lua/iron/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ end
-- supplied as argument.
-- @param ft filetype
core.repl_for = function(ft)
if require('iron.dap').is_dap_session_running() then
-- If there's a dap session running, default to the dap repl. By
-- intercepting here, we can support dap repls in filetypes that aren't
-- normally supported (e.g. java).
-- TODO find and open the dap repl window?
return
end
local meta = ll.get(ft)
if ll.repl_exists(meta) then
local currwin = vim.api.nvim_get_current_win()
Expand Down Expand Up @@ -239,6 +246,11 @@ local send = function(ft, data)
-- track non-default REPls.
local meta = vim.b[0].repl

if require('iron.dap').is_dap_session_running() then
require('iron.dap').send_to_dap(data)
return
end

-- However, this attached meta may associated with a REPL that has been
-- closed, we need to check for that.
-- If the attached REPL is not existed or has been closed, we will try to
Expand Down Expand Up @@ -756,6 +768,10 @@ core.setup = function(opts)
end
end

if opts.config.dap_integration then
require('iron.dap').enable_integration()
end

if ll.tmp.repl_open_cmd == nil then
local msg = "A default repl_open_cmd was not set. "
msg = msg .. "Please set a default by adding '_DEFAULT' "
Expand Down
31 changes: 31 additions & 0 deletions lua/iron/dap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local M = {}

local is_dap_integration_enabled = false

--- Sets up a hook to keep track of the DAP session state.
function M.enable_integration()
is_dap_integration_enabled = true
end

--- Returns true if dap_integration is enabled and a dap session is running.
--- This function will always return false if dap_integration is not enabled.
--- @return boolean
function M.is_dap_session_running()
local has_dap, dap = pcall(require, 'dap')
return has_dap and is_dap_integration_enabled and dap.session() ~= nil
end

--- Send the lines to the dap-repl
--- @param lines string|string[]
function M.send_to_dap(lines)
local text
if type(lines) == 'table' then
text = table.concat(lines, "\n"):gsub('\r', '')
else
text = lines
end
require('dap').repl.execute(text)
require('dap').repl.open()
end

return M