private static async Task<Document> ReplaceWithExpressionBodiedMember(Document document, PropertyDeclarationSyntax declaration, CancellationToken cancellationToken) { SyntaxTriviaList leadingTrivia; var expression = GetExpression(declaration.AccessorList, out leadingTrivia); var declarationTrivia = declaration.GetLeadingTrivia(); declarationTrivia = declarationTrivia.AddRange(leadingTrivia); var newDeclaration = declaration .WithLeadingTrivia(declarationTrivia) .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(expression)) .WithAccessorList(null) .WithSemicolonToken(GetSemicolon(declaration.AccessorList)) .WithAdditionalAnnotations(Formatter.Annotation); var root = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = root.ReplaceNode(declaration, newDeclaration); return document.WithSyntaxRoot(newRoot); }
/// <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="propertyDeclaration">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, PropertyDeclarationSyntax propertyDeclaration, DocumentationCommentTriviaSyntax documentComment) { var @class = propertyDeclaration.Parent as ClassDeclarationSyntax; var first = @class?.DescendantNodes().FirstOrDefault() == propertyDeclaration; var summary = this._commentNodeFactory.CreateCommentSummaryTextFromExistingProperty(propertyDeclaration, documentComment) ?? this._commentNodeFactory.CreateCommentSummaryText(propertyDeclaration); var comment = this._commentNodeFactory .CreateDocumentComment(summary) .WithAdditionalAnnotations(Formatter.Annotation); var trivia = SyntaxFactory.Trivia(comment); var pd = first ? propertyDeclaration.WithLeadingTrivia(trivia) : propertyDeclaration.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, trivia); var result = documentComment != null ? root.ReplaceNode(documentComment, comment.AdjustDocumentationCommentNewLineTrivia()) : root.ReplaceNode(propertyDeclaration, pd); var newDocument = context.Document.WithSyntaxRoot(result); return Task.FromResult(newDocument); }