Esempio n. 1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            SwitchStatementSyntax switchStatement,
            SwitchSectionSyntax[] sections,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            IEnumerable <SwitchSectionSyntax> newSections = switchStatement
                                                            .Sections
                                                            .Select(section =>
            {
                if ((sections == null || Array.IndexOf(sections, section) != -1) &&
                    SwitchStatementAnalysis.CanAddBraces(section))
                {
                    return(section.WithStatements(SingletonList <StatementSyntax>(Block(section.Statements))));
                }
                else
                {
                    return(section);
                }
            });

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

            SyntaxNode newRoot = root.ReplaceNode(switchStatement, newSwitchStatement);

            return(document.WithSyntaxRoot(newRoot));
        }
Esempio n. 2
0
        private static async Task <Document> RemoveBracesFromSwitchSectionsAsync(
            Document document,
            SwitchStatementSyntax switchStatement,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            IEnumerable <SwitchSectionSyntax> newSections = switchStatement
                                                            .Sections
                                                            .Select(section =>
            {
                if (SwitchStatementAnalysis.CanRemoveBracesFromSection(section))
                {
                    var block = (BlockSyntax)section.Statements[0];
                    return(section.WithStatements(block.Statements));
                }
                else
                {
                    return(section);
                }
            });

            SwitchStatementSyntax newSwitchStatement = switchStatement
                                                       .WithSections(List(newSections))
                                                       .WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(switchStatement, newSwitchStatement);

            return(document.WithSyntaxRoot(newRoot));
        }
Esempio n. 3
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            SwitchStatementSyntax switchStatement = root
                                                    .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                    .FirstAncestorOrSelf <SwitchStatementSyntax>();

            if (switchStatement == null)
            {
                return;
            }

            if (switchStatement.Sections.Count > 0 &&
                switchStatement.SwitchKeyword.Span.Contains(context.Span))
            {
                SwitchStatementAnalysisResult result = SwitchStatementAnalysis.Analyze(switchStatement);

                if (result.CanAddBraces)
                {
                    context.RegisterRefactoring(
                        "Add braces to switch sections",
                        cancellationToken => AddBracesToSwitchSectionsAsync(context.Document, switchStatement, cancellationToken));
                }

                if (result.CanRemoveBraces)
                {
                    context.RegisterRefactoring(
                        "Remove braces from switch sections",
                        cancellationToken => RemoveBracesFromSwitchSectionsAsync(context.Document, switchStatement, cancellationToken));
                }

                if (switchStatement.Sections
                    .Any(section => !section.Labels.Contains(SyntaxKind.DefaultSwitchLabel)))
                {
                    context.RegisterRefactoring(
                        "Convert to if-else chain",
                        cancellationToken => ConvertSwitchToIfElseRefactoring.RefactorAsync(context.Document, switchStatement, cancellationToken));
                }
            }
        }
Esempio n. 4
0
        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 info = new SelectedNodesInfo <SwitchSectionSyntax>(sections, context.Span);

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

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

                                switch (SwitchStatementAnalysis.AnalyzeSection(section))
                                {
                                case SwitchSectionAnalysisResult.AddBraces:
                                {
                                    addBraces.Add(section);
                                    break;
                                }

                                case SwitchSectionAnalysisResult.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 (info.AreManySelected)
                            {
                                title += "s";
                            }

                            context.RegisterRefactoring(
                                title,
                                cancellationToken =>
                            {
                                return(RemoveStatementsFromSwitchSectionsRefactoring.RefactorAsync(
                                           context.Document,
                                           switchStatement,
                                           info.SelectedNodes().ToImmutableArray(),
                                           cancellationToken));
                            });
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (SelectedStatementsRefactoring.IsAnyRefactoringEnabled(context))
            {
                SelectedStatementsInfo info = SelectedStatementsInfo.Create(switchSection, context.Span);
                await SelectedStatementsRefactoring.ComputeRefactoringAsync(context, info).ConfigureAwait(false);
            }

            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 (SwitchStatementAnalysis.AnalyzeSection(switchSection))
                {
                case SwitchSectionAnalysisResult.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 && SwitchStatementAnalysis.CanAddBraces(f)))
                    {
                        context.RegisterRefactoring(
                            AddBracesToSwitchSectionsRefactoring.Title,
                            cancellationToken => AddBracesToSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken));
                    }

                    break;
                }

                case SwitchSectionAnalysisResult.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 && SwitchStatementAnalysis.CanRemoveBraces(f)))
                    {
                        context.RegisterRefactoring(
                            RemoveBracesFromSwitchSectionsRefactoring.Title,
                            cancellationToken => RemoveBracesFromSwitchSectionsRefactoring.RefactorAsync(context.Document, switchStatement, null, cancellationToken));
                    }

                    break;
                }
                }
            }
        }