Skip to content

Commit

Permalink
fix(variables): handle changing child var ref
Browse files Browse the repository at this point in the history
Variable references can change at any time. If a variable was hidden and
re-opened with one of its child var refs changed which was previously
opened, the render would always be invalidated due to state not
containing the variables. This change makes sure that the expanded
variables are always monitored.

See #47
  • Loading branch information
rcarriga committed Aug 15, 2021
1 parent 90a4025 commit c9fc568
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lua/dapui/components/variables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ function Variables:render(render_state, parent_ref, variables, indent)
local child_vars = self.state:variables(variable.variablesReference)
if not child_vars then
render_state:invalidate()
-- Happens when the parent component is collapsed and the variable
-- reference changes when re-opened. The name is recorded as opened
-- but the variable reference is not yet monitored.
if not self.state:is_monitored(variable.variablesReference) then
self.state:monitor(variable.variablesReference)
end
return
else
self:_get_child_component(variable.name):render(
Expand Down
1 change: 0 additions & 1 deletion lua/dapui/render/loop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ function M.run(element_names)
end
end
end
else
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lua/dapui/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ function UIState:monitor(var_ref)
end)
end

function UIState:is_monitored(var_ref)
return self._monitored_vars[var_ref] ~= nil
end

function UIState:stop_monitor(var_ref)
self._monitored_vars[var_ref] = (self._monitored_vars[var_ref] or 1) - 1
if self._monitored_vars[var_ref] then
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/components/variables_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe("checking variables", function()
monitor = function(_, ref)
monitored[ref] = true
end,
is_monitored = function(_, ref)
return monitored[ref] ~= nil
end,
stop_monitor = function(_, ref)
monitored[ref] = nil
end,
Expand Down Expand Up @@ -182,6 +185,22 @@ describe("checking variables", function()
local render_state = render.new_state()
local component = Variables(mock_state)

local vars = mock_state:variables(1)
component:render(render_state, 1, vars)
render_state.mappings["expand"][1][1]()
render_state = render.new_state()
mock_state.variables = function()
return nil
end
vars[1].variablesReference = 10
component:render(render_state, 1, vars)
assert.True(monitored[10])
end)

it("monitors child var when variable reference is changed", function()
local render_state = render.new_state()
local component = Variables(mock_state)

local vars = mock_state:variables(1)
component:render(render_state, 1, vars)
render_state.mappings["expand"][1][1]()
Expand Down

0 comments on commit c9fc568

Please sign in to comment.