Skip to content

Commit

Permalink
Don't convert scope variables to dict with name as key
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mfussenegger committed Jun 1, 2024
1 parent 936ade9 commit 8ab07cd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 59 deletions.
11 changes: 3 additions & 8 deletions lua/dap/entity.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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


Expand Down
8 changes: 2 additions & 6 deletions lua/dap/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
101 changes: 56 additions & 45 deletions lua/dap/ui/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
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]
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
break
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
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)
end,
}
end


function M.builder(widget)
Expand Down
4 changes: 4 additions & 0 deletions lua/dap/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ab07cd

Please sign in to comment.