Esempio n. 1
0
        public static FormattingSuggestion AnalyzeNewLineBeforeOrAfter(
            SyntaxNodeAnalysisContext context,
            SyntaxToken token,
            ExpressionSyntax expression,
            AnalyzerOptionDescriptor afterDescriptor)
        {
            SyntaxToken previousToken = token.GetPreviousToken();

            if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(previousToken.TrailingTrivia))
            {
                if (!token.LeadingTrivia.Any() &&
                    SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(token.TrailingTrivia) &&
                    SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(expression.GetLeadingTrivia()) &&
                    !afterDescriptor.IsEnabled(context))
                {
                    return(FormattingSuggestion.AddNewLineBefore);
                }
            }
            else if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(previousToken.TrailingTrivia))
            {
                if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(token.LeadingTrivia) &&
                    SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(token.TrailingTrivia) &&
                    !expression.GetLeadingTrivia().Any() &&
                    afterDescriptor.IsEnabled(context))
                {
                    return(FormattingSuggestion.AddNewLineAfter);
                }
            }

            return(FormattingSuggestion.None);
        }
Esempio n. 2
0
        private static void Analyze(
            SyntaxNodeAnalysisContext context,
            SyntaxToken token,
            SyntaxNode node,
            AnalyzerOptionDescriptor obsoleteAnalyzerOption = default)
        {
            SyntaxTriviaList trailingTrivia = token.TrailingTrivia;
            SyntaxTriviaList leadingTrivia  = node.GetLeadingTrivia();

            if (IsStandardTriviaBetweenLines(trailingTrivia, leadingTrivia))
            {
                return;
            }

            if (token
                .SyntaxTree
                .GetLineSpan(TextSpan.FromBounds(token.Span.End, node.SpanStart), context.CancellationToken)
                .GetLineCount() != 3)
            {
                return;
            }

            var trivia = default(SyntaxTrivia);

            foreach (SyntaxTrivia t in leadingTrivia)
            {
                if (!t.IsWhitespaceTrivia())
                {
                    trivia = t;
                    break;
                }
            }

            if (!trivia.IsEndOfLineTrivia())
            {
                return;
            }

            if (!trailingTrivia.IsEmptyOrWhitespace())
            {
                return;
            }

            if (!leadingTrivia.IsEmptyOrWhitespace())
            {
                return;
            }

            Location location = Location.Create(token.SyntaxTree, TextSpan.FromBounds(node.FullSpan.Start, trivia.Span.End));

            if (obsoleteAnalyzerOption.IsDefault)
            {
                DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.RemoveRedundantEmptyLine, location);
            }
            else
            {
                DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.RemoveRedundantEmptyLine, location, obsoleteAnalyzerOption);
            }
        }