simvx.core.scene_io.edits¶
Prefix-preserving editing primitives for parso trees.
parso stores leading whitespace and comments on each leaf’s prefix string;
the prefix of a non-leaf node is the prefix of its first leaf. Edits that
splice nodes in or out of the tree must transfer prefixes carefully so that
trailing comments stay glued to the right line, blank-line spacing does not
drift, and indent depth is preserved.
The primitives here operate directly on the parso tree (mutating
parent.children lists). They are deliberately small — composition lives
in higher tiers (scene_file, scene_module).
Module Contents¶
Functions¶
Replace |
|
Insert |
|
Insert |
|
Remove |
|
Return the value subtree for kwarg |
|
Set kwarg |
API¶
- simvx.core.scene_io.edits.replace_node(old: parso.tree.NodeOrLeaf, new: parso.tree.NodeOrLeaf, *, preserve_prefix: bool = True) None[source]¶
Replace
oldwithnewin their shared parent’schildrenlist.With
preserve_prefix=True(default), the leading whitespace + comments ofold’s first leaf are transferred ontonew’s first leaf so that same-line trailing comments on the previous statement, leading blank lines, and decorators above remain attached.
- simvx.core.scene_io.edits.insert_after(sibling: parso.tree.NodeOrLeaf, new_node: parso.tree.NodeOrLeaf, *, copy_indent: bool = True) None[source]¶
Insert
new_nodeimmediately aftersiblingin their shared parent.With
copy_indent=True(default), the indent run ofsibling’s first leaf prefix is copied ontonew_nodeso the new statement sits at the same column.new_node’s prefix is overwritten with"\n<indent>"— callers wanting custom prefixes should passcopy_indent=Falseand populate the prefix themselves.
- simvx.core.scene_io.edits.insert_before(sibling: parso.tree.NodeOrLeaf, new_node: parso.tree.NodeOrLeaf, *, copy_indent: bool = True) None[source]¶
Insert
new_nodeimmediately beforesiblingin their shared parent.With
copy_indent=True(default),new_nodeinheritssibling’s full prefix (so any leading comments/blank lines stay above the inserted node) andsibling’s prefix is reset to"\n<indent>"so it sits at the same column it did originally.Note: this transfers comments above
siblingto the inserted node. To keep them attached tosibling, passcopy_indent=Falseand manage prefixes manually.
- simvx.core.scene_io.edits.remove_node(node: parso.tree.NodeOrLeaf, *, collapse_blank_lines: bool = True) None[source]¶
Remove
nodefrom its parent’schildrenlist.With
collapse_blank_lines=True(default), surplus blank lines innode’s prefix are collapsed onto the next sibling so deleting statements in sequence does not balloon vertical spacing. The collapse rule is: keep at most one blank line of separation; the indent run on the final line is preserved verbatim.Same-line trailing comments stored in
node’s prefix (which originate on the previous sibling — see module docstring) are re-attached to the next sibling so they stay on their original line.
- simvx.core.scene_io.edits.get_call_kwarg(call_node: parso.tree.NodeOrLeaf, name: str) parso.tree.NodeOrLeaf | None[source]¶
Return the value subtree for kwarg
namein a call, orNone.call_nodemay be either thetrailer(the(...)after a name) or the enclosingatom_expr— both forms are accepted.
- simvx.core.scene_io.edits.set_call_kwarg(call_node: parso.tree.NodeOrLeaf, name: str, value_expr: str) None[source]¶
Set kwarg
nameon a call expression.Overwrites if
namealready exists (preserves the order of other args); appends if not.value_expris parsed with :func:parse_snippet, so callers pass real Python source (e.g."Vec2(0, 0)"or'"hello"').A trailing comma in the original
arglistis preserved when appending.