コード例 #1
0
            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 = PublicApiFixHelpers.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);
            }
コード例 #2
0
        private static SourceText AddNullableEnable(SourceText sourceText)
        {
            string     extraLine     = "#nullable enable" + PublicApiFixHelpers.GetEndOfLine(sourceText);
            SourceText newSourceText = sourceText.WithChanges(new TextChange(new TextSpan(0, 0), extraLine));

            return(newSourceText);
        }
コード例 #3
0
        public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            Project project = context.Document.Project;

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

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

            return(Task.CompletedTask);
        }
コード例 #4
0
        public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var project = context.Document.Project;
            var publicSurfaceAreaDocument = PublicApiFixHelpers.GetUnshippedDocument(project);

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                string minimalSymbolName           = diagnostic.Properties[DeclarePublicApiAnalyzer.MinimalNamePropertyBagKey];
                string publicSurfaceAreaSymbolName = diagnostic.Properties[DeclarePublicApiAnalyzer.PublicApiNamePropertyBagKey];
                ImmutableHashSet <string> siblingSymbolNamesToRemove = diagnostic.Properties[DeclarePublicApiAnalyzer.PublicApiNamesOfSiblingsToRemovePropertyBagKey]
                                                                       .Split(DeclarePublicApiAnalyzer.PublicApiNamesOfSiblingsToRemovePropertyBagValueSeparator.ToCharArray())
                                                                       .ToImmutableHashSet();

                context.RegisterCodeFix(
                    new AdditionalDocumentChangeAction(
                        $"Add {minimalSymbolName} to public API",
                        c => GetFixAsync(publicSurfaceAreaDocument, project, publicSurfaceAreaSymbolName, siblingSymbolNamesToRemove, c)),
                    diagnostic);
            }

            return(Task.CompletedTask);
        }
コード例 #5
0
        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 ? PublicApiFixHelpers.GetShippedDocument(project) : PublicApiFixHelpers.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);