Skip to content

Commit

Permalink
Allow adapters to define reverse request handlers
Browse files Browse the repository at this point in the history
Alternative to #281
  • Loading branch information
mfussenegger committed Sep 30, 2021
1 parent 60cce6a commit 65863f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lua/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,9 @@ function M.attach(adapter, config, opts, bwc_dummy)
utils.notify('Config needs the `request` property which must be one of `attach` or `launch`', vim.log.levels.ERROR)
return
end
local host = assert(adapter.host, 'Adapter used with attach must have a host property')
local port = assert(adapter.port, 'Adapter used with attach must have a port property')
session = require('dap.session'):connect(host, port, opts, function(err)
assert(adapter.host, 'Adapter used with attach must have a host property')
assert(adapter.port, 'Adapter used with attach must have a port property')
session = require('dap.session'):connect(adapter, opts, function(err)
assert(not err, vim.inspect(err))
session:initialize(config, adapter)
end)
Expand Down
30 changes: 22 additions & 8 deletions lua/dap/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ local function launch_external_terminal(terminal, args)
end


function Session:run_in_terminal(request)
local function run_in_terminal(self, request)
local body = request.arguments
log.debug('run_in_terminal', body)
local settings = dap().defaults[self.config.type]
Expand Down Expand Up @@ -583,17 +583,31 @@ function Session:handle_body(body)
log.warn('No event handler for ', decoded)
end
end)
elseif decoded.type == 'request' and decoded.command == 'runInTerminal' then
self:run_in_terminal(decoded)
elseif decoded.type == 'request' then
local handler = self.handlers.reverse_requests[decoded.command]
if handler then
handler(self, decoded)
end
log.warn('No handler for reverse request', decoded)
else
log.warn('Received unexpected message', decoded)
end
end


local function session_defaults(opts)
local default_reverse_request_handlers = {
runInTerminal = run_in_terminal
}


local function session_defaults(adapter, opts)
local handlers = {}
handlers.after = opts.after
handlers.reverse_requests = vim.tbl_extend(
'error',
default_reverse_request_handlers,
adapter.reverse_request_handlers or {}
)
return {
handlers = handlers;
message_callbacks = {};
Expand All @@ -607,8 +621,8 @@ local function session_defaults(opts)
end


function Session:connect(host, port, opts, on_connect)
local session = session_defaults(opts or {})
function Session:connect(adapter, opts, on_connect)
local session = session_defaults(adapter, opts or {})
setmetatable(session, self)
self.__index = self

Expand All @@ -622,7 +636,7 @@ function Session:connect(host, port, opts, on_connect)
client:close()
end;
}
client:connect(host or '127.0.0.1', tonumber(port), function(err)
client:connect(adapter.host or '127.0.0.1', tonumber(adapter.port), function(err)
if not err then
client:read_start(rpc.create_read_loop(function(body)
session:handle_body(body)
Expand All @@ -635,7 +649,7 @@ end


function Session:spawn(adapter, opts)
local session = session_defaults(opts or {})
local session = session_defaults(adapter, opts or {})
setmetatable(session, self)
self.__index = self

Expand Down

0 comments on commit 65863f6

Please sign in to comment.