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

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out ObjectCreationExpressionSyntax objectCreationExpression))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.UseCSharp6DictionaryInitializer:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use C# 6.0 dictionary initializer",
                        cancellationToken => UseCSharp6DictionaryInitializerRefactoring.RefactorAsync(context.Document, objectCreationExpression.Initializer, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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

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

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

            if (objectCreationExpression == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.UseCSharp6DictionaryInitializer:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use C# 6.0 dictionary initializer",
                        cancellationToken => UseCSharp6DictionaryInitializerRefactoring.RefactorAsync(context.Document, objectCreationExpression.Initializer, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

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

            base.Initialize(context);

            context.RegisterSyntaxNodeAction(
                f => UseCSharp6DictionaryInitializerRefactoring.AnalyzeInitializerExpression(f),
                SyntaxKind.CollectionInitializerExpression);
        }
        private void AnalyzeAccessorList(SyntaxNodeAnalysisContext context)
        {
            var initializer = (InitializerExpressionSyntax)context.Node;

            UseCSharp6DictionaryInitializerRefactoring.Analyze(context, initializer);
        }