Skip to content

Keep the tree expansion state when refreshing the JSON tree - #252

Open
leoshone wants to merge 1 commit into
NPP-JSONViewer:masterfrom
leoshone:fix/keep-expansion-on-refresh
Open

Keep the tree expansion state when refreshing the JSON tree#252
leoshone wants to merge 1 commit into
NPP-JSONViewer:masterfrom
leoshone:fix/keep-expansion-on-refresh

Conversation

@leoshone

@leoshone leoshone commented Sep 3, 2026

Copy link
Copy Markdown

Problem

"Refresh JSON Tree" rebuilds every node, so the tree always comes back fully
collapsed. Re-reading a document you were already looking at means rebuilding
the expansion by hand every time, which makes the button impractical on a
larger file.

Steps to reproduce

  1. Open a .json file with nested objects and show the JSON Viewer panel.
  2. Expand a few nodes, select one, scroll to where you are working.
  3. Click "Refresh JSON Tree".
  4. Everything is collapsed again and the selection is gone.

Fix

Capture which nodes are expanded and which one is selected before the tree is
rebuilt, then re-apply that state onto the new tree, matching nodes by path.

  • A path is the list of keys from the tree root down to a node
    (nested.deep.flag), so it survives the tree being rebuilt from scratch.
  • Paths that no longer exist (the document was edited and dropped them) are
    silently discarded, and nodes that are new simply stay collapsed.
  • The pure path arithmetic lives in the new TreeExpansion.h / .cpp so that
    it can be unit tested without a window.

Notes

  • DrawJsonTree() gained a bPreserveExpansion parameter that defaults to
    false
    . Every other caller (panel opening, format, compress, sort) keeps
    behaving exactly as before; only the refresh button opts in.
  • No resource, ini or default behaviour change.
  • Independent of Persist the tree view zoom level across Notepad++ sessions #251, and both are based on current master. They touch nearby
    code in JsonViewDlg, so whichever merges second may need a trivial rebase.

Tests

tests/UnitTest/TreeExpansionTest.cpp - 14 cases over the three pure helpers:
path splitting/joining, and which paths must be re-expanded versus dropped.

Verified by hand on a portable Notepad++: expanding two levels and selecting a
node survives a refresh byte-for-byte, and - importantly - collapsing a node
and refreshing leaves it collapsed, so this is not simply "expand everything".

"Refresh JSON Tree" rebuilds every node, so the tree always came back
fully collapsed - even when the user only wanted to re-read a document
they were already looking at.

Capture which nodes are expanded and which one is selected before the
tree is thrown away, then re-apply that state onto the freshly built
tree, matching nodes by path. Paths that no longer exist (the document
changed in the meantime) are silently dropped, and nodes that are new
stay collapsed.

The state is keyed by node path, which is the list of keys from the
tree root down to a node. The pure path arithmetic lives in the new
TreeExpansion.h/.cpp so it can be unit tested without a window.

DrawJsonTree() gained a bPreserveExpansion parameter that defaults to
false, so every other caller (panel opening, formatting, compressing,
sorting) keeps behaving exactly as before; only the refresh button
opts in.
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.

1 participant