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); }
private static Task <Document> RefactorAsync( Document document, SwitchSectionSyntax section, SyntaxListSelection <SwitchLabelSyntax> selectedLabels, CancellationToken cancellationToken) { SyntaxList <SwitchLabelSyntax> labels = section.Labels; int lastIndex = selectedLabels.LastIndex; if (selectedLabels.Last() == labels.Last()) { lastIndex--; } var switchStatement = (SwitchStatementSyntax)section.Parent; SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections; int index = sections.IndexOf(section); SyntaxList <SwitchSectionSyntax> newSections = sections .ReplaceAt(index, section.RemoveNodes(labels.Take(lastIndex + 1), SyntaxRemoveOptions.KeepNoTrivia)) .InsertRange(index, CreateSwitchSections(section, selectedLabels, lastIndex)); SwitchStatementSyntax newSwitchStatement = switchStatement.WithSections(newSections); return(document.ReplaceNodeAsync(switchStatement, newSwitchStatement, cancellationToken)); }
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 ComputeRefactorings(RefactoringContext context, MemberDeclarationSyntax member) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.SplitAttributes, RefactoringIdentifiers.MergeAttributes) && !member.IsKind(SyntaxKind.NamespaceDeclaration) && SyntaxListSelection <AttributeListSyntax> .TryCreate(member.GetAttributeLists(), context.Span, out SyntaxListSelection <AttributeListSyntax> selectedAttributeLists)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.SplitAttributes) && selectedAttributeLists.Any(f => f.Attributes.Count > 1)) { context.RegisterRefactoring( "Split attributes", ct => SplitAsync(context.Document, member, selectedAttributeLists.ToArray(), ct), RefactoringIdentifiers.SplitAttributes); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MergeAttributes) && selectedAttributeLists.Count > 1) { context.RegisterRefactoring( "Merge attributes", ct => MergeAsync(context.Document, member, selectedAttributeLists.ToArray(), ct), RefactoringIdentifiers.MergeAttributes); } } }
public static void ComputeRefactoring(RefactoringContext context, MemberDeclarationListSelection selectedMembers) { if (selectedMembers.Count <= 1) { return; } SyntaxListSelection <MemberDeclarationSyntax> .Enumerator en = selectedMembers.GetEnumerator(); if (!en.MoveNext()) { return; } TextSpan span = context.Span; string refactoringId = GetRefactoringId(en.Current); if (refactoringId == null) { return; } while (en.MoveNext()) { if (refactoringId != GetRefactoringId(en.Current)) { return; } } if (refactoringId == RefactoringIdentifiers.UseExpressionBodiedMember && context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember)) { context.RegisterRefactoring( UseExpressionBodiedMemberRefactoring.Title, ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, selectedMembers, ct), RefactoringIdentifiers.UseExpressionBodiedMember); } else if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandExpressionBody)) { context.RegisterRefactoring( ExpandExpressionBodyRefactoring.Title, ct => ExpandExpressionBodyRefactoring.RefactorAsync(context.Document, selectedMembers, ct), RefactoringIdentifiers.ExpandExpressionBody); } }
private static Task <Document> RefactorAsync( Document document, SyntaxListSelection <SwitchLabelSyntax> selectedLabels, IComparer <SwitchLabelSyntax> comparer, CancellationToken cancellationToken = default) { SyntaxList <SwitchLabelSyntax> labels = selectedLabels.UnderlyingList; SyntaxList <SwitchLabelSyntax> newLabels = labels.ReplaceRange( selectedLabels.FirstIndex, selectedLabels.Count, selectedLabels.OrderBy(f => f, comparer)); var section = (SwitchSectionSyntax)labels[0].Parent; SwitchSectionSyntax newSection = section.WithLabels(newLabels); return(document.ReplaceNodeAsync(section, newSection, cancellationToken)); }
private static IEnumerable <SwitchSectionSyntax> CreateSwitchSections( SwitchSectionSyntax section, SyntaxListSelection <SwitchLabelSyntax> selectedLabels, int lastIndex) { int firstIndex = selectedLabels.FirstIndex; if (firstIndex > 0) { yield return(SwitchSection(section.Labels.Take(firstIndex + 1).ToSyntaxList(), BreakStatement()) .WithFormatterAnnotation()); firstIndex++; } for (int i = firstIndex; i <= lastIndex; i++) { yield return(SwitchSection(selectedLabels.UnderlyingList[i], BreakStatement()) .WithFormatterAnnotation()); } }
public static void ComputeRefactoring( RefactoringContext context, SyntaxListSelection <SwitchLabelSyntax> selectedLabels) { SyntaxList <SwitchLabelSyntax> labels = selectedLabels.UnderlyingList; int firstIndex = selectedLabels.FirstIndex; if (!(labels[firstIndex] is CaseSwitchLabelSyntax label)) { return; } ExpressionSyntax value = label.Value; SyntaxKind kind = value.Kind(); if (kind == SyntaxKind.StringLiteralExpression) { string valueText = ((LiteralExpressionSyntax)value).Token.ValueText; for (int i = firstIndex + 1; i <= selectedLabels.LastIndex; i++) { if (!(labels[i] is CaseSwitchLabelSyntax label2)) { return; } if (!label2.Value.IsKind(SyntaxKind.StringLiteralExpression)) { return; } string valueText2 = ((LiteralExpressionSyntax)label2.Value).Token.ValueText; if (StringComparer.CurrentCulture.Compare(valueText, valueText2) > 0) { context.RegisterRefactoring( Title, ct => RefactorAsync(context.Document, selectedLabels, StringLiteralExpressionLabelComparer.Instance, ct), RefactoringIdentifiers.SortCaseLabels); return; } valueText = valueText2; } } else if (kind == SyntaxKind.SimpleMemberAccessExpression) { var memberAccess = (MemberAccessExpressionSyntax)value; string containingName = (memberAccess.Expression as IdentifierNameSyntax)?.Identifier.ValueText; if (containingName == null) { return; } string name = (memberAccess.Name as IdentifierNameSyntax)?.Identifier.ValueText; if (name == null) { return; } for (int i = firstIndex + 1; i <= selectedLabels.LastIndex; i++) { if (!(labels[i] is CaseSwitchLabelSyntax label2)) { return; } if (!label2.Value.IsKind(SyntaxKind.SimpleMemberAccessExpression)) { return; } var memberAccess2 = (MemberAccessExpressionSyntax)label2.Value; if (!StringComparer.CurrentCulture.Equals(containingName, (memberAccess2.Expression as IdentifierNameSyntax)?.Identifier.ValueText)) { return; } string name2 = (memberAccess2.Name as IdentifierNameSyntax)?.Identifier.ValueText; if (StringComparer.CurrentCulture.Compare(name, name2) > 0) { context.RegisterRefactoring( Title, ct => RefactorAsync(context.Document, selectedLabels, SimpleMemberAccessExpressionLabelComparer.Instance, ct), RefactoringIdentifiers.SortCaseLabels); return; } name = name2; } } }
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); } }
internal EnumeratorImpl(SyntaxListSelection <TNode> selection) { _en = new Enumerator(selection); }
internal Enumerator(SyntaxListSelection <TNode> selection) { _selection = selection; _index = -1; }
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); } }