Пример #1
0
        private static void AnalyzeBraces(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement)
        {
            if (!ifStatement.IsParentKind(SyntaxKind.ElseClause) &&
                ifStatement.Else != null)
            {
                BracesAnalysisResult result = CSharpAnalysis.AnalyzeBraces(ifStatement);

                if ((result & BracesAnalysisResult.AddBraces) != 0)
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.AddBracesToIfElse, ifStatement);
                }

                if ((result & BracesAnalysisResult.RemoveBraces) != 0)
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.RemoveBracesFromIfElse, ifStatement);

                    foreach (SyntaxNode node in ifStatement.DescendantNodes())
                    {
                        if (node.IsKind(SyntaxKind.Block))
                        {
                            context.ReportBraces(DiagnosticDescriptors.RemoveBracesFromIfElseFadeOut, (BlockSyntax)node);
                        }
                    }
                }
            }
        }
Пример #2
0
        private void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            if (!ifStatement.IsParentKind(SyntaxKind.ElseClause) &&
                ifStatement.Else != null)
            {
                BracesAnalysisResult result = CSharpAnalysis.AnalyzeBraces(ifStatement);

                if ((result & BracesAnalysisResult.AddBraces) != 0)
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.AddBracesToIfElseWhenExpressionSpansOverMultipleLines, ifStatement);
                }

                if ((result & BracesAnalysisResult.RemoveBraces) != 0)
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.RemoveBracesFromIfElse, ifStatement);

                    foreach (SyntaxNode node in ifStatement.DescendantNodes())
                    {
                        if (node.IsKind(SyntaxKind.Block))
                        {
                            context.ReportBraces(DiagnosticDescriptors.RemoveBracesFromIfElseFadeOut, (BlockSyntax)node);
                        }
                    }
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (SelectedStatementsRefactoring.IsAnyRefactoringEnabled(context))
            {
                StatementContainerSelection selectedStatements;
                if (StatementContainerSelection.TryCreate(switchSection, context.Span, out 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;

                switch (CSharpAnalysis.AnalyzeBraces(switchSection))
                {
                case BracesAnalysisResult.AddBraces:
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddBracesToSwitchSection))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken));
                    }

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

                    break;
                }

                case BracesAnalysisResult.RemoveBraces:
                {
                    if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveBracesFromSwitchSection))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionRefactoring.RefactorAsync(context.Document, switchSection, cancellationToken));
                    }

                    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));
                    }

                    break;
                }
                }
            }
        }
        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)
            {
                SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

                if (sections.Any())
                {
                    var selectedSections = new SelectedNodeCollection <SwitchSectionSyntax>(sections, context.Span);

                    if (selectedSections.Any())
                    {
                        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;
                                }

                                switch (CSharpAnalysis.AnalyzeBraces(section))
                                {
                                case BracesAnalysisResult.AddBraces:
                                {
                                    addBraces.Add(section);
                                    break;
                                }

                                case BracesAnalysisResult.RemoveBraces:
                                {
                                    removeBraces.Add(section);
                                    break;
                                }
                                }
                            }

                            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));
                                });
                            }

                            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));
                                });
                            }
                        }

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

                            if (selectedSections.IsMultiple)
                            {
                                title += "s";
                            }

                            context.RegisterRefactoring(
                                title,
                                cancellationToken =>
                            {
                                return(RemoveStatementsFromSwitchSectionsRefactoring.RefactorAsync(
                                           context.Document,
                                           switchStatement,
                                           selectedSections.ToImmutableArray(),
                                           cancellationToken));
                            });
                        }
                    }
                }
            }
        }