3
\$\begingroup\$

I am trying to turn

namespace DevComponents.WpfEditors.Primitives
{
    /// <summary>
    /// Interaction logic for EditorFreeTextButton.xaml
    /// </summary>
    public partial class EditorFreeTextButton : EditorButton
    {
        public EditorFreeTextButton()
        {
            InitializeComponent();
        }
    }
}

into

namespace DevComponents.WpfEditors.Primitives
{
    /// <summary>
    /// Interaction logic for EditorFreeTextButton.xaml
    /// </summary>
    [System.Reflection.Obfuscation(Exclude = true)]
    public partial class EditorFreeTextButton : EditorButton
    {
        public EditorFreeTextButton()
        {
            InitializeComponent();
        }
    }
}

through Roslyn. I have the following code that works

private static SyntaxNode AddAnnotation(ClassDeclarationSyntax classNode, SyntaxNode rootNode)
{
    var name = SyntaxFactory.ParseName("System.Reflection.Obfuscation");
    var arguments = SyntaxFactory.ParseAttributeArgumentList("(Exclude = true)");
    var attribute = SyntaxFactory.Attribute(name, arguments);
    var leadingTrivia = classNode.GetLeadingTrivia();
    var whitespace = leadingTrivia.LastOrDefault(x => x.Kind() == SyntaxKind.WhitespaceTrivia);

    var attributeList = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(attribute))
        .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
    var newClassNode = classNode.WithoutLeadingTrivia()
        .WithLeadingTrivia(whitespace)
        .AddAttributeLists(attributeList)
        .WithLeadingTrivia(leadingTrivia);

    rootNode = rootNode.ReplaceNode(classNode, newClassNode);
    return rootNode;
}

However I feel like I am coding in circles to get all the leading Trivia right.

Is there a easier way to insert an attribute to be after the SingleLineDocumentationCommentTrivia and be formatted correctly with the correct leading whitespace trivia?

\$\endgroup\$
2
  • \$\begingroup\$ @Hosch250 I think i misread what you said before I commented and you did not use the function I think you where using. Please undelete your answer so I can look in to and ask you about Formatter.Annotation more. \$\endgroup\$ Commented Oct 25, 2016 at 19:48
  • \$\begingroup\$ I'm so sorry, I missed your notification. \$\endgroup\$
    – user34073
    Commented Dec 29, 2016 at 21:30

1 Answer 1

1
\$\begingroup\$

Look into the WithAnnotation(Formatter.Annotation) member. This should create a new node with the correct leading indentation.

var newClassNode = classNode.AddAttributeLists(attributeList)
                            .WithAnnotation(Formatter.Annotation)
\$\endgroup\$
2
  • \$\begingroup\$ That puts the annotation before the SingleLineDocumentationCommentTrivia. however the Formatter.Annotation might be useful \$\endgroup\$ Commented Oct 25, 2016 at 19:43
  • \$\begingroup\$ OK, I'll delete this then. \$\endgroup\$
    – user34073
    Commented Oct 25, 2016 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.