From 936ade9e46ceaea280e5e35dd6fc0c9340df94be Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Sat, 1 Jun 2024 10:11:45 +0200 Subject: [PATCH] Use scopes from response without copy --- lua/dap/ext/vscode.lua | 2 +- lua/dap/protocol.lua | 6 ++++++ lua/dap/session.lua | 37 ++++++++++++++++++++++--------------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/lua/dap/ext/vscode.lua b/lua/dap/ext/vscode.lua index 5fb120f9..d256352b 100644 --- a/lua/dap/ext/vscode.lua +++ b/lua/dap/ext/vscode.lua @@ -175,7 +175,7 @@ function M._load_json(jsonstr) end ---@param path string? ----@return Configuration[] +---@return dap.Configuration[] function M.getconfigs(path) local resolved_path = path or (vim.fn.getcwd() .. '/.vscode/launch.json') if not vim.loop.fs_stat(resolved_path) then diff --git a/lua/dap/protocol.lua b/lua/dap/protocol.lua index 88b792f8..5668ab70 100644 --- a/lua/dap/protocol.lua +++ b/lua/dap/protocol.lua @@ -65,6 +65,7 @@ ---@field stackFrames dap.StackFrame[] ---@field totalFrames? number + ---@class dap.Scope ---@field name string ---@field presentationHint? "arguments"|"locals"|"registers"|string @@ -79,6 +80,11 @@ ---@field endColumn? number ---@field variables? table by variable name. Not part of spec + +---@class dap.ScopesResponse +---@field scopes dap.Scope[] + + ---@class dap.ValueFormat ---@field hex? boolean Display the value in hex diff --git a/lua/dap/session.lua b/lua/dap/session.lua index 62a7f077..b3fc2b8d 100644 --- a/lua/dap/session.lua +++ b/lua/dap/session.lua @@ -760,27 +760,34 @@ end ---@param current_frame dap.StackFrame function Session:_request_scopes(current_frame) - self:request('scopes', { frameId = current_frame.id }, function(_, scopes_resp) - if not scopes_resp or not scopes_resp.scopes then + local params = { + frameId = current_frame.id + } + ---@param scope_resp dap.ScopesResponse? + local function on_scopes(_, scope_resp) + if not scope_resp then return end - current_frame.scopes = {} - for _, scope in pairs(scopes_resp.scopes) do - table.insert(current_frame.scopes, scope) + 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 - local params = { variablesReference = scope.variablesReference } - self:request('variables', params, function(_, variables_resp) - if not variables_resp then - return + + ---@param resp dap.VariableResponse? + local function on_variables(_, resp) + if resp then + scope.variables = utils.to_dict(resp.variables, toname) end - scope.variables = utils.to_dict( - variables_resp.variables, - function(v) return v.name end - ) - end) + end + local varparams = { variablesReference = scope.variablesReference } + self:request('variables', varparams, on_variables) end end - end) + end + self:request('scopes', params, on_scopes) end