Exemple #1
0
        private static AttributeArgumentListSyntax ToMultiLine(AttributeArgumentListSyntax argumentList, CancellationToken cancellationToken = default)
        {
            SyntaxTriviaList leadingTrivia = SyntaxTriviaAnalysis.GetIncreasedIndentationTriviaList(argumentList, cancellationToken);

            var nodesAndTokens = new List <SyntaxNodeOrToken>();

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

            if (en.MoveNext())
            {
                nodesAndTokens.Add(en.Current
                                   .TrimTrailingTrivia()
                                   .WithLeadingTrivia(leadingTrivia));

                while (en.MoveNext())
                {
                    nodesAndTokens.Add(CommaToken().WithTrailingTrivia(NewLine()));

                    nodesAndTokens.Add(en.Current
                                       .TrimTrailingTrivia()
                                       .WithLeadingTrivia(leadingTrivia));
                }
            }

            return(AttributeArgumentList(
                       OpenParenToken().WithTrailingTrivia(NewLine()),
                       SeparatedList <AttributeArgumentSyntax>(nodesAndTokens),
                       argumentList.CloseParenToken.WithoutLeadingTrivia()));
        }
Exemple #2
0
        public static InitializerExpressionSyntax ToMultiLine(InitializerExpressionSyntax initializer, CancellationToken cancellationToken)
        {
            SyntaxNode parent = initializer.Parent;

            if (parent.IsKind(SyntaxKind.ObjectCreationExpression) &&
                !initializer.IsKind(SyntaxKind.CollectionInitializerExpression))
            {
                return(initializer
                       .WithExpressions(
                           SeparatedList(
                               initializer.Expressions.Select(expression => expression.WithLeadingTrivia(NewLine())))));
            }
            else
            {
                IndentationAnalysis indentationAnalysis = SyntaxTriviaAnalysis.AnalyzeIndentation(initializer, cancellationToken);

                SyntaxTriviaList braceTrivia      = TriviaList(NewLine(), indentationAnalysis.Indentation);
                SyntaxTriviaList expressionTrivia = TriviaList(NewLine(), indentationAnalysis.GetIncreasedIndentationTrivia());

                return(initializer
                       .WithExpressions(
                           SeparatedList(
                               initializer.Expressions.Select(expression => expression.WithLeadingTrivia(expressionTrivia))))
                       .WithOpenBraceToken(initializer.OpenBraceToken.WithLeadingTrivia(braceTrivia))
                       .WithCloseBraceToken(initializer.CloseBraceToken.WithLeadingTrivia(braceTrivia)));
            }
        }
Exemple #3
0
        public static ParameterListSyntax ToMultiLine(ParameterListSyntax parameterList, CancellationToken cancellationToken = default)
        {
            SyntaxTriviaList leadingTrivia = SyntaxTriviaAnalysis.GetIncreasedIndentationTriviaList(parameterList, cancellationToken);

            var nodesAndTokens = new List <SyntaxNodeOrToken>();

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

            if (en.MoveNext())
            {
                nodesAndTokens.Add(en.Current.WithLeadingTrivia(leadingTrivia));

                while (en.MoveNext())
                {
                    nodesAndTokens.Add(CommaToken().WithTrailingTrivia(NewLine()));

                    nodesAndTokens.Add(en.Current.WithLeadingTrivia(leadingTrivia));
                }
            }

            return(ParameterList(
                       OpenParenToken().WithTrailingTrivia(NewLine()),
                       SeparatedList <ParameterSyntax>(nodesAndTokens),
                       parameterList.CloseParenToken));
        }
Exemple #4
0
        public static IndentationAnalysis Create(SyntaxNode node, CancellationToken cancellationToken = default)
        {
            SyntaxTrivia indentation = SyntaxTriviaAnalysis.DetermineIndentation(node, cancellationToken);

            int size = SyntaxTriviaAnalysis.DetermineIndentationSize(node, cancellationToken);

            return(new IndentationAnalysis(indentation, size));
        }
Exemple #5
0
        public static ConditionalExpressionSyntax ToMultiLine(ConditionalExpressionSyntax conditionalExpression, CancellationToken cancellationToken = default)
        {
            SyntaxTriviaList leadingTrivia = SyntaxTriviaAnalysis.GetIncreasedIndentationTriviaList(conditionalExpression, cancellationToken);

            leadingTrivia = leadingTrivia.Insert(0, NewLine());

            return(ConditionalExpression(
                       conditionalExpression.Condition.WithoutTrailingTrivia(),
                       Token(leadingTrivia, SyntaxKind.QuestionToken, TriviaList(Space)),
                       conditionalExpression.WhenTrue.WithoutTrailingTrivia(),
                       Token(leadingTrivia, SyntaxKind.ColonToken, TriviaList(Space)),
                       conditionalExpression.WhenFalse.WithoutTrailingTrivia()));
        }
Exemple #6
0
        public static Task <Document> ToMultiLineAsync(
            Document document,
            BinaryExpressionSyntax condition,
            CancellationToken cancellationToken = default)
        {
            SyntaxTriviaList leadingTrivia = SyntaxTriviaAnalysis.GetIncreasedIndentationTriviaList(condition, cancellationToken);

            leadingTrivia = leadingTrivia.Insert(0, NewLine());

            var rewriter = new BinaryExpressionToMultiLineRewriter(leadingTrivia);

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

            return(document.ReplaceNodeAsync(condition, newCondition, cancellationToken));
        }
        public static IndentationAnalysis Create(SyntaxNode node, CancellationToken cancellationToken = default)
        {
            SyntaxTrivia indentation = SyntaxTriviaAnalysis.DetermineIndentation(node, cancellationToken);

            (SyntaxTrivia trivia1, SyntaxTrivia trivia2, bool isFromCompilationUnit) = DetermineSingleIndentation(node, cancellationToken);

            if (isFromCompilationUnit)
            {
                return(new IndentationAnalysis(indentation, trivia1.Span.Length - trivia2.Span.Length));
            }
            else if (indentation.Span.Length > 0)
            {
                return((trivia1.Span.Length > 0)
                    ? new IndentationAnalysis(indentation, trivia1.Span.Length)
                    : new IndentationAnalysis(indentation, indentation.Span.Length));
            }
            else if (trivia1.Span.Length > 0)
            {
                return(new IndentationAnalysis(trivia1, -1));
            }
            else
            {
                return(default);
Exemple #8
0
        public static TNode PrependEndOfLineToLeadingTrivia <TNode>(this TNode node) where TNode : SyntaxNode
        {
            SyntaxTrivia endOfLine = SyntaxTriviaAnalysis.GetEndOfLine(node);

            return(node.PrependToLeadingTrivia(endOfLine));
        }
Exemple #9
0
        public static SyntaxToken AppendEndOfLineToTrailingTrivia(this SyntaxToken token)
        {
            SyntaxTrivia endOfLine = SyntaxTriviaAnalysis.GetEndOfLine(token);

            return(token.AppendToTrailingTrivia(endOfLine));
        }
Exemple #10
0
        public static SyntaxToken PrependEndOfLineToLeadingTrivia(this SyntaxToken token)
        {
            SyntaxTrivia endOfLine = SyntaxTriviaAnalysis.GetEndOfLine(token);

            return(token.PrependToLeadingTrivia(endOfLine));
        }
        public static SyntaxToken PrependEndOfLineToTrailingTrivia(this SyntaxToken token)
        {
            SyntaxTrivia endOfLine = SyntaxTriviaAnalysis.DetermineEndOfLine(token);

            return(token.PrependToTrailingTrivia(endOfLine));
        }
        public static SyntaxToken AppendEndOfLineToLeadingTrivia(this SyntaxToken token)
        {
            SyntaxTrivia endOfLine = SyntaxTriviaAnalysis.DetermineEndOfLine(token);

            return(token.AppendToLeadingTrivia(endOfLine));
        }
        public static TNode AppendEndOfLineToTrailingTrivia <TNode>(this TNode node) where TNode : SyntaxNode
        {
            SyntaxTrivia endOfLine = SyntaxTriviaAnalysis.DetermineEndOfLine(node);

            return(node.AppendToTrailingTrivia(endOfLine));
        }
Exemple #14
0
        public static Task <Document> ToMultiLineAsync(
            Document document,
            ExpressionSyntax expression,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default)
        {
            IndentationAnalysis indentationAnalysis = SyntaxTriviaAnalysis.AnalyzeIndentation(expression, cancellationToken);

            string indentation = Environment.NewLine + indentationAnalysis.GetIncreasedIndentation();

            TextChange?       textChange  = null;
            List <TextChange> textChanges = null;

            foreach (SyntaxNode node in CSharpUtility.EnumerateExpressionChain(expression))
            {
                switch (node.Kind())
                {
                case SyntaxKind.SimpleMemberAccessExpression:
                {
                    var memberAccess = (MemberAccessExpressionSyntax)node;

                    if (memberAccess.Expression.IsKind(SyntaxKind.ThisExpression))
                    {
                        break;
                    }

                    if (semanticModel
                        .GetSymbol(memberAccess.Expression, cancellationToken)?
                        .Kind == SymbolKind.Namespace)
                    {
                        break;
                    }

                    AddTextChange(memberAccess.OperatorToken);
                    break;
                }

                case SyntaxKind.MemberBindingExpression:
                {
                    var memberBinding = (MemberBindingExpressionSyntax)node;

                    AddTextChange(memberBinding.OperatorToken);
                    break;
                }
                }
            }

            if (textChanges != null)
            {
                TextChange[] arr = textChanges.ToArray();
                Array.Reverse(arr);
                return(document.WithTextChangesAsync(arr, cancellationToken));
            }
            else
            {
                return(document.WithTextChangeAsync(textChange.Value, cancellationToken));
            }

            void AddTextChange(SyntaxToken operatorToken)
            {
                var tc = new TextChange(new TextSpan(operatorToken.SpanStart, 0), indentation);

                if (textChange == null)
                {
                    textChange = tc;
                }
                else
                {
                    (textChanges ??= new List <TextChange>()
                    {
                        textChange.Value
                    }).Add(tc);
                }
            }
        }