예제 #1
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out PropertyDeclarationSyntax property))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantAutoPropertyInitialization:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant initialization",
                        ct => RemoveRedundantAutoPropertyInitializationRefactoring.RefactorAsync(context.Document, property, ct),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

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

            if (property == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.UseAutoProperty:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use auto-property",
                        cancellationToken => UseAutoPropertyRefactoring.RefactorAsync(context.Document, property, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.RemoveRedundantAutoPropertyInitialization:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant initialization",
                        cancellationToken => RemoveRedundantAutoPropertyInitializationRefactoring.RefactorAsync(context.Document, property, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }