private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            if (ifStatement.IsParentKind(SyntaxKind.ElseClause))
            {
                return;
            }

            if (ifStatement.Else == null)
            {
                return;
            }

            if (ifStatement.Else.ContainsDirectives)
            {
                return;
            }

            if (ifStatement.Statement.ContainsDirectives)
            {
                return;
            }

            BracesAnalysis analysis = BracesAnalysis.AnalyzeBraces(ifStatement);

            if (!analysis.AddBraces)
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.AddBracesToIfElseWhenExpressionSpansOverMultipleLines, ifStatement);
        }
        private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            if (ifStatement.IsParentKind(SyntaxKind.ElseClause))
            {
                return;
            }

            if (ifStatement.Else == null)
            {
                return;
            }

            BracesAnalysis analysis = BracesAnalysis.AnalyzeBraces(ifStatement);

            if (!analysis.RemoveBraces)
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveBracesFromIfElse, ifStatement);

            foreach (IfStatementOrElseClause ifOrElse in ifStatement.AsCascade())
            {
                if (ifOrElse.Statement is BlockSyntax block)
                {
                    CSharpDiagnosticHelpers.ReportBraces(context, DiagnosticDescriptors.RemoveBracesFromIfElseFadeOut, block);
                }
            }
        }
        public static void ComputeRefactorings(RefactoringContext context, SwitchStatementSyntax switchStatement)
        {
            bool fRemoveStatements = context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveStatementsFromSwitchSections);
            bool fAddBraces        = context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSections);
            bool fRemoveBraces     = context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSections);

            if (!fRemoveStatements && !fAddBraces && !fRemoveBraces)
            {
                return;
            }

            if (!SyntaxListSelection <SwitchSectionSyntax> .TryCreate(switchStatement.Sections, context.Span, out SyntaxListSelection <SwitchSectionSyntax> selectedSections))
            {
                return;
            }

            if (fAddBraces || fRemoveBraces)
            {
                var addBraces    = new List <SwitchSectionSyntax>();
                var removeBraces = new List <SwitchSectionSyntax>();

                foreach (SwitchSectionSyntax section in selectedSections)
                {
                    if (addBraces.Count > 0 &&
                        removeBraces.Count > 0)
                    {
                        break;
                    }

                    BracesAnalysis analysis = BracesAnalysis.AnalyzeBraces(section);

                    if (analysis.AddBraces)
                    {
                        addBraces.Add(section);
                    }
                    else if (analysis.RemoveBraces)
                    {
                        removeBraces.Add(section);
                    }
                }

                if (fAddBraces && addBraces.Count > 0)
                {
                    string title = AddBracesToSwitchSectionRefactoring.Title;

                    if (addBraces.Count > 1)
                    {
                        title += "s";
                    }

                    context.RegisterRefactoring(
                        title,
                        cancellationToken =>
                    {
                        return(AddBracesToSwitchSectionsRefactoring.RefactorAsync(
                                   context.Document,
                                   switchStatement,
                                   addBraces.ToArray(),
                                   cancellationToken));
                    },
                        RefactoringIdentifiers.AddBracesToSwitchSections);
                }

                if (fRemoveBraces &&
                    removeBraces.Count > 0)
                {
                    string title = RemoveBracesFromSwitchSectionRefactoring.Title;

                    if (removeBraces.Count > 1)
                    {
                        title += "s";
                    }

                    context.RegisterRefactoring(
                        title,
                        cancellationToken =>
                    {
                        return(RemoveBracesFromSwitchSectionsRefactoring.RefactorAsync(
                                   context.Document,
                                   switchStatement,
                                   removeBraces.ToArray(),
                                   cancellationToken));
                    },
                        RefactoringIdentifiers.RemoveBracesFromSwitchSections);
                }
            }

            if (fRemoveStatements)
            {
                string title = "Remove statements from section";

                if (selectedSections.Count > 1)
                {
                    title += "s";
                }

                context.RegisterRefactoring(
                    title,
                    cancellationToken =>
                {
                    return(RemoveStatementsFromSwitchSectionsRefactoring.RefactorAsync(
                               context.Document,
                               switchStatement,
                               selectedSections.ToImmutableArray(),
                               cancellationToken));
                },
                    RefactoringIdentifiers.RemoveStatementsFromSwitchSections);
            }
        }
Beispiel #4
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (SelectedStatementsRefactoring.IsAnyRefactoringEnabled(context) &&
                StatementListSelection.TryCreate(switchSection, context.Span, out StatementListSelection selectedStatements))
            {
                await SelectedStatementsRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitSwitchLabels))
            {
                SplitSwitchLabelsRefactoring.ComputeRefactoring(context, switchSection);
            }

            if (context.Span.IsEmpty &&
                context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddBracesToSwitchSection,
                    RefactoringIdentifiers.AddBracesToSwitchSections,
                    RefactoringIdentifiers.RemoveBracesFromSwitchSection,
                    RefactoringIdentifiers.RemoveBracesFromSwitchSections))
            {
                var switchStatement = (SwitchStatementSyntax)switchSection.Parent;

                SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

                BracesAnalysis analysis = BracesAnalysis.AnalyzeBraces(switchSection);

                if (analysis.AddBraces)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSection))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                            RefactoringIdentifiers.AddBracesToSwitchSection);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSections) &&
                        sections.Any(f => f != switchSection && AddBracesToSwitchSectionAnalysis.CanAddBraces(f)))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionsRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken),
                            RefactoringIdentifiers.AddBracesToSwitchSections);
                    }
                }
                else if (analysis.RemoveBraces)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSection))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                            RefactoringIdentifiers.RemoveBracesFromSwitchSection);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSections) &&
                        sections.Any(f => f != switchSection && RemoveBracesFromSwitchSectionRefactoring.CanRemoveBraces(f)))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionsRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken),
                            RefactoringIdentifiers.RemoveBracesFromSwitchSections);
                    }
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (SelectedStatementsRefactoring.IsAnyRefactoringEnabled(context) &&
                StatementListSelection.TryCreate(switchSection, context.Span, out StatementListSelection selectedStatements))
            {
                await SelectedStatementsRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SortCaseLabels) &&
                SyntaxListSelection <SwitchLabelSyntax> .TryCreate(switchSection.Labels, context.Span, out SyntaxListSelection <SwitchLabelSyntax> selectedLabels) &&
                selectedLabels.Count > 1)
            {
                SortCaseLabelsRefactoring.ComputeRefactoring(context, selectedLabels);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitSwitchLabels))
            {
                SplitSwitchLabelsRefactoring.ComputeRefactoring(context, switchSection);
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddBracesToSwitchSection,
                    RefactoringIdentifiers.AddBracesToSwitchSections,
                    RefactoringIdentifiers.RemoveBracesFromSwitchSection,
                    RefactoringIdentifiers.RemoveBracesFromSwitchSections) &&
                context.Span.IsEmpty &&
                IsContainedInCaseOrDefaultKeyword(context.Span))
            {
                var switchStatement = (SwitchStatementSyntax)switchSection.Parent;

                SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

                BracesAnalysis analysis = BracesAnalysis.AnalyzeBraces(switchSection);

                if (analysis.AddBraces)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSection))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                            RefactoringIdentifiers.AddBracesToSwitchSection);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSections) &&
                        sections.Any(f => f != switchSection && AddBracesToSwitchSectionAnalysis.CanAddBraces(f)))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionsRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken),
                            RefactoringIdentifiers.AddBracesToSwitchSections);
                    }
                }
                else if (analysis.RemoveBraces)
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSection))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken),
                            RefactoringIdentifiers.RemoveBracesFromSwitchSection);
                    }

                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSections) &&
                        sections.Any(f => f != switchSection && RemoveBracesFromSwitchSectionRefactoring.CanRemoveBraces(f)))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionsRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken),
                            RefactoringIdentifiers.RemoveBracesFromSwitchSections);
                    }
                }
            }

            bool IsContainedInCaseOrDefaultKeyword(TextSpan span)
            {
                foreach (SwitchLabelSyntax label in switchSection.Labels)
                {
                    if (label.Keyword.Span.Contains(span))
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }