Esempio n. 1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            SwitchStatementSyntax switchStatement,
            SwitchSectionSyntax[] sections,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            IEnumerable <SwitchSectionSyntax> newSections = switchStatement
                                                            .Sections
                                                            .Select(section =>
            {
                if ((sections == null || Array.IndexOf(sections, section) != -1) &&
                    RemoveBracesFromSwitchSectionRefactoring.CanRemoveBraces(section))
                {
                    var block = (BlockSyntax)section.Statements[0];
                    return(section.WithStatements(block.Statements));
                }
                else
                {
                    return(section);
                }
            });

            SwitchStatementSyntax newSwitchStatement = switchStatement
                                                       .WithSections(List(newSections))
                                                       .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(switchStatement, newSwitchStatement, cancellationToken).ConfigureAwait(false));
        }
        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 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.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);
            }
        }