Roslyn: Annotations are not preserved on non-whitespace trivia after formatting

Created on 19 Nov 2020  路  3Comments  路  Source: dotnet/roslyn

Version Used: CodeAnalysis 3.8.0, dotnet sdk 5.0.100

Steps to Reproduce:

```C#
class Program
{
public static string Text => @"
namespace TestApp
{
class Test
{
/* __marker__ */
}
}
";

    static void Main(string[] args)
    {
        var pos = Text.IndexOf("/* __marker__ */");
        var syntaxTree = CSharpSyntaxTree.ParseText(Text);
        var root = syntaxTree.GetRoot();

        // Create an annotation and attach it to the multiline comment trivia. Then replace the original trivia with the annotated trivia.
        var annotation = new SyntaxAnnotation("marker");
        var markerTrivia = root.FindTrivia(pos, findInsideTrivia: true);
        var annotatedMarkerTrivia = markerTrivia.WithAdditionalAnnotations(annotation);
        root = root.ReplaceTrivia(markerTrivia, annotatedMarkerTrivia);

        // Format the root node and get all trivia with the marker annotation
        var formattedRoot = Formatter.Format(root, new AdhocWorkspace());
        var annotatedTrivia = formattedRoot.GetAnnotatedTrivia("marker");

        Console.WriteLine(annotatedTrivia.Count()); // Expected 1; Actual 0
    }
}

```

Expected Behavior: The annotation is preserved

Actual Behavior: The annotation is not preserved

Area-IDE Bug IDE-Formatter Resolution-Fixed

Most helpful comment

I have a fix for this. PR soon.

All 3 comments

Discussed offline with @CyrusNajmabadi. See original discussion here, https://github.com/dotnet/roslyn/discussions/49485

I'm investigating this now.

I have a fix for this. PR soon.

Was this page helpful?
0 / 5 - 0 ratings