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?

asked Oct 25, 2016 at 18:55
\$\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\$ 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)
answered Oct 25, 2016 at 19:43
\$\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\$ Commented Oct 25, 2016 at 19:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.