Пример #1
0
        private static async Task <ImmutableArray <CodeAction> > GetAddReferencesCodeActionsAsync(CodeFixContext context, ISet <AssemblyIdentity> uniqueIdentities)
        {
            var result = ArrayBuilder <CodeAction> .GetInstance();

            foreach (var identity in uniqueIdentities)
            {
                var codeAction = await AddMissingReferenceCodeAction.CreateAsync(
                    context.Document.Project, identity, context.CancellationToken).ConfigureAwait(false);

                result.Add(codeAction);
            }

            return(result.ToImmutableAndFree());
        }
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var cancellationToken = context.CancellationToken;
            var root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var model = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var uniqueIdentities = new HashSet <AssemblyIdentity>();

            foreach (var diagnostic in context.Diagnostics)
            {
                var nodes   = FindNodes(root, diagnostic);
                var types   = GetTypesForNodes(model, nodes, cancellationToken).Distinct();
                var message = diagnostic.GetMessage();
                AssemblyIdentity identity = GetAssemblyIdentity(types, message);
                if (identity != null && !uniqueIdentities.Contains(identity) && !identity.Equals(model.Compilation.Assembly.Identity))
                {
                    uniqueIdentities.Add(identity);
                    context.RegisterCodeFix(
                        await AddMissingReferenceCodeAction.CreateAsync(context.Document.Project, identity, context.CancellationToken).ConfigureAwait(false),
                        diagnostic);
                }
            }
        }