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); }
private static void AnalyzeElseClause(SyntaxNodeAnalysisContext context) { var elseClause = (ElseClauseSyntax)context.Node; StatementSyntax statement = elseClause.Statement; if (!statement.IsKind(SyntaxKind.IfStatement)) { return; } SyntaxTriviaList trailingTrivia = elseClause.ElseKeyword.TrailingTrivia; if (!SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(trailingTrivia)) { return; } if (!statement.GetLeadingTrivia().IsEmptyOrWhitespace()) { return; } DiagnosticHelpers.ReportDiagnostic( context, DiagnosticDescriptors.RemoveNewLineBetweenIfKeywordAndElseKeyword, Location.Create(elseClause.SyntaxTree, new TextSpan(trailingTrivia.Last().SpanStart, 0))); }
private static void AnalyzeArrowExpressionClause(SyntaxNodeAnalysisContext context) { var arrowExpressionClause = (ArrowExpressionClauseSyntax)context.Node; SyntaxToken arrowToken = arrowExpressionClause.ArrowToken; SyntaxToken previousToken = arrowToken.GetPreviousToken(); if (!SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(previousToken.TrailingTrivia)) { return; } if (arrowToken.LeadingTrivia.Any()) { return; } if (!SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(arrowToken.TrailingTrivia)) { return; } if (!SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(arrowExpressionClause.Expression.GetLeadingTrivia())) { return; } context.ReportDiagnostic(DiagnosticDescriptors.AddNewLineBeforeExpressionBodyArrowInsteadOfAfterIt, arrowToken); }
public static FormattingSuggestion AnalyzeNewLineBeforeOrAfter( SyntaxToken token, ExpressionSyntax expression, NewLinePosition newLinePosition) { SyntaxToken previousToken = token.GetPreviousToken(); if (newLinePosition == NewLinePosition.Before && SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(previousToken.TrailingTrivia)) { if (!token.LeadingTrivia.Any() && SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(token.TrailingTrivia) && SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(expression.GetLeadingTrivia())) { return(FormattingSuggestion.AddNewLineBefore); } } else if (newLinePosition == NewLinePosition.After && SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(previousToken.TrailingTrivia)) { if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(token.LeadingTrivia) && SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(token.TrailingTrivia) && !expression.GetLeadingTrivia().Any()) { return(FormattingSuggestion.AddNewLineAfter); } } return(FormattingSuggestion.None); }
private static void AnalyzeDoStatement(SyntaxNodeAnalysisContext context) { var doStatement = (DoStatementSyntax)context.Node; StatementSyntax statement = doStatement.Statement; if (!statement.IsKind(SyntaxKind.Block)) { return; } SyntaxTriviaList trailingTrivia = statement.GetTrailingTrivia(); if (!SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(trailingTrivia)) { return; } if (!doStatement.WhileKeyword.LeadingTrivia.IsEmptyOrWhitespace()) { return; } context.ReportDiagnostic( DiagnosticDescriptors.RemoveNewLineBetweenClosingBraceAndWhileKeyword, Location.Create(doStatement.SyntaxTree, new TextSpan(trailingTrivia.Last().SpanStart, 0))); }
private static void AnalyzeDoStatement(SyntaxNodeAnalysisContext context) { var doStatement = (DoStatementSyntax)context.Node; StatementSyntax statement = doStatement.Statement; if (!statement.IsKind(SyntaxKind.Block)) { return; } NewLineStyle newLineStyle = context.GetNewLineBeforeWhileInDoStatement(); if (newLineStyle == NewLineStyle.None) { return; } SyntaxTriviaList trailingTrivia = statement.GetTrailingTrivia(); if (!trailingTrivia.Any() || trailingTrivia.SingleOrDefault(shouldThrow: false).IsWhitespaceTrivia()) { if (!doStatement.WhileKeyword.LeadingTrivia.Any() && newLineStyle == NewLineStyle.Add) { context.ReportDiagnostic( DiagnosticRules.AddOrRemoveNewLineBeforeWhileInDoStatement, Location.Create(doStatement.SyntaxTree, new TextSpan(statement.FullSpan.End, 0)), "Add"); } } else if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(trailingTrivia)) { if (doStatement.WhileKeyword.LeadingTrivia.IsEmptyOrWhitespace() && newLineStyle == NewLineStyle.Remove) { context.ReportDiagnostic( DiagnosticRules.AddOrRemoveNewLineBeforeWhileInDoStatement, Location.Create(doStatement.SyntaxTree, new TextSpan(trailingTrivia.Last().SpanStart, 0)), properties: DiagnosticProperties.AnalyzerOption_Invert, "Remove"); } } }
private static void Analyze(SyntaxNodeAnalysisContext context, BaseListSyntax baseList, SyntaxToken previousToken) { SyntaxTriviaList trailingTrivia = previousToken.TrailingTrivia; if (!SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(trailingTrivia)) { return; } if (!baseList.ColonToken.LeadingTrivia.IsEmptyOrWhitespace()) { return; } context.ReportDiagnostic( DiagnosticDescriptors.RemoveNewLineBeforeBaseList, Location.Create(baseList.SyntaxTree, new TextSpan(trailingTrivia.Last().SpanStart, 0))); }
private static void AnalyzeDoStatement(SyntaxNodeAnalysisContext context) { var doStatement = (DoStatementSyntax)context.Node; StatementSyntax statement = doStatement.Statement; if (!statement.IsKind(SyntaxKind.Block)) { return; } SyntaxTriviaList trailingTrivia = statement.GetTrailingTrivia(); if (!trailingTrivia.Any() || trailingTrivia.SingleOrDefault(shouldThrow: false).IsWhitespaceTrivia()) { if (!doStatement.WhileKeyword.LeadingTrivia.Any() && !AnalyzerOptions.RemoveNewLineBetweenClosingBraceAndWhileKeyword.IsEnabled(context)) { context.ReportDiagnostic( DiagnosticRules.AddNewLineBetweenClosingBraceAndWhileKeywordOrViceVersa, Location.Create(doStatement.SyntaxTree, new TextSpan(statement.FullSpan.End, 0)), AnalyzerOptions.RemoveNewLineBetweenClosingBraceAndWhileKeyword); } } else if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(trailingTrivia)) { if (doStatement.WhileKeyword.LeadingTrivia.IsEmptyOrWhitespace() && AnalyzerOptions.RemoveNewLineBetweenClosingBraceAndWhileKeyword.IsEnabled(context)) { context.ReportDiagnostic( DiagnosticRules.ReportOnly.RemoveNewLineBetweenClosingBraceAndWhileKeyword, Location.Create(doStatement.SyntaxTree, new TextSpan(trailingTrivia.Last().SpanStart, 0)), properties: DiagnosticProperties.AnalyzerOption_Invert, AnalyzerOptions.RemoveNewLineBetweenClosingBraceAndWhileKeyword); } } }
private static void AnalyzeArrowExpressionClause(SyntaxNodeAnalysisContext context) { var arrowExpressionClause = (ArrowExpressionClauseSyntax)context.Node; SyntaxToken arrowToken = arrowExpressionClause.ArrowToken; SyntaxToken previousToken = arrowToken.GetPreviousToken(); if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(previousToken.TrailingTrivia)) { if (!arrowToken.LeadingTrivia.Any() && SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(arrowToken.TrailingTrivia) && SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(arrowExpressionClause.Expression.GetLeadingTrivia()) && context.IsAnalyzerSuppressed(AnalyzerOptions.AddNewLineAfterExpressionBodyArrowInsteadOfBeforeIt)) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticDescriptors.AddNewLineBeforeExpressionBodyArrowInsteadOfAfterItOrViceVersa, arrowToken.GetLocation()); } } else if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(previousToken.TrailingTrivia)) { if (SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(arrowToken.LeadingTrivia) && SyntaxTriviaAnalysis.IsEmptyOrSingleWhitespaceTrivia(arrowToken.TrailingTrivia) && !arrowExpressionClause.Expression.GetLeadingTrivia().Any() && !context.IsAnalyzerSuppressed(AnalyzerOptions.AddNewLineAfterExpressionBodyArrowInsteadOfBeforeIt)) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticDescriptors.ReportOnly.AddNewLineAfterExpressionBodyArrowInsteadOfBeforeIt, arrowToken.GetLocation(), properties: DiagnosticProperties.AnalyzerOption_Invert); } } }