private Task <Document> AddSummaryCommentAsync(Document document, BaseTypeDeclarationSyntax declaration, CancellationToken c)
        {
            var leadingTrivias  = declaration.GetLeadingTrivia();
            var whitespaceCount = leadingTrivias[leadingTrivias.Count - 1].Span.Length;
            var newDeclaration  = declaration.WithLeadingTrivia(
                AnalyzerUtil.GetNewLeadingTriviaWithSummary(leadingTrivias, summaryComments, whitespaceCount));

            return(AnalyzerUtil.ReplaceNode(declaration, newDeclaration, document));
        }
Exemple #2
0
        /// <summary>
        /// Format a type declaration into a string representing the contents of a file
        /// containing that single type declaration.
        /// </summary>
        /// <param name="typeDecl">
        /// The type declaration to be formatted.
        /// </param>
        /// <param name="copyrightNotice">
        /// The copyright notice to display at the top of the file, or null if there is
        /// no copyright notice.
        /// </param>
        /// <param name="usings">
        /// A list containing the names of any namespaces required by the type declaration,
        /// or null if no namespaces are required.
        /// </param>
        /// <param name="namespaceName">
        /// The name of the namespace containing the type declaration. Required.
        /// </param>
        /// <param name="summaryComment">
        /// The summary comment for the type, or null if there is no summary comment.
        /// </param>
        /// <returns>
        /// The formatted string.
        /// </returns>
        /// <remarks>
        /// The first parameter is declared as <see cref="BaseTypeDeclarationSyntax"/>
        /// because this method works for enums as well as for classes and interfaces.
        /// Classes and interfaces derive from <see cref="TypeDeclarationSyntax"/>, but
        /// enums do not: they derive from <see cref="EnumDeclarationSyntax"/>, and both
        /// those types derive from BaseTypeDeclarationSyntax.
        /// </remarks>
        internal static string Format(
            this BaseTypeDeclarationSyntax typeDecl,
            string copyrightNotice,
            List <string> usings,
            string namespaceName,
            string summaryComment)
        {
            typeDecl = AddGeneratedCodeAttribute(typeDecl);

            if (summaryComment != null)
            {
                typeDecl = typeDecl.WithLeadingTrivia(
                    SyntaxHelper.MakeDocComment(summaryComment));
            }

            NamespaceDeclarationSyntax namespaceDecl =
                SyntaxFactory.NamespaceDeclaration(
                    SyntaxFactory.IdentifierName(namespaceName))
                .AddMembers(typeDecl);

            CompilationUnitSyntax compilationUnit = SyntaxFactory.CompilationUnit()
                                                    .AddMembers(namespaceDecl);

            if (usings == null)
            {
                usings = new List <string>();
            }

            usings.Add("System.CodeDom.Compiler"); // For GeneratedCodeAttribute

            UsingDirectiveSyntax[] usingDirectives = usings
                                                     .Distinct()
                                                     .OrderBy(u => u, UsingComparer.Instance)
                                                     .Select(u => SyntaxFactory.UsingDirective(MakeQualifiedName(u)))
                                                     .ToArray();

            compilationUnit = compilationUnit.AddUsings(usingDirectives);

            if (!string.IsNullOrWhiteSpace(copyrightNotice))
            {
                compilationUnit = compilationUnit.WithLeadingTrivia(
                    SyntaxFactory.ParseLeadingTrivia(copyrightNotice));
            }

            var        workspace     = new AdhocWorkspace();
            SyntaxNode formattedNode = Formatter.Format(compilationUnit, workspace);

            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                formattedNode.WriteTo(writer);
            }

            return(sb.ToString());
        }
Exemple #3
0
        private static void GenerateSingleCSharpDocument(
            IEnumerable <GeneratorResult> results,
            SourceDocumentKind kind,
            string fileName,
            ICollection <SourceDocument> documents)
        {
            var code = new StringBuilder();

            // marker for style cop to ignore this code
            code.AppendLine("// <auto-generated/>");

            // enable nullability settings
            code.AppendLine("#nullable enable");

            CompilationUnitSyntax compilationUnit = CompilationUnit();

            foreach (var group in results.GroupBy(t => t.Result.Namespace).OrderBy(t => t.Key))
            {
                NamespaceDeclarationSyntax namespaceDeclaration =
                    NamespaceDeclaration(IdentifierName(group.Key));

                foreach (var item in group)
                {
                    BaseTypeDeclarationSyntax typeDeclaration = item.Result.TypeDeclaration;
#if DEBUG
                    SyntaxTriviaList trivia = typeDeclaration
                                              .GetLeadingTrivia()
                                              .Insert(0, Comment("// " + item.Generator.FullName));

                    typeDeclaration = typeDeclaration.WithLeadingTrivia(trivia);
#endif
                    namespaceDeclaration = namespaceDeclaration.AddMembers(typeDeclaration);
                }

                compilationUnit = compilationUnit.AddMembers(namespaceDeclaration);
            }

            compilationUnit = compilationUnit.NormalizeWhitespace(elasticTrivia: true);

            code.AppendLine();
            code.AppendLine(compilationUnit.ToFullString());

            documents.Add(new(
                              fileName,
                              code.ToString(),
                              kind));
        }
        private static void GenerateMultipleCSharpDocuments(
            MapperContext context,
            CSharpGeneratorSettings settings,
            ICollection <SourceDocument> documents)
        {
            var generatorSettings = new CSharpSyntaxGeneratorSettings(
                settings.NoStore,
                settings.InputRecords,
                settings.EntityRecords);

            var results = new List <(Type Generator, CSharpSyntaxGeneratorResult Result)>();

            foreach (var descriptor in context.GetAllDescriptors())
            {
                foreach (var generator in _generators)
                {
                    if (generator.CanHandle(descriptor, generatorSettings))
                    {
                        CSharpSyntaxGeneratorResult result =
                            generator.Generate(descriptor, generatorSettings);
                        results.Add((generator.GetType(), result));
                    }
                }
            }

            foreach (var group in results.GroupBy(t => t.Result.Namespace).OrderBy(t => t.Key))
            {
                foreach ((Type generator, CSharpSyntaxGeneratorResult result) in group)
                {
                    BaseTypeDeclarationSyntax typeDeclaration = result.TypeDeclaration;
#if DEBUG
                    SyntaxTriviaList trivia = typeDeclaration
                                              .GetLeadingTrivia()
                                              .Insert(0, Comment("// " + generator.FullName));

                    typeDeclaration = typeDeclaration.WithLeadingTrivia(trivia);
#endif
                    CompilationUnitSyntax compilationUnit =
                        CompilationUnit().AddMembers(
                            NamespaceDeclaration(IdentifierName(group.Key)).AddMembers(
                                typeDeclaration));

                    compilationUnit = compilationUnit.NormalizeWhitespace(elasticTrivia: true);

                    var code = new StringBuilder();

                    // marker for style cop to ignore this code
                    code.AppendLine("// <auto-generated/>");

                    // enable nullability settings
                    code.AppendLine("#nullable enable");

                    code.AppendLine();
                    code.AppendLine(compilationUnit.ToFullString());

                    documents.Add(new(
                                      result.FileName,
                                      code.ToString(),
                                      SourceDocumentKind.CSharp,
                                      path: result.Path));
                }
            }
        }