Repository navigation
👌 Copy edited tokens in SyntaxTreeNode.to_tokens instead of writing in place - #464
Merged
Merged
Conversation
…n place #453 made to_tokens() keep edits to the children of inline and image nodes by assigning the rebuilt child list to the shared Token objects. That also changed the token stream the tree was built from, and a list returned earlier could change again after another tree built from the same tokens called to_tokens(). A token is now replaced by token.copy(children=...) only when its rebuilt children differ by identity from its current ones. Edits still reach the output, the original tokens are left unchanged, and an unedited round trip still returns the same Token objects and child lists. A copied image token makes the enclosing inline token's children differ, so it is copied too. The test that pinned the in-place write is replaced by tests for each of these properties, and the docstring and docs/using.md no longer need the shared-state caveats or the deepcopy advice.
…heck The docstring and docs/using.md now say that the tree's children are the source of truth, so changes made directly to a token's children list do not appear in the output, and that to_tokens() itself does not modify the token stream, although nodes share their Token objects with it. The edit check uses all(map(operator.is_, ...)), which halves its overhead on an unedited round trip. A new test replaces a child with an equal but distinct token, which fails if the identity check becomes an equality check, and another pins that direct changes to a token's children do not reach the output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This follows up #453 by @KennyMcSimpson, which fixed
SyntaxTreeNode.to_tokens()silently dropping edits to the children of inline and image nodes. The fix is kept; this changes how it writes the edits back, before the change is released.As merged,
to_tokens()assigns the rebuilt child list to the Token objects the tree shares with the stream it was built from. That has two side effects. The original token stream changes too, which it did not before #453. And a list already returned can change later: if two trees are built from the same tokens and one is edited, the edited tree's output reverts once the other tree callsto_tokens(), because the last call wins. The docstring anddocs/using.mdneeded several sentences and acopy.deepcopyrecipe to explain this.With this change, a token is replaced by
token.copy(children=...)only when its rebuilt children differ by identity from its current ones. Edits still appear in the output, the original tokens are left unchanged, and a returned list stays stable. An unedited round trip still returns the same Token objects, and now the same child lists as well. Copies propagate upward, so an edited image inside an inline token gives a new image token and a new inline token. The copy is shallow, sometaand a non-emptyattrsare still shared, as they are for every token the tree reuses.The test that pinned the in-place write is replaced by tests for each of these properties: edits reach the output, the original stream is unchanged, a returned list is stable after another tree's
to_tokens(), an unedited round trip reuses the same objects, and a nested image edit propagates. All four new tests fail against the current in-place code, and the identity test fails if the copy is made unconditionally. Two more tests pin that edits are detected by identity rather than equality (a child replaced by an equal but distinct token reaches the output), and that the tree'schildrenare the source of truth: a change made directly to a token'schildrenlist does not appear in the output. The docstring and thedocs/using.mdparagraph drop the shared-state caveats and say instead that the tree'schildrenwin, and thatto_tokens()does not modify the token stream, although nodes share theirTokenobjects with it, so setting a field such asnode.token.contentchanges both.The full test suite and pre-commit pass. MyST-Parser master's test suite (at
e0a12c3) also passes against this branch (1345 passed, 11 skipped). Itscompute_unique_slugis the one known caller that relies onto_tokens(), and it only reads the heading's inlinechildrencontent.