示例#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));
        }
        public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node)
        {
            var parentProperty   = node.FirstAncestorOrSelf <PropertyDeclarationSyntax>();
            var parentInvocation = node.FirstAncestorOrSelf <InvocationExpressionSyntax>();

            if (parentProperty != null && parentProperty.Identifier.Text == "SelectorExpression" ||
                parentInvocation != null && parentInvocation.ToString().Contains("SelectorExpression"))
            {
                var initializerExpressions    = GenerateInitializerExpressions(_metadata, "", "p.").ToList();
                var expressionsWithSeparators = node.AddExpressions(initializerExpressions.ToArray()).Expressions.GetWithSeparators();

                // This section is here only to format code well. Namely, in initializer expression, to add comma token after each expression and newline after comma.
                var list = new List <SyntaxNodeOrToken>();
                var expressionTrailingTrivia = new List <SyntaxTrivia>();

                foreach (var item in expressionsWithSeparators.ToList())
                {
                    // This is required if we have a custom code trivia which is attached to expression node, but should be attached after comma-token.
                    if (item.IsToken)
                    {
                        expressionTrailingTrivia.Add(SyntaxExtenders.EndOfLineTrivia);
                        list.Add(item.AsToken().WithTrailingTrivia(expressionTrailingTrivia.ToArray()));
                        expressionTrailingTrivia.Clear();
                    }

                    else
                    {
                        expressionTrailingTrivia = item.GetTrailingTrivia().ToList();
                        list.Add(item.WithTrailingTrivia());
                    }
                }

                if (list.Any() && list.Last().IsNode)
                {
                    var item = list.Last();
                    list.Remove(item);
                    list.Add(item.WithTrailingTrivia(expressionTrailingTrivia));
                }

                return(node.WithExpressions(SyntaxFactory.SeparatedList <ExpressionSyntax>(list)));
            }

            return(base.VisitInitializerExpression(node));
        }
        public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node)
        {
            var parentProperty   = node.FirstAncestorOrSelf <PropertyDeclarationSyntax>();
            var parentInvocation = node.FirstAncestorOrSelf <InvocationExpressionSyntax>();

            if (parentProperty != null && parentProperty.Identifier.Text == "SelectorExpression" ||
                parentInvocation != null && parentInvocation.ToString().Contains("SelectorExpression"))
            {
                var initializerExpressions = GenerateInitializerExpressions(_metadata, "", "p.").ToList();
                var nodeTokenList          = SyntaxFactory.NodeOrTokenList(node.ChildNodesAndTokens())
                                             .RemoveAt(node.ChildNodesAndTokens().Count - 1)
                                             .RemoveAt(0);

                foreach (var exp in initializerExpressions)
                {
                    nodeTokenList = nodeTokenList.Add(exp);
                    nodeTokenList = nodeTokenList.Add(SyntaxFactory.Token(SyntaxKind.CommaToken).AppendNewLine());
                }

                return(node.WithExpressions(SyntaxFactory.SeparatedList <ExpressionSyntax>(nodeTokenList)));
            }

            return(base.VisitInitializerExpression(node));
        }