public static void ComputeRefactoring(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (!switchSection.IsParentKind(SyntaxKind.SwitchStatement))
            {
                return;
            }

            SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels;

            if (labels.Count <= 1)
            {
                return;
            }

            if (!SyntaxListSelection <SwitchLabelSyntax> .TryCreate(labels, context.Span, out SyntaxListSelection <SwitchLabelSyntax> selectedLabels))
            {
                return;
            }

            if (selectedLabels.Count == 1 &&
                (selectedLabels.First() == labels.Last()))
            {
                return;
            }

            context.RegisterRefactoring(
                "Split labels",
                ct => RefactorAsync(context.Document, switchSection, selectedLabels, ct),
                RefactoringDescriptors.SplitSwitchLabels);
        }
        public static void ComputeRefactoring(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (switchSection.IsParentKind(SyntaxKind.SwitchStatement))
            {
                SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels;

                if (labels.Count > 1 &&
                    SyntaxListSelection <SwitchLabelSyntax> .TryCreate(labels, context.Span, out SyntaxListSelection <SwitchLabelSyntax> selection))
                {
                    if (selection.Count > 1 || (selection.First() != labels.Last()))
                    {
                        SwitchLabelSyntax[] selectedLabels = selection.ToArray();

                        if (selectedLabels.Last() == labels.Last())
                        {
                            selectedLabels = selectedLabels.Take(selectedLabels.Length - 1).ToArray();
                        }

                        context.RegisterRefactoring(
                            "Split labels",
                            cancellationToken => RefactorAsync(context.Document, switchSection, selectedLabels, cancellationToken));
                    }
                }
            }
        }
        public static void ComputeRefactoring(RefactoringContext context, SwitchSectionSyntax switchSection)
        {
            if (switchSection.IsParentKind(SyntaxKind.SwitchStatement))
            {
                SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels;

                if (labels.Count > 1)
                {
                    var nodes = new SelectedNodeCollection <SwitchLabelSyntax>(labels, context.Span);

                    if (nodes.Any())
                    {
                        if (nodes.IsMultiple || (nodes.First != labels.Last()))
                        {
                            SwitchLabelSyntax[] selectedLabels = nodes.ToArray();

                            if (selectedLabels.Last() == labels.Last())
                            {
                                selectedLabels = selectedLabels.Take(selectedLabels.Length - 1).ToArray();
                            }

                            context.RegisterRefactoring(
                                "Split labels",
                                cancellationToken => RefactorAsync(context.Document, switchSection, selectedLabels, cancellationToken));
                        }
                    }
                }
            }
        }
 public static void Analyze(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection)
 {
     if (switchSection.IsParentKind(SyntaxKind.SwitchStatement) &&
         switchSection.Labels.Any(SyntaxKind.DefaultSwitchLabel) &&
         ContainsOnlyBreakStatement(switchSection) &&
         switchSection
         .DescendantTrivia(switchSection.Span)
         .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
     {
         context.ReportDiagnostic(
             DiagnosticDescriptors.RemoveRedundantDefaultSwitchSection,
             switchSection);
     }
 }
示例#5
0
        public static void Analyze(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection)
        {
            if (switchSection.IsParentKind(SyntaxKind.SwitchStatement))
            {
                SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels;

                if (labels.Count > 1 &&
                    labels.Any(SyntaxKind.DefaultSwitchLabel))
                {
                    foreach (SwitchLabelSyntax label in labels)
                    {
                        if (!label.IsKind(SyntaxKind.DefaultSwitchLabel) &&
                            label
                            .DescendantTrivia(label.Span)
                            .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                        {
                            context.ReportDiagnostic(
                                DiagnosticDescriptors.RemoveUnnecessaryCaseLabel,
                                label);
                        }
                    }
                }
            }
        }