/// <summary> /// Add documentation for the property. /// </summary> /// <param name="context">the code fix context.</param> /// <param name="root">the root syntax node.</param> /// <param name="fieldDeclaration">the property declaration containing invalid documentation.</param> /// <param name="documentComment">the existing comment.</param> /// <returns>the correct code.</returns> private Task<Document> AddDocumentationAsync( CodeFixContext context, SyntaxNode root, FieldDeclarationSyntax fieldDeclaration, DocumentationCommentTriviaSyntax documentComment) { var @class = fieldDeclaration.Parent as ClassDeclarationSyntax; var first = @class?.DescendantNodes().FirstOrDefault() == fieldDeclaration; var summary = this._commentNodeFactory.CreateCommentSummaryText(fieldDeclaration); var comment = this._commentNodeFactory .CreateDocumentComment(summary) .WithAdditionalAnnotations(Formatter.Annotation); var trivia = SyntaxFactory.Trivia(comment); var pd = first ? fieldDeclaration.WithLeadingTrivia(trivia) : fieldDeclaration.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, trivia); var result = documentComment != null ? root.ReplaceNode(documentComment, comment.AdjustDocumentationCommentNewLineTrivia()) : root.ReplaceNode(fieldDeclaration, pd); var newDocument = context.Document.WithSyntaxRoot(result); return Task.FromResult(newDocument); }
private void MoveInitialTrivia(ref FieldDeclarationSyntax newField, ref MemberDeclarationSyntax newDeclaration) { var leadingTrivia = newDeclaration.GetLeadingTrivia(); if (!leadingTrivia.Any()) return; var last = leadingTrivia.Last(); if (leadingTrivia.Any() && (SyntaxKind)last.RawKind == SyntaxKind.WhitespaceTrivia) { newField = newField.WithLeadingTrivia(leadingTrivia); newDeclaration = newDeclaration.WithLeadingTrivia(last); } }