Don't convert scope variables to dict with name as key

That was only used for a fallback case in the expression widget.
It's cheaper to do it there if needed instead of doing it all the time.
This commit is contained in:
Mathias Fussenegger 2024-06-01 10:28:02 +02:00 committed by Mathias Fußenegger
parent 0a5c5093e4
commit 1f609cbc48
4 changed files with 67 additions and 61 deletions

View file

@ -1,9 +1,6 @@
local utils = require('dap.utils')
local M = {}
---@diagnostic disable-next-line: deprecated
local islist = vim.islist or vim.tbl_islist
local variable = {}
M.variable = variable
@ -18,6 +15,7 @@ local syntax_mapping = {
}
---@param var dap.Variable|dap.EvaluateResponse
function variable.get_key(var)
return var.name or var.result
end
@ -28,6 +26,7 @@ function variable.is_lazy(var)
end
---@param var dap.Variable|dap.EvaluateResponse
function variable.render_parent(var)
if var.name then
return variable.render_child(var, 0)
@ -59,11 +58,7 @@ end
---@param var dap.Variable
---@result dap.Variable[]
function variable.get_children(var)
if islist(var.variables) then
return var.variables
else
return var.variables and vim.tbl_values(var.variables) or {}
end
return var.variables or {}
end

View file

@ -770,18 +770,14 @@ function Session:_request_scopes(current_frame)
end
local scopes = scope_resp.scopes
current_frame.scopes = scopes
local function toname(var)
return var.name
end
for _, scope in ipairs(scopes) do
if not scope.expensive then
---@param resp dap.VariableResponse?
local function on_variables(_, resp)
if resp then
scope.variables = utils.to_dict(resp.variables, toname)
end
scope.variables = resp and resp.variables or nil
end
local varparams = { variablesReference = scope.variablesReference }
self:request('variables', varparams, on_variables)
end

View file

@ -355,55 +355,66 @@ M.sessions = {
}
M.expression = {
new_buf = new_buf,
before_open = function(view)
view.__expression = vim.fn.expand('<cexpr>')
end,
render = function(view, expr)
local session = require('dap').session()
local layer = view.layer()
if not session then
layer.render({'No active session'})
return
end
local expression = expr or view.__expression
local context = session.capabilities.supportsEvaluateForHovers and "hover" or "repl"
local args = {
expression = expression,
context = context
}
local frame = session.current_frame or {}
local variable
local scopes = frame.scopes or {}
session:evaluate(args, function(err, resp)
if err then
for _, s in pairs(scopes) do
variable = s.variables and s.variables[expression]
if variable then
break
end
end
if variable then
local tree = ui.new_tree(require('dap.entity').variable.tree_spec)
tree.render(view.layer(), variable)
else
local msg = 'Cannot evaluate "'..expression..'"!'
layer.render({msg})
end
elseif resp and resp.result then
local attributes = (resp.presentationHint or {}).attributes or {}
if resp.variablesReference > 0 or vim.tbl_contains(attributes, "rawString") then
local tree = ui.new_tree(require('dap.entity').variable.tree_spec)
tree.render(layer, resp)
else
local lines = vim.split(resp.result, "\n", { plain = true })
layer.render(lines)
do
---@param scopes dap.Scope[]
---@param expression string
---@return dap.Variable?
local function find_var(scopes, expression)
for _, s in ipairs(scopes) do
for _, var in ipairs(s.variables or {}) do
if var.name == expression then
return var
end
end
end)
end,
}
end
return nil
end
M.expression = {
new_buf = new_buf,
before_open = function(view)
view.__expression = vim.fn.expand('<cexpr>')
end,
render = function(view, expr)
local session = require('dap').session()
local layer = view.layer()
if not session then
layer.render({'No active session'})
return
end
local expression = expr or view.__expression
local context = session.capabilities.supportsEvaluateForHovers and "hover" or "repl"
local args = {
expression = expression,
context = context
}
local frame = session.current_frame or {}
local scopes = frame.scopes or {}
session:evaluate(args, function(err, resp)
if err then
local variable = find_var(scopes, expression)
if variable then
local tree = ui.new_tree(require('dap.entity').variable.tree_spec)
tree.render(view.layer(), variable)
else
local msg = 'Cannot evaluate "'..expression..'"!'
layer.render({msg})
end
elseif resp and resp.result then
local attributes = (resp.presentationHint or {}).attributes or {}
if resp.variablesReference > 0 or vim.tbl_contains(attributes, "rawString") then
local tree = ui.new_tree(require('dap.entity').variable.tree_spec)
tree.render(layer, resp)
else
local lines = vim.split(resp.result, "\n", { plain = true })
layer.render(lines)
end
end
end)
end,
}
end
function M.builder(widget)

View file

@ -20,7 +20,11 @@ end
-- `get_key` is used to get the key from an element of values
-- `get_value` is used to set the value from an element of values and
-- defaults to the full element
---@deprecated
function M.to_dict(values, get_key, get_value)
if vim.notify_once then
vim.notify_once("dap.utils.to_dict is deprecated for removal in nvim-dap 0.10.0")
end
local rtn = {}
get_value = get_value or function(v) return v end
for _, v in pairs(values or {}) do