KirillOsenkov / RoslynQuoter

Roslyn tool that for a given C# program shows syntax tree API calls to construct its syntax tree
http://roslynquoter.azurewebsites.net
Apache License 2.0
918 stars 118 forks source link

Confusing code generated for XML elements in documentation comments #47

Open svick opened 5 years ago

svick commented 5 years ago

Consider a member with XML documentation comment:

/// <summary></summary>
int i;

The relevant section of code generated for the XML comment is:

XmlExampleElement()
.WithStartTag(
    XmlElementStartTag(
        XmlName(
            Identifier("summary"))))
.WithEndTag(
    XmlElementEndTag(
        XmlName(
            Identifier("summary"))))

Notice how the code first creates an <example> element and then overwrites both tags with the right name. That's not technically wrong, but it's certainly a confusing way to create the tree.

Ideally, RoslynQuoter should use a specific factory method, when one is available (in this case, XmlSummaryElement). Though always using the generic XmlElement method would be fine too.

KirillOsenkov commented 5 years ago

Unfortunately the algorithm breaks down when there's more than one way to create the given tree. It works so well in general because usually there's only a single method that can create a node of the desired type, so there's no ambiguity.

However Xml factory methods were added later, and they have a few convenience methods, so the algorithm just chooses the first one that works, and that explains the results above.

We'd need some heuristics to fine tune this behavior and provide hints to the algorithm on a case-by-case basis.

KirillOsenkov commented 5 years ago

See here for details: https://github.com/KirillOsenkov/RoslynQuoter/blob/d2fd4bea22196129c33ece05820c75d0ec7788ec/src/Quoter/Quoter.cs#L897

I'm not sure how to add such a heuristic, perhaps we can have "factoryMethodsToPrefer", so we can disambiguate here: https://github.com/KirillOsenkov/RoslynQuoter/blob/d2fd4bea22196129c33ece05820c75d0ec7788ec/src/Quoter/Quoter.cs#L1069