Exemplo n.º 1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

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

            Debug.Assert(type != null, $"{nameof(type)} is null");

            if (type == null)
            {
                return;
            }

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            GenericNameSyntax newType = UseGenericEventHandlerRefactoring.CreateGenericEventHandler(type, semanticModel, context.CancellationToken);

            CodeAction codeAction = CodeAction.Create(
                $"Use '{newType}'",
                cancellationToken => UseGenericEventHandlerRefactoring.RefactorAsync(context.Document, type, cancellationToken),
                DiagnosticIdentifiers.UseGenericEventHandler + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        public override void Initialize(AnalysisContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            base.Initialize(context);

            context.RegisterSymbolAction(f => UseGenericEventHandlerRefactoring.AnalyzeEvent(f), SymbolKind.Event);
        }
Exemplo n.º 3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out TypeSyntax type))
            {
                return;
            }

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            GenericNameSyntax newType = UseGenericEventHandlerRefactoring.CreateGenericEventHandler(type, semanticModel, context.CancellationToken);

            CodeAction codeAction = CodeAction.Create(
                $"Use '{newType}'",
                cancellationToken => UseGenericEventHandlerRefactoring.RefactorAsync(context.Document, type, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.UseGenericEventHandler));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }