Ejemplo n.º 1
0
        private static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, EqualsValueClauseSyntax initializer)
        {
            AccessorListSyntax accessorList = CreateAccessorList(property);

            accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList);

            if (initializer != null)
            {
                accessorList = accessorList
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithoutTrailingTrivia());
            }

            PropertyDeclarationSyntax newProperty = property
                                                    .WithIdentifier(property.Identifier.WithTrailingSpace())
                                                    .WithExpressionBody(null)
                                                    .WithAccessorList(accessorList);

            if (initializer != null)
            {
                newProperty = newProperty
                              .WithInitializer(initializer)
                              .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
            }
            else
            {
                newProperty = newProperty
                              .WithSemicolonToken(Token(SyntaxKind.None));
            }

            return(newProperty
                   .WithTriviaFrom(property)
                   .WithAdditionalAnnotations(Formatter.Annotation));
        }
Ejemplo n.º 2
0
        private static async Task <Document> FormatAccessorListAsync(
            Document document,
            AccessorListSyntax accessorList,
            CancellationToken cancellationToken)
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);

            if (accessorList.Accessors.All(f => f.Body == null))
            {
                var propertyDeclaration = (PropertyDeclarationSyntax)accessorList.Parent;

                TextSpan span = TextSpan.FromBounds(
                    propertyDeclaration.Identifier.Span.End,
                    accessorList.CloseBraceToken.Span.Start);

                PropertyDeclarationSyntax newPropertyDeclaration =
                    WhitespaceOrEndOfLineRemover.RemoveFrom(propertyDeclaration, span);

                newPropertyDeclaration = newPropertyDeclaration
                                         .WithAdditionalAnnotations(Formatter.Annotation);

                root = root.ReplaceNode(propertyDeclaration, newPropertyDeclaration);
            }
            else
            {
                AccessorListSyntax newAccessorList = GetNewAccessorList(accessorList)
                                                     .WithAdditionalAnnotations(Formatter.Annotation);

                root = root.ReplaceNode(accessorList, newAccessorList);
            }

            return(document.WithSyntaxRoot(root));
        }
        private static ExpressionSyntax ProcessExpression(ExpressionSyntax expression)
        {
            if (expression
                .DescendantTrivia(expression.Span)
                .All(f => f.IsWhitespaceOrEndOfLine()))
            {
                expression = WhitespaceOrEndOfLineRemover.RemoveFrom(expression)
                             .WithAdditionalAnnotations(Formatter.Annotation);
            }

            return(expression);
        }
        private static async Task <Document> FormatAllParametersOnSingleLineAsync(
            Document document,
            ParameterListSyntax parameterList,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            ParameterListSyntax newParameterList = WhitespaceOrEndOfLineRemover.RemoveFrom(parameterList)
                                                   .WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(parameterList, newParameterList);

            return(document.WithSyntaxRoot(newRoot));
        }
Ejemplo n.º 5
0
            public override SyntaxNode VisitAccessorDeclaration(AccessorDeclarationSyntax node)
            {
                if (node == null)
                {
                    throw new ArgumentNullException(nameof(node));
                }

                if (AccessorListDiagnosticAnalyzer.ShouldBeFormatted(node))
                {
                    return(WhitespaceOrEndOfLineRemover.RemoveFrom(node, node.Span));
                }

                return(base.VisitAccessorDeclaration(node));
            }
        private static async Task <Document> FormatExpressionChainOnSingleLineAsync(
            Document document,
            MemberAccessExpressionSyntax expression,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            SyntaxNode newNode = WhitespaceOrEndOfLineRemover.RemoveFrom(expression)
                                 .WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(expression, newNode);

            return(document.WithSyntaxRoot(newRoot));
        }
Ejemplo n.º 7
0
        private static AccessorListSyntax GetNewAccessorList(AccessorListSyntax accessorList)
        {
            if (accessorList.IsSingleline(includeExteriorTrivia: false))
            {
                SyntaxTriviaList triviaList = accessorList.CloseBraceToken.LeadingTrivia
                                              .Add(SyntaxHelper.NewLine);

                return(WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                       .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(triviaList)));
            }
            else
            {
                return(AccessorSyntaxRewriter.VisitNode(accessorList));
            }
        }
Ejemplo n.º 8
0
        private static AccessorListSyntax CreateAccessorList(BlockSyntax block, bool singleline)
        {
            AccessorListSyntax accessorList =
                AccessorList(
                    SingletonList(
                        AccessorDeclaration(
                            SyntaxKind.GetAccessorDeclaration,
                            block)));

            if (singleline)
            {
                accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList);
            }

            return(accessorList);
        }
Ejemplo n.º 9
0
        private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(
                List(propertyDeclaration
                     .AccessorList
                     .Accessors.Select(accessor => accessor
                                       .WithBody(Block())
                                       .WithSemicolonToken(Token(SyntaxKind.None)))));

            accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));

            return(propertyDeclaration
                   .WithInitializer(null)
                   .WithAccessorList(accessorList));
        }
        private static AccessorListSyntax CreateAccessorList(BlockSyntax block, bool singleline)
        {
            AccessorListSyntax accessorList =
                AccessorList(
                    SingletonList(
                        AccessorDeclaration(
                            SyntaxKind.GetAccessorDeclaration,
                            block)));

            if (singleline)
            {
                accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));
            }

            return(accessorList);
        }
Ejemplo n.º 11
0
        private static PropertyDeclarationSyntax ExpandPropertyAndAddBackingField(PropertyDeclarationSyntax propertyDeclaration, string name)
        {
            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                AccessorDeclarationSyntax newGetter = getter
                                                      .WithBody(
                    Block(
                        SingletonList <StatementSyntax>(
                            ReturnStatement(IdentifierName(name)))))
                                                      .WithSemicolonToken(Token(SyntaxKind.None));

                propertyDeclaration = propertyDeclaration
                                      .WithInitializer(null)
                                      .WithAccessorList(
                    propertyDeclaration.AccessorList.ReplaceNode(getter, newGetter));
            }

            AccessorDeclarationSyntax setter = propertyDeclaration.Setter();

            if (setter != null)
            {
                AccessorDeclarationSyntax newSetter = setter
                                                      .WithBody(
                    Block(
                        SingletonList <StatementSyntax>(
                            ExpressionStatement(
                                AssignmentExpression(
                                    SyntaxKind.SimpleAssignmentExpression,
                                    IdentifierName(name),
                                    IdentifierName("value"))))))
                                                      .WithSemicolonToken(Token(SyntaxKind.None));

                propertyDeclaration = propertyDeclaration
                                      .WithAccessorList(
                    propertyDeclaration.AccessorList.ReplaceNode(setter, newSetter));
            }

            AccessorListSyntax accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(propertyDeclaration.AccessorList)
                                              .WithCloseBraceToken(propertyDeclaration.AccessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));

            return(propertyDeclaration
                   .WithAccessorList(accessorList));
        }
        private static EventDeclarationSyntax Expand(EventFieldDeclarationSyntax eventDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(
                List(new AccessorDeclarationSyntax[]
            {
                AccessorDeclaration(SyntaxKind.AddAccessorDeclaration, Block()),
                AccessorDeclaration(SyntaxKind.RemoveAccessorDeclaration, Block()),
            }));

            accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));

            VariableDeclaratorSyntax declarator = eventDeclaration.Declaration.Variables[0];

            return(EventDeclaration(
                       eventDeclaration.AttributeLists,
                       eventDeclaration.Modifiers,
                       eventDeclaration.Declaration.Type,
                       null,
                       declarator.Identifier,
                       accessorList));
        }