internal TypeDocumentationModel(
     INamedTypeSymbol typeSymbol,
     DocumentationModel documentationModel)
 {
     Symbol             = typeSymbol;
     DocumentationModel = documentationModel;
 }
示例#2
0
        public void Append(DocumentationModel documentationModel)
        {
            if ((Options.IgnoredParts & DeclarationListParts.AssemblyAttributes) == 0)
            {
                foreach (IAssemblySymbol assembly in documentationModel.Assemblies
                         .OrderBy(f => f.Name)
                         .ThenBy(f => f.Identity.Version))
                {
                    AppendAssemblyAttributes(assembly);
                }
            }

            if (Options.NestNamespaces)
            {
                AppendWithNamespaceHierarchy(documentationModel);
            }
            else
            {
                IEnumerable <INamedTypeSymbol> types = documentationModel.Types.Where(f => f.ContainingType == null && !Options.ShouldBeIgnored(f));

                foreach (INamespaceSymbol namespaceSymbol in types
                         .Select(f => f.ContainingNamespace)
                         .Distinct(MetadataNameEqualityComparer <INamespaceSymbol> .Instance)
                         .OrderBy(f => f, NamespaceComparer))
                {
                    if (!namespaceSymbol.IsGlobalNamespace)
                    {
                        Append(namespaceSymbol, _namespaceFormat);
                        BeginTypeContent();
                    }

                    _currentNamespace = namespaceSymbol;

                    if (Options.Depth <= DocumentationDepth.Type)
                    {
                        AppendTypes(types.Where(f => MetadataNameEqualityComparer <INamespaceSymbol> .Instance.Equals(f.ContainingNamespace, namespaceSymbol)));
                    }

                    _currentNamespace = null;

                    if (!namespaceSymbol.IsGlobalNamespace)
                    {
                        EndTypeContent();
                        AppendLine();
                    }
                }
            }
        }
        private static void AppendAutoGeneratedComment(DocumentationModel documentationModel, StringBuilder sb)
        {
            sb.AppendLine("// --------------------------------------------------");
            sb.AppendLine("// <auto-generated>");
            sb.Append("//     This file was generated by ");
            sb.AppendLine(typeof(DeclarationListGenerator).GetTypeInfo().Assembly.FullName);
            sb.AppendLine("//     Assemblies:");

            foreach (IAssemblySymbol assembly in documentationModel.Assemblies
                     .OrderBy(f => f.Name)
                     .ThenBy(f => f.Identity.Version))
            {
                sb.Append("//         ");
                sb.AppendLine(assembly.Identity.ToString());
            }

            sb.AppendLine("// </auto-generated>");
            sb.AppendLine("// --------------------------------------------------");
            sb.AppendLine();
        }
        public static async Task <string> GenerateAsync(
            DocumentationModel documentationModel,
            DeclarationListOptions options = null,
            IComparer <INamespaceSymbol> namespaceComparer = null,
            IComparer <INamedTypeSymbol> typeComparer      = null,
            IComparer <ISymbol> memberComparer             = null,
            CancellationToken cancellationToken            = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            options = options ?? DeclarationListOptions.Default;

            var builder = new DeclarationListBuilder(
                options: options,
                namespaceComparer: namespaceComparer,
                typeComparer: typeComparer,
                memberComparer: memberComparer);

            builder.Append(documentationModel);

            StringBuilder sb = StringBuilderCache.GetInstance();

            if ((options.IgnoredParts & DeclarationListParts.AutoGeneratedComment) == 0)
            {
                AppendAutoGeneratedComment(documentationModel, sb);
            }

            foreach (INamespaceSymbol namespaceSymbol in builder.Namespaces.OrderBy(f => f, builder.NamespaceComparer))
            {
                cancellationToken.ThrowIfCancellationRequested();

                sb.Append("using ");
                sb.Append(namespaceSymbol.ToDisplayString(SymbolDisplayFormats.TypeNameAndContainingTypesAndNamespaces));
                sb.AppendLine(";");
            }

            if (builder.Namespaces.Count > 0)
            {
                sb.AppendLine();
            }

            sb.Append(builder);

            string content = sb.ToString();

            Project project = new AdhocWorkspace()
                              .CurrentSolution
                              .AddProject("AdHocProject", "AdHocProject", documentationModel.Language)
                              .WithMetadataReferences(documentationModel.Compilation.References);

            if (project.ParseOptions is CSharpParseOptions csharpParseOptions)
            {
                project = project.WithParseOptions(csharpParseOptions.WithLanguageVersion(LanguageVersion.Latest));
            }
            else
            {
                Debug.Fail(project.ParseOptions.GetType().FullName);
            }

            Document document = project.AddDocument("AdHocFile.cs", SourceText.From(content));

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var rewriter = new Rewriter(options, semanticModel, cancellationToken);

            root = rewriter.Visit(root);

            document = document.WithSyntaxRoot(root);

            document = await Simplifier.ReduceAsync(document, cancellationToken : cancellationToken).ConfigureAwait(false);

            root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            return(root.ToFullString());
        }
示例#5
0
        private void AppendWithNamespaceHierarchy(DocumentationModel documentationModel)
        {
            IEnumerable <INamedTypeSymbol> types = documentationModel.Types.Where(f => f.ContainingType == null && !Options.ShouldBeIgnored(f));

            var rootNamespaces = new HashSet <INamespaceSymbol>(MetadataNameEqualityComparer <INamespaceSymbol> .Instance);

            var nestedNamespaces = new HashSet <INamespaceSymbol>(MetadataNameEqualityComparer <INamespaceSymbol> .Instance);

            foreach (INamespaceSymbol namespaceSymbol in types.Select(f => f.ContainingNamespace))
            {
                if (namespaceSymbol.IsGlobalNamespace)
                {
                    rootNamespaces.Add(namespaceSymbol);
                }
                else
                {
                    INamespaceSymbol n = namespaceSymbol;

                    while (true)
                    {
                        INamespaceSymbol containingNamespace = n.ContainingNamespace;

                        if (containingNamespace.IsGlobalNamespace)
                        {
                            rootNamespaces.Add(n);
                            break;
                        }

                        nestedNamespaces.Add(n);

                        n = containingNamespace;
                    }
                }
            }

            foreach (INamespaceSymbol namespaceSymbol in rootNamespaces
                     .OrderBy(f => f, NamespaceComparer))
            {
                AppendNamespace(namespaceSymbol);
                AppendLine();
            }

            void AppendNamespace(INamespaceSymbol namespaceSymbol, bool isNested = false, bool startsWithNewLine = false)
            {
                if (!namespaceSymbol.IsGlobalNamespace)
                {
                    if (isNested)
                    {
                        if (startsWithNewLine)
                        {
                            AppendLine();
                        }

                        Append("// ");
                        Append(namespaceSymbol, SymbolDisplayFormats.TypeNameAndContainingTypesAndNamespaces);
                        AppendLine();
                    }

                    Append(namespaceSymbol, _namespaceHierarchyFormat);
                    BeginTypeContent();
                }

                _currentNamespace = namespaceSymbol;

                if (Options.Depth <= DocumentationDepth.Type)
                {
                    AppendTypes(types.Where(f => MetadataNameEqualityComparer <INamespaceSymbol> .Instance.Equals(f.ContainingNamespace, namespaceSymbol)));
                }

                startsWithNewLine = false;

                foreach (INamespaceSymbol namespaceSymbol2 in nestedNamespaces
                         .Where(f => MetadataNameEqualityComparer <INamespaceSymbol> .Instance.Equals(f.ContainingNamespace, namespaceSymbol))
                         .Distinct(MetadataNameEqualityComparer <INamespaceSymbol> .Instance)
                         .OrderBy(f => f, NamespaceComparer)
                         .ToArray())
                {
                    nestedNamespaces.Remove(namespaceSymbol2);

                    AppendNamespace(namespaceSymbol2, isNested: true, startsWithNewLine: startsWithNewLine);

                    startsWithNewLine = true;
                }

                _currentNamespace = null;

                if (!namespaceSymbol.IsGlobalNamespace)
                {
                    EndTypeContent();
                }
            }
        }
 public IEnumerable <IMethodSymbol> GetExtensionMethods()
 {
     return(DocumentationModel.GetExtensionMethods(Symbol));
 }