Syntax RoslynTree Diff

Say I have two SyntaxTree A and B ,
where B was created by modifying A.

I would like the following information:

  • Syntax Nodes and tokens removed from A to create B
  • Syntax Nodes and Tokens Added to A to Create B

Is there an API for this?
If not, how can this be effectively calculated?

This information must be available to Roslyn.
since immutable GreenNode are shared between trees.

One solution I can think of is using SyntaxTree.GetChangedSpans()
and then find the intersecting tokens.
However, this seems like a hack, and I'm not sure if it is always accurate. A small change to the text can greatly affect SyntaxTree :
(for example, replacing * with + in an expression can change the order / priority)

+5
source share
1 answer

We are internally different from each other , which lives at the compiler level and thus uses green nodes, we simply did not disclose it as an API. This is what we use to manage GetChangedSpans. We intentionally did not open the green nodes directly, because it was an implementation detail.

There is no specific reason why the API cannot be made public. I think when this happened, we were worried about how the behavior is actually specified or what minimal "kindness" you can expect from diff. This, and we did not have a motivational scenario, to make sure that our work was useful.

+1
source

Source: https://habr.com/ru/post/1241221/


All Articles