From 8ab07cd5d915f619c5c7182030672255fb48c656 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Sat, 1 Jun 2024 10:28:02 +0200 Subject: [PATCH] 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. --- lua/dap/entity.lua | 11 ++--- lua/dap/session.lua | 8 +--- lua/dap/ui/widgets.lua | 101 +++++++++++++++++++++++------------------ lua/dap/utils.lua | 4 ++ 4 files changed, 65 insertions(+), 59 deletions(-) diff --git a/lua/dap/entity.lua b/lua/dap/entity.lua index fe55dce8..272c4ee7 100644 --- a/lua/dap/entity.lua +++ b/lua/dap/entity.lua @@ -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 diff --git a/lua/dap/session.lua b/lua/dap/session.lua index b3fc2b8d..ce291ba5 100644 --- a/lua/dap/session.lua +++ b/lua/dap/session.lua @@ -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 diff --git a/lua/dap/ui/widgets.lua b/lua/dap/ui/widgets.lua index 4d8ec223..dc056c26 100644 --- a/lua/dap/ui/widgets.lua +++ b/lua/dap/ui/widgets.lua @@ -355,55 +355,66 @@ M.sessions = { } -M.expression = { - new_buf = new_buf, - before_open = function(view) - view.__expression = vim.fn.expand('') - 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('') + 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) diff --git a/lua/dap/utils.lua b/lua/dap/utils.lua index 92f49e8e..d692d2c8 100644 --- a/lua/dap/utils.lua +++ b/lua/dap/utils.lua @@ -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