private static GenericInfo ToMultiLine(GenericInfo info)
        {
            SyntaxNode declaration = info.Node;
            SyntaxList <TypeParameterConstraintClauseSyntax> constraintClauses = info.ConstraintClauses;

            TypeParameterConstraintClauseSyntax first = constraintClauses.First();

            SyntaxToken previousToken = declaration.FindToken(first.FullSpan.Start - 1);

            declaration = declaration.ReplaceToken(previousToken, previousToken.WithTrailingTrivia(TriviaList(NewLine())));

            SyntaxTriviaList leadingTrivia = declaration
                                             .FindToken(declaration.SpanStart)
                                             .LeadingTrivia;

            SyntaxTriviaList trivia = IncreaseIndentation(leadingTrivia.LastOrDefault());

            int count = constraintClauses.Count;

            for (int i = 0; i < count; i++)
            {
                TypeParameterConstraintClauseSyntax newNode = constraintClauses[i].WithLeadingTrivia(trivia);

                if (i < count - 1)
                {
                    newNode = newNode.WithTrailingTrivia(NewLine());
                }

                constraintClauses = constraintClauses.ReplaceAt(i, newNode);
            }

            return(SyntaxInfo.GenericInfo(declaration).WithConstraintClauses(constraintClauses));
        }
예제 #2
0
        private static SyntaxNode ToMultiLine(SyntaxNode node, SyntaxList <TypeParameterConstraintClauseSyntax> constraintClauses)
        {
            TypeParameterConstraintClauseSyntax first = constraintClauses.First();

            SyntaxToken previousToken = node.FindToken(first.FullSpan.Start - 1);

            node = node.ReplaceToken(previousToken, previousToken.WithTrailingTrivia(TriviaList(NewLine())));

            SyntaxTriviaList leadingTrivia = node
                                             .FindToken(node.SpanStart)
                                             .LeadingTrivia;

            SyntaxTriviaList trivia = IncreaseIndentation(leadingTrivia.LastOrDefault());

            int count = constraintClauses.Count;

            for (int i = 0; i < count; i++)
            {
                TypeParameterConstraintClauseSyntax newNode = constraintClauses[i].WithLeadingTrivia(trivia);

                if (i < count - 1)
                {
                    newNode = newNode.WithTrailingTrivia(NewLine());
                }

                constraintClauses = constraintClauses.ReplaceAt(i, newNode);
            }

            return(WithConstraintClauses(node, constraintClauses));
        }
예제 #3
0
        private static Task <Document> AddCallToConfigureAwaitRefactorAsync(
            Document document,
            AwaitExpressionSyntax awaitExpression,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax expression = awaitExpression.Expression;

            SyntaxTriviaList leading = default;

            switch (expression.Kind())
            {
            case SyntaxKind.SimpleMemberAccessExpression:
            {
                var memberAccess = (MemberAccessExpressionSyntax)expression;

                leading = memberAccess.OperatorToken.LeadingTrivia;
                break;
            }

            case SyntaxKind.InvocationExpression:
            {
                var invocation = (InvocationExpressionSyntax)expression;

                SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(invocation);

                leading = invocationInfo.OperatorToken.LeadingTrivia;
                break;
            }
            }

            SyntaxTrivia last = leading.LastOrDefault();

            last = (last.IsWhitespaceTrivia()) ? last : default;

            ParenthesizedExpressionSyntax expression2 = expression.WithoutTrailingTrivia().Parenthesize();

            if (last.IsWhitespaceTrivia())
            {
                expression2 = expression2.WithTrailingTrivia(SyntaxTriviaAnalysis.DetermineEndOfLine(awaitExpression));
            }

            InvocationExpressionSyntax newInvocationExpression = InvocationExpression(
                SimpleMemberAccessExpression(
                    expression2,
                    Token(SyntaxKind.DotToken).WithLeadingTrivia(last),
                    IdentifierName("ConfigureAwait")),
                ArgumentList(
                    Token(SyntaxKind.OpenParenToken),
                    SingletonSeparatedList(Argument(FalseLiteralExpression())),
                    Token(default, SyntaxKind.CloseParenToken, expression.GetTrailingTrivia())));
        private static void AnalyzeEnumDeclaration(SyntaxNodeAnalysisContext context)
        {
            var enumDeclaration = (EnumDeclarationSyntax)context.Node;

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

            int count = members.Count;

            if (count <= 1)
            {
                return;
            }

            SyntaxTree tree = context.Node.SyntaxTree;

            bool?isPrevSingleLine = null;

            for (int i = 1; i < count; i++)
            {
                SyntaxToken commaToken = members.GetSeparator(i - 1);

                SyntaxTriviaList trailingTrivia = commaToken.TrailingTrivia;

                SyntaxTrivia lastTrailingTrivia = trailingTrivia.LastOrDefault();

                if (!lastTrailingTrivia.IsKind(SyntaxKind.EndOfLineTrivia))
                {
                    isPrevSingleLine = false;
                    continue;
                }

                EnumMemberDeclarationSyntax member = members[i];

                SyntaxTrivia documentationCommentTrivia = member.GetDocumentationCommentTrivia();

                bool hasDocumentationComment = !documentationCommentTrivia.IsKind(SyntaxKind.None);

                if (!hasDocumentationComment)
                {
                    bool isSingleLine = tree.IsSingleLineSpan(member.Span, context.CancellationToken);

                    if (isSingleLine)
                    {
                        if (isPrevSingleLine == null)
                        {
                            isPrevSingleLine = tree.IsSingleLineSpan(TextSpan.FromBounds(members[i - 1].SpanStart, commaToken.Span.End), context.CancellationToken);
                        }

                        if (isPrevSingleLine == true)
                        {
                            isPrevSingleLine = isSingleLine;
                            continue;
                        }
                    }

                    isPrevSingleLine = isSingleLine;
                }
                else
                {
                    isPrevSingleLine = null;
                }

                if (member
                    .GetLeadingTrivia()
                    .FirstOrDefault()
                    .IsKind(SyntaxKind.EndOfLineTrivia))
                {
                    continue;
                }

                int end = (hasDocumentationComment) ? documentationCommentTrivia.SpanStart : member.SpanStart;

                if (tree.GetLineCount(TextSpan.FromBounds(commaToken.Span.End, end), context.CancellationToken) == 2)
                {
                    DiagnosticHelpers.ReportDiagnostic(context,
                                                       DiagnosticDescriptors.AddEmptyLineBetweenDeclarations,
                                                       lastTrailingTrivia);
                }
            }
        }
예제 #5
0
 private SyntaxTriviaList EnsureEndsLine(SyntaxTriviaList trivia) =>
 trivia.LastOrDefault().IsKind(SyntaxKind.EndOfLineTrivia) ? trivia : trivia.Add(SyntaxFactory.EndOfLine(Environment.NewLine));
        private static void AnalyzeCompilationUnit(SyntaxNodeAnalysisContext context)
        {
            var compilationUnit = (CompilationUnitSyntax)context.Node;

            SyntaxToken endOfFile = compilationUnit.EndOfFileToken;

            SyntaxTriviaList.Reversed.Enumerator en = endOfFile.LeadingTrivia.Reverse().GetEnumerator();

            bool?preferNewLineAtEndOfFile = context.PreferNewLineAtEndOfFile();

            if (preferNewLineAtEndOfFile == null)
            {
                return;
            }

            if (preferNewLineAtEndOfFile == false)
            {
                if (en.MoveNext() &&
                    (!en.Current.IsWhitespaceTrivia() ||
                     en.MoveNext()))
                {
                    if (en.Current.IsEndOfLineTrivia())
                    {
                        ReportDiagnostic(context, endOfFile);
                    }
                    else if (SyntaxFacts.IsPreprocessorDirective(en.Current.Kind()) &&
                             en.Current.GetStructure() is DirectiveTriviaSyntax directiveTrivia &&
                             directiveTrivia.GetTrailingTrivia().LastOrDefault().IsEndOfLineTrivia())
                    {
                        ReportDiagnostic(context, endOfFile);
                    }
                }
                else
                {
                    SyntaxTriviaList trailing = endOfFile.GetPreviousToken().TrailingTrivia;

                    if (trailing.Any())
                    {
                        Debug.Assert(endOfFile.FullSpan.Start == trailing.Span.End);

                        if (endOfFile.FullSpan.Start == trailing.Span.End &&
                            trailing.LastOrDefault().IsEndOfLineTrivia())
                        {
                            ReportDiagnostic(context, endOfFile);
                        }
                    }
                }
            }
            else if (en.MoveNext())
            {
                if (CSharpFacts.IsCommentTrivia(en.Current.Kind()) ||
                    SyntaxFacts.IsPreprocessorDirective(en.Current.Kind()))
                {
                    ReportDiagnostic(context, endOfFile);
                }
                else if (en.Current.IsWhitespaceOrEndOfLineTrivia() &&
                         endOfFile.LeadingTrivia.Span.Start == 0)
                {
                    while (en.MoveNext())
                    {
                        if (!en.Current.IsWhitespaceOrEndOfLineTrivia())
                        {
                            return;
                        }
                    }

                    ReportDiagnostic(context, endOfFile);
                }
            }
            else if (endOfFile.SpanStart > 0)
            {
                SyntaxTriviaList trailing = endOfFile.GetPreviousToken().TrailingTrivia;

                if (!trailing.Any())
                {
                    ReportDiagnostic(context, endOfFile);
                }
                else
                {
                    Debug.Assert(endOfFile.FullSpan.Start == trailing.Span.End);

                    if (endOfFile.FullSpan.Start == trailing.Span.End &&
                        !trailing.Last().IsEndOfLineTrivia())
                    {
                        ReportDiagnostic(context, endOfFile);
                    }
                }
            }