diff --git a/README.md b/README.md index 630a098..80331c6 100644 --- a/README.md +++ b/README.md @@ -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), diff --git a/lua/iron/core.lua b/lua/iron/core.lua index 1223021..b96f89e 100644 --- a/lua/iron/core.lua +++ b/lua/iron/core.lua @@ -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() @@ -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 @@ -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' " diff --git a/lua/iron/dap.lua b/lua/iron/dap.lua new file mode 100644 index 0000000..4fe689e --- /dev/null +++ b/lua/iron/dap.lua @@ -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