Implement tracking of syntax nodes if the syntax tree is edited #2118
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.
On any syntax tree, you can call
.tracked
. Any tree that is derived from that tracked tree is able to find the original syntax node in the tracked tree. Use cases of this could be to find the original source ranges of syntax nodes that have been re-written in macros or to allow a syntax rewriter to modify a syntax tree while still being able to get the original source locations for any node that was kept.The basic idea is as follows: Every syntax node within the tree already contains an
indexInTree
, which is its index in a depth-first traversal. The node of each tree stores the ranges ofindexInTree
that have been re-used from the original tree and the offset my which theirindexInTree
has been shifted compared to the original tree.For example, if you have the following tree (index in tree in parentheses)
And you construct a new tree a follows
Then the new tree would have a single syntax tracking range of
2...4: -2
. This defines that nodes withindexInTree
2 to 4 occur in the tracked tree and that to receive theindexInTree
within the tracked tree, 2 needs to be subtracted from the node in the derived tree.I still need to investigate the performance implications of this change. My assumption is that
rootInfo.syntaxTracking?.trackedTree != nil
fromtrackedTree != nil
inreplacingChild
)