Skip to content

fix(dap): keep the Variables tree expanded after "Set Value…" and stepping - #1937

Merged
lagergren merged 2 commits into
redhat-developer:mainfrom
muescha:fix/dap-keep-variables-tree-expanded
Nov 11, 2026
Merged

lagergren merged 2 commits into
redhat-developer:mainfrom
muescha:fix/dap-keep-variables-tree-expanded

Conversation

@muescha

@muescha muescha commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In a DAP debug session, the Variables view collapses each time it is rebuilt:

  • after "Set Value…": the view shows "Collecting data…", then item and every other expanded entry are collapsed again, and have to be expanded by hand to see the new value;
  • after each step (Step Over, Step Into, …) in the same function, in the same way.

The Java debugger keeps the expanded entries in both cases.

Cause

Claude Code traced it in the IntelliJ Platform sources:

  1. After a successful setVariable, DAPValueModifier calls callback.valueModified(). The platform's callback (DebuggerUIUtil, around line 648 in intellij-community) then calls rebuildAllSessionsViews(project), which rebuilds the Variables view from the current stack frame.
  2. XVariablesViewBase (buildTreeAndRestoreState, saveCurrentTreeState) saves and restores the expanded nodes and the selection per stack frame, keyed by XStackFrame.getEqualityObject(). It only does this when that object is non-null.
  3. XStackFrame.getEqualityObject() returns null by default, and DAPStackFrame doesn't override it. So the platform never saves or restores the tree state for a DAP frame, and every rebuild starts collapsed.

The JavaDoc of getEqualityObject() says the same for stepping: "If the stack frame is not changed after stepping in the debugger, the expanded nodes and the selection will be restored". So the same cause also collapses the tree after every step in a DAP session, not only after "Set Value…".

Reloading the values isn't the problem: variablesReference values change on each fetch anyway (the trace shows item as 7, 83, 159, 235, …). The platform restores the tree by node names (scope groups such as Locals, then variable names), so it can restore the state although the references change.

Change

DAPStackFrame (src/main/java/com/redhat/devtools/lsp4ij/dap/client/DAPStackFrame.java) overrides getEqualityObject() with a value that stays the same while the user is "in the same function", but changes for a different one: the frame's name and its source (the path, else ref:<sourceReference> for a sourceReference > 0, else the source name).

@Override
public @Nullable Object getEqualityObject() {
    return getEqualityObject(stackFrame);
}

static @NotNull Object getEqualityObject(@NotNull StackFrame stackFrame) {
    Source source = stackFrame.getSource();
    String sourceKey = null;
    if (source != null) {
        if (StringUtils.isNotBlank(source.getPath())) {
            sourceKey = source.getPath();
        } else if (source.getSourceReference() != null && source.getSourceReference() > 0) {
            sourceKey = "ref:" + source.getSourceReference();
        } else {
            sourceKey = source.getName();
        }
    }
    return List.of(Objects.toString(stackFrame.getName(), ""), Objects.toString(sourceKey, ""));
}

Not in the key:

  • StackFrame.id: it changes with every stop, so the state would never be restored.
  • the line: it changes with every step.

Other debuggers in the IntelliJ Platform do the same or less:

  • Java (JavaStackFrame) keys on the declaring type and the method signature (NodeManagerImpl.getContextKeyForFrame), without the line or the depth.
  • Python (PyStackFrame) uses one constant object for every frame.

So a recursive call of the same function at another depth shares the state, as in the Java debugger. That only decides which nodes are open; the values are always fetched again.

Requests: LSP4IJ already sends variables for every scope of the frame on each stop, expensive ones included (DAPStackFrame.computeChildren). This change doesn't touch that. What it adds is one variables request for each entry the user had expanded, here item, which the user would otherwise send by expanding it by hand.

Tests

DAPStackFrameEqualityTest (6 tests, all pass):

  • same name and source, different id and line → equal
  • other function name → different
  • other source path → different
  • same sourceReference → equal; other sourceReference → different
  • source with only a name → keyed by that name
  • no source → equal for the same name

Manual test in IntelliJ IDEA 2026.3 with the JSC debug adapter (jsc-debug) and swift/Tests/Fixtures/set-variable.js, stopped at line 24 with item expanded:

Before: item is collapsed; Locals shows only its top-level entries, followed by the groups Global (lexical), Global and Diagnostics. item has to be expanded again by hand.

Before, "Set Value…" (count → 42):

dap-variables-tree-before-set-value.mp4

Before, Step Over (line 24 → 25):

dap-variables-tree-before-step-over.mp4

After: the view shows "Collecting data…" for a moment, then item is expanded again, with count = 42.

After, "Set Value…":

dap-variables-tree-after-set-value.mp4

After, Step Over:

dap-variables-tree-after-step-over.mp4

🤖 Generated with Claude Code

…tepping

The Variables view saves and restores the expanded nodes and the selection
per stack frame, keyed by XStackFrame.getEqualityObject(), only when that
object is non-null. DAPStackFrame didn't override it, so every rebuild of
the view (after "Set Value...", after each step) started collapsed.

Use the function name and the source of the DAP frame. The frame id
changes on each stop and the line on each step, so neither is part of it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved debugger stack-frame matching across stops and steps. Frames are recognized by their function and source, so changes to frame IDs or line numbers no longer prevent matching. Frames with different functions or sources remain distinct, and frames without source information can still be matched.

DAPStackFrame now provides an equality object based on function name and source identity. Tests cover changes to frame IDs, lines, function names, and sources.

Changes: Stack Frame Equality

Layer / File(s) Summary
Define and test frame identity
src/main/java/com/redhat/devtools/lsp4ij/dap/client/DAPStackFrame.java, src/test/java/com/redhat/devtools/lsp4ij/dap/client/DAPStackFrameEqualityTest.java
getEqualityObject() uses the function name and a source key. The source key prefers a nonblank path, then a positive source reference, then the source name. Tests check equality for changed frame IDs or lines and differences for changed functions or sources.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: angelozerr

Merge Risk: Low · up to fd7fb

Expanded variables or selection may appear on the wrong recursive frame after stepping. The change remains mergeable with awareness of this edge case.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly summarizes the main user-visible change: preserving expanded Variables entries after value updates and stepping.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description explains the DAP Variables view state-restoration problem and the frame equality key added to address it.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8ae3dcd5-fd07-4eb0-bb5a-aa3369b43d95
Commits

Reviewing files that changed from the base of the PR and between 754254a and fd7fb7b.

Files selected for processing: 2

  • src/main/java/com/redhat/devtools/lsp4ij/dap/client/DAPStackFrame.java
  • src/test/java/com/redhat/devtools/lsp4ij/dap/client/DAPStackFrameEqualityTest.java

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 48.24%. Comparing base (754254a) to head (82ded9f).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...dhat/devtools/lsp4ij/dap/client/DAPStackFrame.java 90.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1937      +/-   ##
==========================================
+ Coverage   48.17%   48.24%   +0.06%     
==========================================
  Files         809      809              
  Lines       32435    32445      +10     
  Branches     6600     4998    -1602     
==========================================
+ Hits        15626    15652      +26     
+ Misses      14314    14306       -8     
+ Partials     2495     2487       -8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lagergren lagergren left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much, @muescha! Approved!

@lagergren lagergren added this to the 0.30.0 milestone Nov 11, 2026
@lagergren
lagergren merged commit 314b562 into redhat-developer:main Nov 11, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants