Mapping a SyntaxNode Through Changes

I’m trying to write a command that asynchronously replaces (using effects) the corresponding range of a SyntaxNode in a document.

For example, with

=A1|

I would want to replace A1 with A2 on keyboard down, since A1 is parsed as a specific syntax node in my spreadsheet grammar.

Is there a way to check if a SyntaxNode is modified by a transaction, or is reused between two syntax trees?

I see that Lezer exports a NodeWeakMap but I’m not sure if that’s intended to be used along with CodeMirror.

Normally, if I were to asynchronously replace a range, I would probably keep a {from, to} tuple and map it through the transaction, checking if either positions were deleted.

Reuse between trees is probably not the check you’re looking for here—nearby changes can cause nodes to be reallocated even if they themselves aren’t touched. Maybe just check if any intermediate change touched the node’s range, and, if that’s important, that it’s still parsed the same way in the current syntax tree (i.e. that it wasn’t commented out or quoted).

nearby changes can cause nodes to be reallocated even if they themselves aren’t touched

Ah, that’s good to know about the reference stability of a SyntaxNode. I will probably write some type of heuristic to check if a range has an approximately equivalent SyntaxNode in between trees.

Thanks!