Esempio n. 1
0
            protected override async Task <Solution> GetChangedSolutionAsync(CancellationToken cancellationToken)
            {
                var updatedPublicSurfaceAreaText = new List <KeyValuePair <DocumentId, SourceText> >();

                foreach (KeyValuePair <Project, ImmutableArray <Diagnostic> > pair in _diagnosticsToFix)
                {
                    Project      project           = pair.Key;
                    TextDocument?shippedDocument   = DeclarePublicApiFix.GetShippedDocument(project);
                    SourceText?  shippedSourceText = shippedDocument is null ? null : await shippedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);

                    if (shippedSourceText is object)
                    {
                        SourceText newShippedSourceText = AddNullableEnable(shippedSourceText);
                        updatedPublicSurfaceAreaText.Add(new KeyValuePair <DocumentId, SourceText>(shippedDocument !.Id, newShippedSourceText));
                    }
                }

                Solution newSolution = _solution;

                foreach (KeyValuePair <DocumentId, SourceText> pair in updatedPublicSurfaceAreaText)
                {
                    newSolution = newSolution.WithAdditionalDocumentText(pair.Key, pair.Value);
                }

                return(newSolution);
            }
            protected override async Task <Solution> GetChangedSolutionAsync(CancellationToken cancellationToken)
            {
                var updatedPublicSurfaceAreaText = new List <KeyValuePair <DocumentId, SourceText> >();

                using var uniqueShippedDocuments = PooledHashSet <string> .GetInstance();

                foreach (var project in _projectsToFix)
                {
                    TextDocument?shippedDocument = DeclarePublicApiFix.GetShippedDocument(project);
                    if (shippedDocument == null ||
                        shippedDocument.FilePath != null && !uniqueShippedDocuments.Add(shippedDocument.FilePath))
                    {
                        // Skip past duplicate shipped documents.
                        // Multi-tfm projects can likely share the same api files, and we want to avoid duplicate code fix application.
                        continue;
                    }

                    var shippedSourceText = await shippedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);

                    SourceText newShippedSourceText = AddNullableEnable(shippedSourceText);
                    updatedPublicSurfaceAreaText.Add(new KeyValuePair <DocumentId, SourceText>(shippedDocument !.Id, newShippedSourceText));
                }

                Solution newSolution = _solution;

                foreach (KeyValuePair <DocumentId, SourceText> pair in updatedPublicSurfaceAreaText)
                {
                    newSolution = newSolution.WithAdditionalDocumentText(pair.Key, pair.Value);
                }

                return(newSolution);
            }
Esempio n. 3
0
        public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            Project project = context.Document.Project;

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                TextDocument?document = DeclarePublicApiFix.GetShippedDocument(project);

                if (document != null)
                {
                    context.RegisterCodeFix(
                        new DeclarePublicApiFix.AdditionalDocumentChangeAction(
                            $"Add '#nullable enable' to public API",
                            c => GetFix(document, c)),
                        diagnostic);
                }
            }

            return(Task.CompletedTask);
        }
        public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            Project project = context.Document.Project;

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                string minimalSymbolName = diagnostic.Properties[DeclarePublicApiAnalyzer.MinimalNamePropertyBagKey];
                string publicSymbolName  = diagnostic.Properties[DeclarePublicApiAnalyzer.PublicApiNamePropertyBagKey];
                string publicSymbolNameWithNullability = diagnostic.Properties[DeclarePublicApiAnalyzer.PublicApiNameWithNullabilityPropertyBagKey];
                bool   isShippedDocument = diagnostic.Properties[DeclarePublicApiAnalyzer.PublicApiIsShippedPropertyBagKey] == "true";

                TextDocument?document = isShippedDocument ? DeclarePublicApiFix.GetShippedDocument(project) : DeclarePublicApiFix.GetUnshippedDocument(project);

                if (document != null)
                {
                    context.RegisterCodeFix(
                        new DeclarePublicApiFix.AdditionalDocumentChangeAction(
                            $"Annotate {minimalSymbolName} in public API",
                            c => GetFix(document, publicSymbolName, publicSymbolNameWithNullability, c)),
                        diagnostic);
                }
            }

            return(Task.CompletedTask);