Esempio n. 1
0
 public DeclarationListBuilder(
     StringBuilder stringBuilder    = null,
     DeclarationListOptions options = null,
     IComparer <INamespaceSymbol> namespaceComparer = null,
     IComparer <INamedTypeSymbol> typeComparer      = null,
     IComparer <ISymbol> memberComparer             = null)
 {
     StringBuilder     = stringBuilder ?? new StringBuilder();
     Options           = options ?? DeclarationListOptions.Default;
     NamespaceComparer = namespaceComparer ?? NamespaceSymbolComparer.SystemFirst;
     TypeComparer      = typeComparer ?? TypeDeclarationComparer.Instance;
     MemberComparer    = memberComparer ?? MemberDeclarationComparer.Instance;
 }
        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());
        }
 public Rewriter(DeclarationListOptions options, SemanticModel semanticModel, CancellationToken cancellationToken = default)
 {
     Options           = options;
     SemanticModel     = semanticModel;
     CancellationToken = cancellationToken;
 }