private static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, EqualsValueClauseSyntax initializer) { AccessorListSyntax accessorList = CreateAccessorList(property); if (accessorList .DescendantTrivia() .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { accessorList = Remover.RemoveWhitespaceOrEndOfLine(accessorList); } PropertyDeclarationSyntax newProperty = property .WithIdentifier(property.Identifier.WithTrailingSpace()) .WithExpressionBody(null) .WithAccessorList(accessorList); if (initializer != null) { newProperty = newProperty .WithInitializer(initializer) .WithSemicolonToken(SemicolonToken()); } else { newProperty = newProperty.WithoutSemicolonToken(); } return(newProperty .WithTriviaFrom(property) .WithFormatterAnnotation()); }
public static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, EqualsValueClauseSyntax initializer) { AccessorListSyntax accessorList = property.AccessorList; if (accessorList != null) { SyntaxList <AccessorDeclarationSyntax> newAccessors = accessorList .Accessors .Select(accessor => { accessor = accessor.Update( attributeLists: accessor.AttributeLists, modifiers: accessor.Modifiers, keyword: accessor.Keyword, body: default(BlockSyntax), expressionBody: default(ArrowExpressionClauseSyntax), semicolonToken: SemicolonToken()); return(accessor.WithTriviaFrom(accessor)); }) .ToSyntaxList(); accessorList = accessorList.WithAccessors(newAccessors); } else { accessorList = AccessorList(AutoGetAccessorDeclaration()) .WithTriviaFrom(property.ExpressionBody); } if (accessorList .DescendantTrivia() .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { accessorList = accessorList.RemoveWhitespace(); } PropertyDeclarationSyntax newProperty = property.Update( attributeLists: property.AttributeLists, modifiers: property.Modifiers, type: property.Type, explicitInterfaceSpecifier: property.ExplicitInterfaceSpecifier, identifier: property.Identifier.WithTrailingTrivia(Space), accessorList: accessorList, expressionBody: default(ArrowExpressionClauseSyntax), initializer: initializer, semicolonToken: (initializer != null) ? SemicolonToken() : default(SyntaxToken)); return(newProperty .WithTriviaFrom(property) .WithFormatterAnnotation()); }