Skip to content

Node JSON and versions

Two small, pure helpers for plugins and tooling. Both live in src/node-json.ts and src/node-versions.ts (the programmatic API barrel src/api.ts was removed; import them from there) (src/api.ts).

JSON serialization

serializeNode(node, { ids?, indent? }) writes a subtree as JSON: a node is { "head", "children" }, a primitive is the bare JSON value. Ids are left out unless ids: true. deserializeNode(text) reads it back with fresh ids, so the result is made of proper concept instances and can't collide with ids already in a tree. A malformed shape throws an error that names the path ($.children[0]: missing string "head"). nodeToJson / nodeFromJson are the same without the text step.

Node versions

snapshotNode(node, label) freezes a subtree's content as { label, hash, created, tree }. The hash ignores ids. compareToVersion(current, version) reports { unchanged, changes, counts }, where each change is { kind: "added" | "removed" | "changed", path, head } and path is the child-index path from the versioned node. It never modifies either side. The matcher is the one the git AST diff uses, so a lightly edited statement reports as changed, not as a remove plus an add.

Storage is up to the caller: a NodeVersionStore (addVersion, serializeVersionStore, parseVersionStore) is plain JSON meant for a sidecar file next to the model, keyed by any stable subject address, such as a root's qualified name. Labels are unique per subject. parseVersionStore treats malformed input as an empty store.

Commands

Three commands (palette and Tools > Versions) drive this for the root shown in the editor (they do not modify the model):

  • Save Version of Active Root… asks for a label (default v1, v2, …) and stores a snapshot. A label must be unique per root.
  • Compare Active Root With Version… asks which saved version to compare against (newest first), then reports "N added, M removed, K changed" and lists each change in the Messages panel with its child-index path.
  • Browse Versions of Active Root… opens a dialog (VersionBrowserModal.tsx) listing the versions newest first with date, node count and a "matches current" marker; selecting one shows the same comparison, Delete removes it. The dialog is a view over createVersionBrowser (list/compare/remove), which re-reads the store on every call.

The store is a versions.json sidecar in the model's folder when the host has a file system (Theia/Electron), and browser local storage (keyed by project and model) otherwise (src/node-versions-store.ts). A root is addressed by its file name within the model. The flows themselves (src/node-versions-commands.ts) take their dialogs, storage and reporters as arguments, so other hosts can reuse them.