Skip to content

Commit

Permalink
Allow to pass evaluate context to repl.execute
Browse files Browse the repository at this point in the history
Debug adapters like `gdb` treat input in the REPL as commands, which is
usually what you want if interacting with the debug adapter from the
REPL, but `repl.execute(expression)` is often used to show the result of
an expression in the REPL.

This adds an `opts` parameter with a `context` field to allow using
something like `dap.repl.execute(vim.fn.expand("<cexpr>"), { context =
"hover" })` in a keymap to evaluate the input as a variable/expression
instead of a command
  • Loading branch information
mfussenegger committed Jan 26, 2025
1 parent 1fdfe74 commit cb28949
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lua/dap/repl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,11 @@ local function trystart(confname)
end


local function coexecute(text)
---@param text string
---@param opts? dap.repl.execute.Opts
local function coexecute(text, opts)
assert(coroutine.running() ~= nil, "Must run in coroutine")
opts = opts or {}

local session = get_session()
if not session then
Expand Down Expand Up @@ -322,12 +325,23 @@ local function coexecute(text)
local args = string.sub(text, string.len(command)+2)
M.commands.custom_commands[command](args)
else
session:evaluate(text, evaluate_handler)
---@type dap.EvaluateArguments
local params = {
expression = text,
context = opts.context
}
session:evaluate(params, evaluate_handler)
end
end


function execute(text)
---@class dap.repl.execute.Opts
---@field context? "watch"|"repl"|"hover"|"variables"|"clipboard"


---@param text string
---@param opts? dap.repl.execute.Opts
function execute(text, opts)
if text == '' then
if history.last then
text = history.last
Expand Down Expand Up @@ -359,14 +373,16 @@ function execute(text)
end
else
require("dap.async").run(function()
coexecute(text)
coexecute(text, opts)
end)
end
end


--- Add and execute text as if entered directly
function M.execute(text)
---@param text string
---@param opts? dap.repl.execute.Opts
function M.execute(text, opts)
M.append(prompt .. text .. "\n", "$", { newline = true })
local numlines = api.nvim_buf_line_count(repl.buf)
if repl.win and api.nvim_win_is_valid(repl.win) then
Expand All @@ -375,7 +391,7 @@ function M.execute(text)
vim.cmd.normal({"zt", bang = true })
end)
end
execute(text)
execute(text, opts)
end


Expand Down

0 comments on commit cb28949

Please sign in to comment.