public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxToken modifier) { SyntaxNode node = modifier.Parent; if (!node.IsKind(SyntaxKind.DestructorDeclaration)) { AccessibilityInfo info = AccessibilityInfo.Create(node.GetModifiers()); if (node.IsKind( SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); var symbol = (INamedTypeSymbol)semanticModel.GetDeclaredSymbol(node, context.CancellationToken); ImmutableArray <SyntaxReference> syntaxReferences = symbol.DeclaringSyntaxReferences; if (syntaxReferences.Length > 1) { ImmutableArray <MemberDeclarationSyntax> memberDeclarations = ImmutableArray.CreateRange( syntaxReferences, f => (MemberDeclarationSyntax)f.GetSyntax(context.CancellationToken)); foreach (Accessibility accessibility in _accessibilities) { if (accessibility != info.Accessibility && CSharpUtility.IsAllowedAccessibility(node, accessibility)) { context.RegisterRefactoring( GetTitle(accessibility), cancellationToken => ChangeAccessibilityRefactoring.RefactorAsync(context.Solution, memberDeclarations, accessibility, cancellationToken)); } } return; } } foreach (Accessibility accessibility in _accessibilities) { if (accessibility != info.Accessibility && CSharpUtility.IsAllowedAccessibility(node, accessibility)) { context.RegisterRefactoring( GetTitle(accessibility), cancellationToken => ChangeAccessibilityRefactoring.RefactorAsync(context.Document, node, accessibility, cancellationToken)); } } } }
public static async Task <Solution> RefactorAsync( Solution solution, ImmutableArray <MemberDeclarationSyntax> memberDeclarations, Accessibility newAccessibility, CancellationToken cancellationToken) { var newDocuments = new List <KeyValuePair <DocumentId, SyntaxNode> >(); foreach (IGrouping <SyntaxTree, MemberDeclarationSyntax> grouping in memberDeclarations .GroupBy(f => f.SyntaxTree)) { Document document = solution.GetDocument(grouping.Key); SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SyntaxNode newRoot = root.ReplaceNodes( grouping, (node, rewrittenNode) => { AccessibilityInfo info = AccessibilityInfo.Create(node.GetModifiers()); if (info.Accessibility == Accessibility.NotApplicable) { return(node); } return(AccessibilityHelper.ChangeAccessibility(node, info, newAccessibility, ModifierComparer.Instance)); }); newDocuments.Add(new KeyValuePair <DocumentId, SyntaxNode>(document.Id, newRoot)); } Solution newSolution = solution; foreach (KeyValuePair <DocumentId, SyntaxNode> kvp in newDocuments) { newSolution = newSolution.WithDocumentSyntaxRoot(kvp.Key, kvp.Value); } return(newSolution); }
public static Task <Solution> RefactorAsync( Solution solution, ImmutableArray <MemberDeclarationSyntax> memberDeclarations, Accessibility newAccessibility, CancellationToken cancellationToken) { return(solution.ReplaceNodesAsync( memberDeclarations, (node, rewrittenNode) => { AccessibilityInfo info = AccessibilityInfo.Create(node.GetModifiers()); if (info.Accessibility == Accessibility.NotApplicable) { return node; } return AccessibilityHelper.ChangeAccessibility(node, info, newAccessibility, ModifierComparer.Instance); }, cancellationToken)); }