Skip to content

Commit

Permalink
Add docs for dap-session and request method
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Nov 8, 2023
1 parent 4048f37 commit 4b5c612
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
66 changes: 66 additions & 0 deletions doc/dap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ session() *dap.session()*
Returns the currently focused session or nil if no session exists or
has focus.

See |dap-session| for a description of a session object.

sessions() *dap.sessions()*
Returns a table with the active top-level debug sessions.
The keys are session ids and the values are the `Session` instances.
Expand Down Expand Up @@ -1234,5 +1236,69 @@ pick_process({opts}) *dap.utils.pick_process*
filter = function(proc) return vim.endswith(proc.name, "sway") end
})
<
==============================================================================
DAP Session *dap-session*

nvim-dap creates a session object per debug session. You can either get the current
session via |dap.session()|, or get access to one via a listener (|dap-extensions|).

The session object is a low level primitive, used to interact with the debug
session. This is not indented for regular debug-client users, but rather for
extension authors, or power users.

The session contains methods - functions where the first parameter is the
session itself. To call them, you can use the method call syntax:
`obj:function_name()`, instead of `obj.function_name(obj)`.
See |lua-function|.


request({command}, {arguments}, {callback}) *dap-session:request()*

Send a request to the debug adapter.
See the requests section in https://microsoft.github.io/debug-adapter-protocol/specification
for a list of supported payloads.

Parameters:
{command} string The command to execute
{arguments} any|nil Arguments
{callback} function Callback called with two parameters: err and result.

For example, to make a `evaluate` request, you'd use:

>lua
local session = assert(require("dap").session(), "has active session")
local arguments = {
expression = "1 + 2"
}
session:request("evaluate", arguments, function(err, result)
vim.print(err or "No error")
vim.print(result or "No result")
end)
<

The method is coroutine aware. If it is running inside a coroutine you can
omit the callback and it will default to resume the coroutine with the
result.

An example:

>lua
local session = assert(require("dap").session(), "has active session")
local arguments = {
expression = "1 + 2"
}
coroutine.wrap(function()
local err, result = session:request("evaluate", arguments)
vim.print(err or "No error")
vim.print(result or "No result")
end)()
<

This is convenient if you want to make multiple requests as it helps
prevent callback nesting.

Note that `coroutine.wrap` doesn't propagate errors but you could setup
error handling via |xpcall()|


vim:ft=help
3 changes: 1 addition & 2 deletions lua/dap/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1664,8 +1664,7 @@ end
--- Send a request to the debug adapter
---@param command string command to execute
---@param arguments any|nil object containing arguments for the command
---@param callback fun(err: table, result: any)|nil
-- callback called with the response result.
---@param callback fun(err: table, result: any)|nil called with the response result.
--- If nil and running within a coroutine the function will yield the result
function Session:request(command, arguments, callback)
local payload = {
Expand Down

0 comments on commit 4b5c612

Please sign in to comment.