Пример #1
0
        public static Task <Document> RefactorAsync(
            Document document,
            ConstructorDeclarationSyntax constructorDeclaration,
            CancellationToken cancellationToken)
        {
            ConstructorDeclarationSyntax newNode = AccessibilityHelper.ChangeAccessibility(constructorDeclaration, Accessibility.Protected, ModifierComparer.Instance);

            return(document.ReplaceNodeAsync(constructorDeclaration, newNode, cancellationToken));
        }
Пример #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.ChangeAccessibility))
            {
                return;
            }

            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(
                    root,
                    context.Span,
                    out SyntaxNode node,
                    predicate: f => OverrideInfo.CanCreate(f)))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case CompilerDiagnosticIdentifiers.CannotChangeAccessModifiersWhenOverridingInheritedMember:
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    OverrideInfo overrideInfo = OverrideInfo.Create(node, semanticModel, context.CancellationToken);

                    if (!overrideInfo.Success)
                    {
                        break;
                    }

                    Accessibility newAccessibility = overrideInfo.OverriddenSymbol.DeclaredAccessibility;

                    CodeAction codeAction = CodeAction.Create(
                        $"Change accessibility to '{AccessibilityHelper.GetAccessibilityName(newAccessibility)}'",
                        cancellationToken =>
                        {
                            if (node.Kind() == SyntaxKind.VariableDeclarator)
                            {
                                node = node.Parent.Parent;
                            }

                            SyntaxNode newNode = AccessibilityHelper.ChangeAccessibility(node, newAccessibility, ModifierComparer.Instance);

                            return(context.Document.ReplaceNodeAsync(node, newNode, cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Пример #3
0
        public static Task <Document> RefactorAsync(
            Document document,
            SyntaxNode node,
            Accessibility newAccessibility,
            CancellationToken cancellationToken)
        {
            SyntaxNode newNode = AccessibilityHelper.ChangeAccessibility(node, newAccessibility, ModifierComparer.Instance);

            return(document.ReplaceNodeAsync(node, newNode, cancellationToken));
        }
Пример #4
0
        public static Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationSyntax member,
            Accessibility accessibility,
            CancellationToken cancellationToken)
        {
            MemberDeclarationSyntax newNode = AccessibilityHelper.ChangeAccessibility(member, accessibility, ModifierComparer.Instance);

            return(document.ReplaceNodeAsync(member, newNode, cancellationToken));
        }
Пример #5
0
        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);
        }
Пример #6
0
        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));
        }