Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
dae8e5b
fix(ast): keep edits queued on the children a replacement hands back
Princesseuh Oct 29, 2026
1d1c49c
fix(ast): keep reporting inserts lost with a dropped child position
Princesseuh Oct 29, 2026
cbc40c0
fix(ast): keep handed-back and moved-out children in place and report…
Princesseuh Nov 3, 2026
5e64f61
fix(visitor): keep children relisted after a removal or handed back b…
Princesseuh Nov 3, 2026
f0c543f
fix(visitor): keep children an async visitor removes and lists again
Princesseuh Nov 6, 2026
589bc98
perf(visitor): only look for relisted children when a visitor returns…
Princesseuh Nov 6, 2026
d79cc0a
fix(visitor): keep a removal unless a replacement deliberately lists …
Princesseuh Nov 6, 2026
84fca11
chore(changeset): note the removal and relist fixes
Princesseuh Nov 6, 2026
672a6ba
fix(visitor): keep a removal when the old children are passed on in o…
Princesseuh Nov 6, 2026
700050c
fix(ast): let a later removal win over a plain ref and hand back chil…
Princesseuh Nov 6, 2026
6bba38f
chore(changeset): mention unwrapping
Princesseuh Nov 6, 2026
6632b47
fix(visitor): count an async visitor's removals as of its call and ca…
Princesseuh Nov 6, 2026
6fb08ce
perf(visitor): check for async visitors once per walk
Princesseuh Nov 6, 2026
5ffad4a
chore(changeset): edits beside a moved node now come along
Princesseuh Nov 6, 2026
d2a5edd
fix(visitor): scope removals for promise visitors and carry relisted …
Princesseuh Nov 6, 2026
7378686
chore(changeset): note that a removal wins over a plain ref in satter…
Princesseuh Nov 6, 2026
da17c39
fix(visitor): make removeNode final and warn when a plugin lists a re…
Princesseuh Nov 7, 2026
fae5726
docs(plugin-api): note that a removed node stays removed
Princesseuh Nov 7, 2026
ecdd327
chore(changeset): describe removal as final
Princesseuh Nov 7, 2026
7e5f4f3
perf(visitor): skip the removal check when a pass removed nothing
Princesseuh Nov 7, 2026
e72b6f0
fix(ast): let a removal beat a replacement and move a node inserted b…
Princesseuh Nov 7, 2026
46d4971
feat(plugin-api): decode a sequence of raw and op-stream items as one…
Princesseuh Nov 7, 2026
876263d
fix(visitor): replace with raw items in one command and warn once per…
Princesseuh Nov 7, 2026
b2ac95d
docs(plugin-api): spell out when a removed node stays removed
Princesseuh Nov 7, 2026
47a632e
chore(changeset): give each package one removal changeset
Princesseuh Nov 7, 2026
6bb91e8
test(visitor): share the paragraph predicate between removal tests
Princesseuh Nov 7, 2026
79d7110
docs(plugin-api): tighten the removed-node note
Princesseuh Nov 7, 2026
b06537c
perf(visitor): check returned results for listed removals outside the…
Princesseuh Nov 7, 2026
9377953
fix(ast): let an insert move the nodes it names even beside a removed…
Princesseuh Nov 7, 2026
4c1f049
fix(visitor): stop checking inserts beside a removed node for listed …
Princesseuh Nov 7, 2026
1581e12
docs(plugin-api): state the removal and insert rules without exceptions
Princesseuh Nov 7, 2026
4ad770a
fix(ast): move a ref's target instead of copying it and keep a remova…
Princesseuh Nov 7, 2026
0524b09
fix(plugin-api): apply a field edit to the node that replaced its target
Princesseuh Nov 7, 2026
6d79a62
fix(visitor): move named nodes and reject self-inserts, tree wrappers…
Princesseuh Nov 7, 2026
835f039
docs(plugin-api): describe the one-place and final-removal rules
Princesseuh Nov 7, 2026
8147282
chore(changeset): describe moves, final removal and field edits after…
Princesseuh Nov 7, 2026
99a8a0c
fix(ast): keep a node named in its own replacement and apply edits to…
Princesseuh Nov 7, 2026
ca19cce
fix(plugin-api): land edits on a replaced node whichever came first
Princesseuh Nov 7, 2026
58d9480
perf(visitor): check each run of children once when warning about rem…
Princesseuh Nov 7, 2026
e67538f
test(visitor): cover edits on replaced, self-wrapped and removed nodes
Princesseuh Nov 7, 2026
28398ad
docs(plugin-api): describe edits that follow a replacement and nested…
Princesseuh Nov 7, 2026
e06c879
fix(ast): name the nodes involved in placement errors
Princesseuh Nov 7, 2026
38e7349
fix(plugin-api): apply attributes, align and properties edits as fiel…
Princesseuh Nov 7, 2026
7d0bb36
fix(visitor): send attributes, align and properties as field edits
Princesseuh Nov 7, 2026
26bda2d
feat(visitor): accept an array of nodes as a visitor's return
Princesseuh Nov 7, 2026
7318eff
fix(visitor): count a negative child index from the end
Princesseuh Nov 7, 2026
428a2ee
fix(visitor): name the plugin in errors from its queued edits
Princesseuh Nov 7, 2026
b1bf805
test(visitor): cover list field edits, array returns, child indexes a…
Princesseuh Nov 7, 2026
a29e7ee
docs(plugin-api): describe array returns, negative child indexes and …
Princesseuh Nov 7, 2026
3ae2fb5
feat(plugin-api): apply plugin edits in the order they are made
Princesseuh Nov 8, 2026
a3e5673
fix(plugin-api): keep in-order edits linear on wide parents and fix c…
Princesseuh Nov 8, 2026
d03b30a
fix(plugin-api): never let raw replacement content take over the repl…
Princesseuh Nov 8, 2026
2c75422
refactor(plugin-api): simplify the in-order applier and drop keep_chi…
Princesseuh Nov 8, 2026
501d8fa
fix(plugin-api): check values the same way on every route and name th…
Princesseuh Nov 8, 2026
9ea5372
chore(changeset): merge the plugin edit changesets into one per behavior
Princesseuh Nov 8, 2026
5280cf0
test(plugin-api): merge duplicate edit tests and keep main's test hel…
Princesseuh Nov 8, 2026
1adbc6a
revert(visitor): count a negative child index from the end
Princesseuh Nov 8, 2026
806cc35
revert(plugin-api): check values the same way on every route and name…
Princesseuh Nov 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/ast-edits-in-call-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/satteri-ast: patch
---

Changed patches to apply one at a time in the order given, through the new `PatchApplier`, with a ref moving its target instead of copying it, `KEEP_CHILDREN_NODE_TYPE` replacing `Patch::Replace`'s `keep_children`, and `REF_KIND_SLOT` removed.
5 changes: 5 additions & 0 deletions .sampo/changesets/ast-error-labels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/satteri-ast: patch
---

Changed patch errors to name the nodes involved with a `NodeLabel`, adding `ChildrenOnLeaf`, `NodePlacedTwice`, `NodeInsideItself` and `AncestorPlacedInside`, and removing `WrapOnRemovedNode` and `ChildPatchOnRemovedNode`.
5 changes: 5 additions & 0 deletions .sampo/changesets/children-on-leaf-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
npm/satteri: patch
---

Changed adding children to a node that cannot hold any, such as text or a void element, to throw instead of silently dropping them.
5 changes: 5 additions & 0 deletions .sampo/changesets/edit-error-messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
npm/satteri: patch
---

Changed errors from queued edits to name the plugin and the nodes involved with their line and column, instead of internal node ids.
6 changes: 6 additions & 0 deletions .sampo/changesets/edits-in-call-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
npm/satteri: patch
cargo/satteri-plugin-api: patch
---

Changed a plugin's edits, its visitors' return values included, to apply in the order they are made, each to the tree the earlier ones left, fixing edits that were lost or failed the compile when the same plugin also replaced, wrapped or returned the node.
5 changes: 5 additions & 0 deletions .sampo/changesets/node-moves-not-copies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
npm/satteri: patch
---

Changed inserting an existing node, or listing it in a replacement, to move it instead of copying it, and listing a node twice, inserting it next to or into itself, or passing a node from the tree as a `wrapNode` wrapper to throw; insert `structuredClone(node)` for a copy.
5 changes: 5 additions & 0 deletions .sampo/changesets/removed-node-stays-removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
npm/satteri: patch
---

Changed a node removed with `ctx.removeNode` to stay removed for the rest of the pass, with a warning if the plugin also inserts it or lists it in a replacement.
5 changes: 5 additions & 0 deletions .sampo/changesets/visitor-array-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
npm/satteri: patch
---

Added returning an array of nodes from a visitor, which replaces the visited node with them, or removes it when empty.
5 changes: 3 additions & 2 deletions crates/bench/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use satteri_arena::{Arena, ArenaBuilder, Hast, StringRef};
use satteri_ast::hast::HastNodeType;
use satteri_ast::hast::codec::encode_element_data;
use satteri_ast::patch::{Patch, PatchContent};
use satteri_ast::patch::{KEEP_CHILDREN_NODE_TYPE, Patch, PatchContent};

/// One keep-children `<span class="link">` Replace per `<a>` element (the link-transform shape).
pub fn link_replace_patches(hast: &Arena<Hast>) -> Vec<Patch<Hast>> {
Expand All @@ -21,11 +21,12 @@ pub fn link_replace_patches(hast: &Arena<Hast>) -> Vec<Patch<Hast>> {
let val = b.alloc_string("link");
b.open_node(HastNodeType::Element as u8);
b.set_data_current(&encode_element_data(tag, &[(name, 0, val)]));
b.open_node(KEEP_CHILDREN_NODE_TYPE);
b.close_node();
b.close_node();
patches.push(Patch::Replace {
node_id: id,
new_tree: PatchContent::Tree(b.finish()),
keep_children: true,
});
}
patches
Expand Down
122 changes: 103 additions & 19 deletions crates/satteri-ast/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,76 @@
//!
//! The command dispatch logic lives in `satteri_plugin_api::js_commands`.

use std::fmt;

use satteri_arena::{ArenaKind, Document, Hast, Mdast};

use crate::hast::HastNodeType;
use crate::hast::codec::decode_element_tag;
use crate::mdast::MdastNodeType;

/// A node as a plugin author finds it in the document: its type, and where it starts.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeLabel {
pub id: u32,
/// The node's type, or `<tag>` for an HTML element.
pub node_type: String,
/// 1-based line and column, for a node read from the source.
pub start: Option<(usize, usize)>,
}

impl NodeLabel {
pub fn of<K: ArenaKind>(arena: &Document<'_, K>, id: u32) -> Self {
let node = arena.get_node(id);
let node_type = if K::KIND_TAG == Mdast::KIND_TAG {
MdastNodeType::from_u8(node.node_type).map(|t| t.name().to_string())
} else if K::KIND_TAG == Hast::KIND_TAG {
HastNodeType::from_u8(node.node_type).map(|t| match t {
HastNodeType::Element if arena.get_type_data(id).len() >= 8 => {
format!(
"<{}>",
arena.get_str(decode_element_tag(arena.get_type_data(id)))
)
}
_ => t.name().to_string(),
})
} else {
None
};
// Offsets are recorded even when line and column tracking is off.
let start = if node.start_line > 0 {
Some((node.start_line as usize, node.start_column as usize))
} else if node.end_offset > node.start_offset {
arena
.source()
.get(..node.start_offset as usize)
.map(|before| {
let line_start = before.rfind('\n').map_or(0, |i| i + 1);
(
before.matches('\n').count() + 1,
before[line_start..].chars().count() + 1,
)
})
} else {
None
};
NodeLabel {
id,
node_type: node_type.unwrap_or_else(|| "node".to_string()),
start,
}
}
}

impl fmt::Display for NodeLabel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.start {
Some((line, column)) => write!(f, "the {} at {line}:{column}", self.node_type),
None => write!(f, "the {} node without a source position", self.node_type),
}
}
}

#[derive(Debug)]
pub enum CommandError {
UnexpectedEof,
Expand Down Expand Up @@ -36,16 +106,19 @@ pub enum CommandError {
UnencodableNodeType(&'static str),
/// An op-stream nests OPENs deeper than the replay supports.
OpstreamTooDeep(usize),
/// `wrapNode` was issued against a node that is also removed in the same
/// command buffer. There's no defined way to "wrap then remove" or
/// "remove then wrap" the same anchor.
WrapOnRemovedNode(u32),
/// `prependChild` or `appendChild` was issued against a node that is
/// also removed. The removed node has no inside to receive children.
ChildPatchOnRemovedNode(u32),
/// A patch's anchor lives inside a subtree whose root was removed, so
/// the patch can never apply.
PatchOnRemovedSubtree(u32),
/// A patch has nowhere to land: by the time it applies, its node is no longer in the tree.
PatchOnRemovedSubtree(NodeLabel),
/// Children are added to a node that cannot hold any, such as text or a void element.
ChildrenOnLeaf(NodeLabel),
/// One payload names the same node twice; a node can only be in one place.
NodePlacedTwice(NodeLabel),
/// Content placed into or next to a node, or replacing the root, contains that node.
NodeInsideItself(NodeLabel),
/// Content placed into or next to `node` contains one of its ancestors.
AncestorPlacedInside {
ancestor: NodeLabel,
node: NodeLabel,
},
/// An op-stream's `OP_OPEN`s and `OP_CLOSE`s did not pair up.
UnbalancedOpstream,
/// A document op-stream is not the one `root` wrapping the tree that
Expand All @@ -56,8 +129,7 @@ pub enum CommandError {
/// A stored node's `type_data` is shorter than its declared layout, so a
/// field write would spill into the next node's data.
TypeDataTooShort,
/// A structurally valid but re-entrant patch shape (e.g. a payload ref
/// naming its own anchor's ancestor) that in-place application rejects.
/// A patch shape the apply cannot place, such as a sibling insert on the root.
UnsupportedPatchShape(&'static str),
/// A `wrapNode` `{raw}` payload with no usable wrapper; the string
/// says why.
Expand Down Expand Up @@ -104,9 +176,6 @@ impl std::fmt::Display for CommandError {
Self::OpstreamTooDeep(max) => {
write!(f, "content nests deeper than {max} levels")
}
Self::WrapOnRemovedNode(id) => {
write!(f, "wrapNode targets node {id} which is also removed")
}
Self::UnbalancedOpstream => {
write!(f, "unbalanced op-stream: OPEN and CLOSE ops do not pair up")
}
Expand All @@ -125,13 +194,28 @@ impl std::fmt::Display for CommandError {
"stored node type_data is shorter than its layout requires"
)
}
Self::ChildPatchOnRemovedNode(id) => write!(
Self::NodePlacedTwice(node) => write!(
f,
"prependChild/appendChild targets node {id} which is also removed"
"{node} is placed in two places, but a node can only be in one; place structuredClone(node) where you want a copy"
),
Self::PatchOnRemovedSubtree(id) => {
write!(f, "patch targets node {id} inside a removed subtree")
Self::NodeInsideItself(node) => write!(
f,
"content placed next to or into {node} contains that same node; place structuredClone(node) there for a copy"
),
Self::ChildrenOnLeaf(node) => {
write!(
f,
"{node} cannot hold children, so nothing can be inserted into it"
)
}
Self::AncestorPlacedInside { ancestor, node } => write!(
f,
"{ancestor} is placed next to or inside {node}, which it contains"
),
Self::PatchOnRemovedSubtree(node) => write!(
f,
"an edit to {node} has nowhere to land: by then the node, or a subtree holding it, was removed or replaced"
),
Self::InvalidRawWrapper(reason) => {
write!(f, "wrapNode: {{raw}} wrapper {reason}")
}
Expand Down
Loading
Loading