public static void Analyze(SyntaxNodeAnalysisContext context, SwitchStatementSyntax switchStatement)
        {
            SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

            if (sections.Any())
            {
                AnalyzeStart(context, sections.First(), switchStatement.OpenBraceToken);
                AnalyzeEnd(context, sections.Last(), switchStatement.CloseBraceToken);

                if (sections.Count > 1)
                {
                    SwitchSectionSyntax prevSection = sections.First();

                    for (int i = 1; i < sections.Count; i++)
                    {
                        if (prevSection.Statements.LastOrDefault()?.IsKind(SyntaxKind.Block) == true)
                        {
                            SwitchSectionSyntax section = sections[i];

                            SyntaxTriviaList trailingTrivia = prevSection.GetTrailingTrivia();
                            SyntaxTriviaList leadingTrivia  = section.GetLeadingTrivia();

                            if (!IsStandardTriviaBetweenSections(trailingTrivia, leadingTrivia) &&
                                switchStatement
                                .SyntaxTree
                                .GetLineSpan(TextSpan.FromBounds(prevSection.Span.End, section.Span.Start), context.CancellationToken)
                                .GetLineCount() == 3)
                            {
                                SyntaxTrivia trivia = leadingTrivia
                                                      .SkipWhile(f => f.IsWhitespaceTrivia())
                                                      .FirstOrDefault();

                                if (trivia.IsEndOfLineTrivia() &&
                                    trailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) &&
                                    leadingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                                {
                                    context.ReportDiagnostic(
                                        DiagnosticDescriptors.RemoveRedundantEmptyLine,
                                        Location.Create(switchStatement.SyntaxTree, TextSpan.FromBounds(section.FullSpan.Start, trivia.Span.End)));
                                }
                            }
                        }

                        prevSection = sections[i];
                    }
                }
            }
        }
示例#2
0
            private SwitchExpressionArmSyntax ConstructSwitchArm(SwitchSectionSyntax section)
            {
                var isDefault = section
                                .Descendants <DefaultSwitchLabelSyntax>()
                                .Any();

                var returnStatement = section
                                      .Descendants <ReturnStatementSyntax>()
                                      .FirstOrDefault();

                if (returnStatement?.Expression == null)
                {
                    return(null);
                }

                PatternSyntax pattern;

                if (!isDefault)
                {
                    var labelExpression = section.Labels
                                          .FirstOrDefault()
                                          ?.DescendantsAndSelf <ExpressionSyntax>()
                                          .FirstOrDefault();

                    pattern = ConstantPattern(labelExpression);
                }
                else
                {
                    pattern = DiscardPattern();
                }

                var arm = SwitchExpressionArm(
                    pattern,
                    null,
                    Token(SyntaxKind.EqualsGreaterThanToken)
                    .WithLeadingTrivia(Space)
                    .WithTrailingTrivia(Space),
                    returnStatement.Expression)
                          .WithLeadingTrivia(section.GetLeadingTrivia());

                if (isDefault)
                {
                    arm = arm.WithTrailingTrivia(EndOfLine(Environment.NewLine));
                }

                return(arm);
            }
        public override SyntaxNode VisitSwitchSection(SwitchSectionSyntax node)
        {
            node = (SwitchSectionSyntax)base.VisitSwitchSection(node);

            var oldStatements = node.Statements;

            if (oldStatements.Count == 0) //empty case - used for fall-through
            {
                return(node);
            }

            var caseTrailing = node.GetTrailingTrivia();
            var caseLeading  = node.GetLeadingTrivia();
            var trueBlock    = ToBlockSyntax(oldStatements, caseLeading, caseTrailing);

            node = node.WithStatements(trueBlock);

            return(node);
        }
        private static SwitchStatementSyntax GetNewSwitchStatement(SwitchSectionSyntax switchSection, SwitchStatementSyntax switchStatement)
        {
            if (switchSection.GetLeadingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                int index = switchStatement.Sections.IndexOf(switchSection);

                if (index > 0)
                {
                    SwitchSectionSyntax previousSection = switchStatement.Sections[index - 1];

                    if (previousSection.GetTrailingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        SwitchStatementSyntax newSwitchStatement = switchStatement.RemoveNode(
                            switchSection,
                            SyntaxRemoveOptions.KeepNoTrivia);

                        previousSection = newSwitchStatement.Sections[index - 1];

                        return(newSwitchStatement.ReplaceNode(
                                   previousSection,
                                   previousSection.WithTrailingTrivia(switchSection.GetTrailingTrivia())));
                    }
                }
                else
                {
                    SyntaxToken openBrace = switchStatement.OpenBraceToken;

                    if (!openBrace.IsMissing &&
                        openBrace.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        return(switchStatement
                               .RemoveNode(switchSection, SyntaxRemoveOptions.KeepNoTrivia)
                               .WithOpenBraceToken(openBrace.WithTrailingTrivia(switchSection.GetTrailingTrivia())));
                    }
                }
            }

            return(switchStatement.RemoveNode(switchSection, SyntaxRemoveOptions.KeepExteriorTrivia));
        }
        public static void ComputeRefactoring(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (!context.Span.IsEmpty)
            {
                return;
            }

            SyntaxList <StatementSyntax> statements = switchSection.Statements;

            if (!statements.Any())
            {
                return;
            }

            if (statements.SingleOrDefault(shouldThrow: false) is BlockSyntax block &&
                block.CloseBraceToken.Span.Contains(context.Span))
            {
                RegisterRefactoring(context, switchSection);
            }

            if (!IsOnEmptyLine(context.Span, switchSection.GetLeadingTrivia()))
            {
                return;
            }

            var switchStatement = (SwitchStatementSyntax)switchSection.Parent;

            SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

            int index = sections.IndexOf(switchSection);

            if (index > 0)
            {
                SwitchSectionSyntax previousSection = sections[index - 1];
                RegisterRefactoring(context, previousSection, insertNewLine: true);
            }
        }