Skip to content

Commit

Permalink
Eagerly load lazy variables when expanding children
Browse files Browse the repository at this point in the history
As described in #1062
Some debug adapters heavily use the lazy hint.

For example, a collapsed list is initially displayed as:

    settings: ArrayList@50 size=1

That's good. But after expanding, the contained items looke like this:

    settings: ArrayList@50 size=1
      0: Assignment@66

Each individual item had to be expanded to show the value:

    settings: ArrayList@50 size=1
      0: Assignment@66 "Assignment{column='stats.jobs_log_size', expressions=[1024]}"

This removes the intermediate step, to go from:

    settings: ArrayList@50 size=1

Straight to:

    settings: ArrayList@50 size=1
      0: Assignment@66 "Assignment{column='stats.jobs_log_size', expressions=[1024]}"
  • Loading branch information
mfussenegger committed Oct 27, 2023
1 parent 3eb26a6 commit 79dbc70
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
58 changes: 36 additions & 22 deletions lua/dap/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,16 @@ function variable.get_children(var)
end


local function sort_vars(vars)
local sorted_variables = {}
for _, v in pairs(vars) do
table.insert(sorted_variables, v)
---@param a dap.Variable
---@param b dap.Variable
local function cmp_vars(a, b)
local num_a = string.match(a.name, '^%[?(%d+)%]?$')
local num_b = string.match(b.name, '^%[?(%d+)%]?$')
if num_a and num_b then
return tonumber(num_a) < tonumber(num_b)
else
return a.name < b.name
end
table.sort(
sorted_variables,
function(a, b)
local num_a = string.match(a.name, '^%[?(%d+)%]?$')
local num_b = string.match(b.name, '^%[?(%d+)%]?$')
if num_a and num_b then
return tonumber(num_a) < tonumber(num_b)
else
return a.name < b.name
end
end
)
return sorted_variables
end


Expand All @@ -88,15 +80,37 @@ function variable.fetch_children(var, cb)
if var.variables then
cb(variable.get_children(var))
elseif session and var.variablesReference then
local params = { variablesReference = var.variablesReference }
session:request('variables', params, function(err, resp)

---@param resp dap.VariableResponse
local function on_variables(err, resp)
if err then
utils.notify('Error fetching variables: ' .. err.message, vim.log.levels.ERROR)
else
var.variables = sort_vars(resp.variables)
cb(var.variables)
local variables = resp.variables
local unloaded = #variables
local function countdown()
unloaded = unloaded - 1
if unloaded == 0 then
var.variables = variables
cb(variables)
end
end

table.sort(variables, cmp_vars)
for i, v in ipairs(variables) do
if variable.is_lazy(v) then
variable.load_value(v, function(loaded_v)
variables[i] = loaded_v
countdown()
end)
else
countdown()
end
end
end
end)
end
local params = { variablesReference = var.variablesReference }
session:request('variables', params, on_variables)
else
cb({})
end
Expand Down
3 changes: 3 additions & 0 deletions lua/dap/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
---@field variables? table<string, dap.Variable> by variable name. Not part of spec


---@class dap.VariableResponse
---@field variables dap.Variable[]

---@class dap.Variable
---@field name string
---@field value string
Expand Down

0 comments on commit 79dbc70

Please sign in to comment.