コード例 #1
0
        public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node)
        {
            var containingProperty = node.FirstAncestorOrSelf <PropertyDeclarationSyntax>();

            if (containingProperty != null && containingProperty.Identifier.ToString() == "SelectorExpression" &&
                containingProperty.FirstAncestorOrSelf <ClassDeclarationSyntax>().Identifier.ToString().Contains("Mapper"))
            {
                if (node.Expressions.Count == 0)
                {
                    node = node.WithOpenBraceToken(GetOpenBraceTokenWithEmptyCustomCode());
                }
                else if (node.Expressions.Count == 1)
                {
                    var newExpression = AddLeadingTriviaComment(AddTrailingTriviaComment(node.Expressions.Single()));
                    node = node.WithExpressions(node.Expressions.RemoveAt(0).Add(newExpression));
                }
                else
                {
                    var firstExpression = AddLeadingTriviaComment(node.Expressions.First());

                    var lastExpression      = AddTrailingTriviaComment(node.Expressions.Last());
                    var lastExpressionIndex = node.Expressions.Count - 1;

                    node = node.WithExpressions(node.Expressions.RemoveAt(lastExpressionIndex).RemoveAt(0).Insert(0, firstExpression).Add(lastExpression));
                }
            }

            return(base.VisitInitializerExpression(node));
        }
コード例 #2
0
        public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node)
        {
            if (NormalizeWhitespaceOnly &&
                node.IsKind(SyntaxKind.ArrayInitializerExpression) &&
                _checkingMethod != RegisterInstanceName)
            {
                var openBrace = node.OpenBraceToken.WithLeadingTrivia(SyntaxHelper.Whitespace(12));
                node = node.WithOpenBraceToken(openBrace);
                var closeBrace = node.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.EndOfLineTrivia, SyntaxHelper.Whitespace(12));
                node = node.WithCloseBraceToken(closeBrace);
            }

            return(base.VisitInitializerExpression(node !));
        }
コード例 #3
0
        private SyntaxNode VisitElementInitializer(InitializerExpressionSyntax node)
        {
            if (node.Expressions.Count != 2 && node.Expressions.All(SyntaxUtils.SpansSingleLine) && node.Expressions.Sum(n => n.Span.Length) < 80)
            {
                return(node);
            }

            var exprs = node.Expressions;

            exprs = SyntaxFactory.SeparatedList <ExpressionSyntax>(SyntaxFactory.NodeOrTokenList(
                                                                       exprs[0].WithoutTrivia(),
                                                                       exprs.GetSeparator(0).WithLeadingTrivia().WithTrailingTrivia(SyntaxFactory.Whitespace(" ")),
                                                                       exprs[1].WithLeadingTrivia().WithTrailingTrivia(SyntaxFactory.Whitespace(" "))
                                                                       ));

            return(node
                   .WithOpenBraceToken(node.OpenBraceToken.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")))
                   .WithExpressions(exprs)
                   .WithCloseBraceToken(node.CloseBraceToken.WithoutTrivia()));
        }
コード例 #4
0
        public static Task <Document> ToSingleLineAsync(
            Document document,
            InitializerExpressionSyntax initializer,
            bool removeTrailingComma            = false,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            InitializerExpressionSyntax newInitializer = initializer
                                                         .ReplaceWhitespace(ElasticSpace, TextSpan.FromBounds(initializer.Span.Start, initializer.Span.End))
                                                         .WithFormatterAnnotation();

            newInitializer = newInitializer.WithOpenBraceToken(newInitializer.OpenBraceToken.WithoutLeadingTrivia().WithTrailingTrivia(Space));

            newInitializer = newInitializer.WithCloseBraceToken(newInitializer.CloseBraceToken.WithoutLeadingTrivia());

            SeparatedSyntaxList <ExpressionSyntax> expressions = newInitializer.Expressions;

            if (expressions.Any())
            {
                ExpressionSyntax firstExpression = expressions.First();

                newInitializer = newInitializer.WithExpressions(expressions.Replace(firstExpression, firstExpression.WithoutLeadingTrivia()));

                expressions = newInitializer.Expressions;

                SyntaxToken trailingComma = expressions.GetTrailingSeparator();

                if (trailingComma.IsKind(SyntaxKind.CommaToken))
                {
                    if (removeTrailingComma)
                    {
                        expressions = expressions.ReplaceSeparator(trailingComma, MissingToken(SyntaxKind.CommaToken));

                        ExpressionSyntax lastExpression = expressions.Last();

                        expressions = expressions.Replace(lastExpression, lastExpression.WithTrailingTrivia(Space));

                        newInitializer = newInitializer.WithExpressions(expressions);
                    }
                    else
                    {
                        newInitializer = newInitializer.WithExpressions(expressions.ReplaceSeparator(trailingComma, trailingComma.WithTrailingTrivia(Space)));
                    }
                }
                else
                {
                    ExpressionSyntax lastExpression = expressions.Last();

                    newInitializer = newInitializer.WithExpressions(expressions.Replace(lastExpression, lastExpression.WithTrailingTrivia(Space)));
                }
            }

            SyntaxNode parent = initializer.Parent;

            SyntaxNode newParent;

            switch (parent.Kind())
            {
            case SyntaxKind.ObjectCreationExpression:
            {
                var expression = (ObjectCreationExpressionSyntax)parent;

                expression = expression.WithInitializer(newInitializer);

                ArgumentListSyntax argumentList = expression.ArgumentList;

                if (argumentList != null)
                {
                    newParent = expression.WithArgumentList(argumentList.WithTrailingTrivia(Space));
                }
                else
                {
                    newParent = expression.WithType(expression.Type.WithTrailingTrivia(Space));
                }

                break;
            }

            case SyntaxKind.ArrayCreationExpression:
            {
                var expression = (ArrayCreationExpressionSyntax)parent;

                newParent = expression
                            .WithInitializer(newInitializer)
                            .WithType(expression.Type.WithTrailingTrivia(Space));

                break;
            }

            case SyntaxKind.ImplicitArrayCreationExpression:
            {
                var expression = (ImplicitArrayCreationExpressionSyntax)parent;

                newParent = expression
                            .WithInitializer(newInitializer)
                            .WithCloseBracketToken(expression.CloseBracketToken.WithTrailingTrivia(Space));

                break;
            }

            case SyntaxKind.EqualsValueClause:
            {
                var equalsValueClause = (EqualsValueClauseSyntax)parent;

                newParent = equalsValueClause
                            .WithValue(newInitializer)
                            .WithEqualsToken(equalsValueClause.EqualsToken.WithTrailingTrivia(Space));

                break;
            }

            case SyntaxKind.SimpleAssignmentExpression:
            {
                var simpleAssignment = (AssignmentExpressionSyntax)parent;

                newParent = simpleAssignment
                            .WithRight(newInitializer)
                            .WithOperatorToken(simpleAssignment.OperatorToken.WithTrailingTrivia(Space));

                break;
            }

            default:
            {
                Debug.Fail(parent.Kind().ToString());

                return(document.ReplaceNodeAsync(initializer, newInitializer, cancellationToken));
            }
            }

            return(document.ReplaceNodeAsync(parent, newParent, cancellationToken));
        }