private static InitializerExpressionSyntax GetMultilineInitializer(InitializerExpressionSyntax initializer)
        {
            SyntaxNode parent = initializer.Parent;

            if (parent.IsKind(SyntaxKind.ObjectCreationExpression) &&
                !initializer.IsKind(SyntaxKind.CollectionInitializerExpression))
            {
                return(initializer
                       .WithExpressions(
                           SeparatedList(
                               initializer.Expressions.Select(expression => expression.WithLeadingTrivia(CSharpFactory.NewLineTrivia()))))
                       .WithFormatterAnnotation());
            }

            SyntaxTriviaList indent  = SyntaxUtility.GetIndentTrivia(initializer);
            SyntaxTriviaList indent2 = indent.Add(CSharpFactory.IndentTrivia());

            indent  = indent.Insert(0, CSharpFactory.NewLineTrivia());
            indent2 = indent2.Insert(0, CSharpFactory.NewLineTrivia());

            return(initializer
                   .WithExpressions(
                       SeparatedList(
                           initializer.Expressions.Select(expression => expression.WithLeadingTrivia(indent2))))
                   .WithOpenBraceToken(initializer.OpenBraceToken.WithLeadingTrivia(indent))
                   .WithCloseBraceToken(initializer.CloseBraceToken.WithLeadingTrivia(indent))
                   .WithFormatterAnnotation());
        }
Exemple #2
0
        private static ConditionalExpressionSyntax CreateMultilineConditionalExpression(ConditionalExpressionSyntax conditionalExpression)
        {
            SyntaxTriviaList triviaList = SyntaxUtility.GetIndentTrivia(conditionalExpression.Parent).Add(CSharpFactory.IndentTrivia());

            triviaList = triviaList.Insert(0, CSharpFactory.NewLineTrivia());

            ParenthesizedExpressionSyntax condition = null;

            if (conditionalExpression.Condition.IsKind(SyntaxKind.ParenthesizedExpression))
            {
                condition = (ParenthesizedExpressionSyntax)conditionalExpression.Condition;
            }
            else
            {
                condition = ParenthesizedExpression(conditionalExpression.Condition.WithoutTrailingTrivia())
                            .WithCloseParenToken(CreateTokenWithTrailingNewLine(SyntaxKind.CloseParenToken));
            }

            return(ConditionalExpression(
                       condition.WithoutTrailingTrivia(),
                       conditionalExpression.WhenTrue.WithoutTrailingTrivia(),
                       conditionalExpression.WhenFalse.WithoutTrailingTrivia())
                   .WithQuestionToken(CreateToken(SyntaxKind.QuestionToken, triviaList))
                   .WithColonToken(CreateToken(SyntaxKind.ColonToken, triviaList)));
        }
        private static IEnumerable <SyntaxNodeOrToken> CreateNodesAndTokens(ParameterListSyntax list)
        {
            SyntaxTriviaList trivia = SyntaxUtility.GetIndentTrivia(list.Parent).Add(CSharpFactory.IndentTrivia());

            SeparatedSyntaxList <ParameterSyntax> .Enumerator en = list.Parameters.GetEnumerator();

            if (en.MoveNext())
            {
                yield return(en.Current.WithLeadingTrivia(trivia));

                while (en.MoveNext())
                {
                    yield return(Token(SyntaxKind.CommaToken).WithTrailingNewLine());

                    yield return(en.Current.WithLeadingTrivia(trivia));
                }
            }
        }
        private static async Task <Document> FormatOnMultipleLinesAsync(
            Document document,
            BinaryExpressionSyntax condition,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            SyntaxTriviaList triviaList = SyntaxFactory.TriviaList(CSharpFactory.NewLineTrivia())
                                          .AddRange(SyntaxUtility.GetIndentTrivia(condition))
                                          .Add(CSharpFactory.IndentTrivia());

            var rewriter = new BinaryExpressioneSyntaxRewriter(triviaList);

            var newCondition = (ExpressionSyntax)rewriter.Visit(condition);

            root = root.ReplaceNode(condition, newCondition);

            return(document.WithSyntaxRoot(root));
        }
        private static async Task <Document> FormatExpressionChainOnMultipleLinesAsync(
            Document document,
            ImmutableArray <MemberAccessExpressionSyntax> expressions,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            MemberAccessExpressionSyntax expression = expressions[0];

            SyntaxTriviaList triviaList = SyntaxUtility.GetIndentTrivia(expression).Add(CSharpFactory.IndentTrivia());

            triviaList = triviaList.Insert(0, CSharpFactory.NewLineTrivia());

            var rewriter = new ExpressionChainSyntaxRewriter(expressions, triviaList);

            SyntaxNode newNode = rewriter.Visit(expression)
                                 .WithFormatterAnnotation();

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

            return(document.WithSyntaxRoot(newRoot));
        }
Exemple #6
0
        private static IEnumerable <SyntaxNodeOrToken> CreateMultilineNodesAndTokens(AttributeArgumentListSyntax argumentList)
        {
            SyntaxTriviaList trivia = SyntaxUtility.GetIndentTrivia(argumentList.Parent).Add(CSharpFactory.IndentTrivia());

            SeparatedSyntaxList <AttributeArgumentSyntax> .Enumerator en = argumentList.Arguments.GetEnumerator();

            if (en.MoveNext())
            {
                yield return(en.Current
                             .TrimTrailingTrivia()
                             .WithLeadingTrivia(trivia));

                while (en.MoveNext())
                {
                    yield return(Token(SyntaxKind.CommaToken)
                                 .WithTrailingNewLine());

                    yield return(en.Current
                                 .TrimTrailingTrivia()
                                 .WithLeadingTrivia(trivia));
                }
            }
        }