Access `closedBy` node prop from SyntaxNode

In my parser grammar, I have declared the following tokens:
BeginHandlebarsExpression[closedBy="EndHandlebarsExpression"] { "{{" }
EndHandlebarsExpression { "}}" }

I’m wondering if there’s any way that I can traverse quickly between the Begin and End syntax nodes that correspond to those tokens. Something like node.openedBy, much like how I use node.parent or node.prevSibling.

I can see a reference to the desired node’s name on node.type.props[0], but no direct handle for the node itself. Am I barking up the wrong tree?

(pun intended)

Open/close relations are not directly tracked by the tree, and there are no accessors for them. The general approach would be to scan sibling nodes for a node of that type, possibly optimizing by only checking the first/last sibling if you know the structure of the parent node puts the delimiters at the start/end.

1 Like

Makes sense! My general approach looks like this:

let node = syntaxTree(context.state).resolveInner(currentPosition, -1)

if (node.name === "EndHandlebarsExp") {
  while (node.name !== 'BeginHandlebarsExp' && node.prevSibling) {
    node = node.prevSibling
  }
}

Seems to work great.

Thanks for your help, marijn