Skip to content

Cache the JSON tree per tab instead of drawing it on tab switch - #4

Draft
leoshone wants to merge 7 commits into
masterfrom
fix/per-tab-tree-snapshot
Draft

Cache the JSON tree per tab instead of drawing it on tab switch#4
leoshone wants to merge 7 commits into
masterfrom
fix/per-tab-tree-snapshot

Conversation

@leoshone

@leoshone leoshone commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Draft PR to trigger CI on the fork (upstream CI only reacts to pull_request events).

@leoshone
leoshone force-pushed the fix/per-tab-tree-snapshot branch from b5f684c to ed4c3d8 Compare September 5, 2026 04:15
leoshone and others added 7 commits September 5, 2026 19:54
…iewer#251)

* Persist the tree view zoom level across Notepad++ sessions

The plugin already ships a zoom slider for the JSON tree (80%..250%),
but the chosen level only lived in the slider control: closing
Notepad++ and starting it again always fell back to 100%.

Store the zoom percentage in JSONViewer.ini under [Others] TREE_ZOOM
and re-apply it when the dialog is initialised.

The value is written only when it actually changes, and while the
slider thumb is being dragged (TB_THUMBTRACK) nothing is written, so
a drag gesture produces a single write at the end instead of one per
pixel.

Purely additive: the existing ini keys and the default behaviour are
untouched.

* Fix the drag detection of the zoom slider

WM_HSCROLL carries the notification code in LOWORD(wParam), not HIWORD: HIWORD holds the thumb position itself (80..250 here), so comparing it against TB_THUMBTRACK never matched and the zoom was written to the ini file continuously while the thumb was being dragged, instead of once when the gesture ended.

Found by an independent review of the integration branch; the end-to-end harness never caught it because it only sends TB_ENDTRACK and never simulates the dragging itself.

---------

Co-authored-by: leoshone <leoshone@users.noreply.github.com>
1. Set json viewer setting on exit that too if it is changed.
2. Header order and minor correction
"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.
When "Follow current tab" is off (the default) the plugin never drew the
tree on its own, but it also never cleared it: the tree kept showing the
document of some earlier tab, with no indication that it belonged there.

With this change the tree is only ever drawn when the user asks for it
("Refresh JSON Tree"). Switching tabs stores the tree of the tab being
left and puts it back verbatim when the tab is activated again, so no
re-parsing happens and the expansion state and selection survive.

The "Follow current tab" option is kept and behaves exactly as before
when enabled: the document of the activated tab is parsed immediately.
Only its "off" path changes, from "do nothing" to "remember per tab".

Notes:
 - Snapshots live in memory only and are dropped when the buffer is
   closed, together with the association to the current buffer.
 - "Auto format on open" now formats the document without drawing the
   tree, so opening a file still cannot trigger a parse.
 - Formatting now redraws the tree while preserving its expansion state,
   which keeps it consistent with Refresh.
 - Built on top of the TreeExpansion helpers introduced for Refresh.
Adds an option, off by default, that draws the tree of a json document as soon as the file is opened. It complements the per-tab snapshot caching: the document is parsed exactly once, and switching back to the tab afterwards replays the stored snapshot instead of parsing again.

The check lives in RestoreTabState(), the single place reached when the tree of a tab has never been drawn, so opening a file, switching back to a tab and showing the panel are all covered by one code path.

Drawing on open is initiated by the plugin, not by the user, so parse errors are reported as a node inside the tree rather than through a modal dialog: DrawJsonTree() takes a bSilent flag for that.

The tree is drawn for documents whose language is JSON, the same criterion the existing "follow current tab" uses.
@leoshone
leoshone force-pushed the fix/per-tab-tree-snapshot branch from ed4c3d8 to cdda86c Compare September 7, 2026 10:20
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