I’m interested in (iterating over) overlay trees. However, I’m not sure how I should check for the existence of a overlay tree:
- Is there a way to (performantly) check for the existence of an overlay tree?
- Is there a method that will enter overlay trees (i.e. similar to
resolveInner()
), that will enter theisTop
node directly (currently I have to traverse up to find it)? - Is there a way to check whether
resolveInner()
entered a overlay tree? - Is there a method, like
tree.iterate()
, that will iterate all sub-trees as well?
For Markdown, I am currently able to deduce the existence of an overlay tree, because they’re likely to start at the same position as the first CodeText
node coming after a CodeInfo
node:
const state = view.state;
const tree = syntaxTree(state);
const doc = state.doc;
tree.iterate({
enter: (nodeRef) => {
if (nodeRef.name !== 'CodeInfo') return;
const codeText = nodeRef.node.nextSibling;
if (codeText?.name !== 'CodeText') return;
// Unsure how to deduce whether resolveInner() resolved to a sub tree or not.
const subTreeNode = tree.resolveInner(codeText.from, 1);
const subTreeCursor = subTreeNode.cursor();
// May it be possible to enter the top node directly?
while (!subTreeCursor.type.isTop && subTreeCursor.parent()) {}
if (subTreeCursor.from === 0) return; // Hit top `Document` node; there wasn't a overlay tree after all.
console.log(subTreeCursor.name); // E.g. `Script` for javascript
console.log(doc.sliceString(subTreeCursor.from, subTreeCursor.to)); // Full javascript snippet.
iterateSubTree(subTreeCursor, (cursor, depth) => {
console.log('\t'.repeat(depth) + cursor.name); // Javascript nodes.
});
},
});