コード例 #1
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());
        }
コード例 #2
0
        private EnumMemberDeclarationSyntax GenerateKindEnumMember(string className)
        {
            string description = string.Format(
                CultureInfo.CurrentCulture,
                Resources.KindEnumMemberDescription,
                _nodeInterfaceName,
                className);

            return(SyntaxFactory.EnumMemberDeclaration(className)
                   .WithLeadingTrivia(
                       SyntaxHelper.MakeDocComment(description)));
        }
コード例 #3
0
 private MemberDeclarationSyntax GenerateVisitActualMethod()
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword)),
                VisitActualMethodName)
            .AddModifiers(
                SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                SyntaxFactory.Token(SyntaxKind.VirtualKeyword))
            .AddParameterListParameters(
                SyntaxFactory.Parameter(
                    SyntaxFactory.Identifier(NodeParameterName))
                .WithType(
                    SyntaxFactory.ParseTypeName(_nodeInterfaceName)))
            .AddBodyStatements(
                SyntaxFactory.IfStatement(
                    SyntaxHelper.IsNull(NodeParameterName),
                    SyntaxFactory.Block(
                        SyntaxFactory.ThrowStatement(
                            SyntaxFactory.ObjectCreationExpression(
                                SyntaxFactory.ParseTypeName("ArgumentNullException"),
                                SyntaxFactory.ArgumentList(
                                    SyntaxFactory.SingletonSeparatedList(
                                        SyntaxFactory.Argument(
                                            SyntaxFactory.LiteralExpression(
                                                SyntaxKind.StringLiteralExpression,
                                                SyntaxFactory.Literal(NodeParameterName))))),
                                default(InitializerExpressionSyntax))))),
                SyntaxFactory.SwitchStatement(
                    SyntaxFactory.MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        SyntaxFactory.IdentifierName(NodeParameterName),
                        SyntaxFactory.IdentifierName(_kindEnumName)))
                .AddSections(
                    GenerateVisitActualSwitchSections()))
            .WithLeadingTrivia(
                SyntaxHelper.MakeDocComment(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.RewritingVisitorVisitActualMethodSummary,
                        _schemaName),
                    Resources.RewritingVisitorVisitActualMethodReturns,
                    new Dictionary <string, string>
     {
         [NodeParameterName] = Resources.RewritingVisitorVisitActualMethodNodeParameter
     })));
 }
コード例 #4
0
        /// <summary>
        /// Create a property declaration.
        /// </summary>
        /// <param name="propertyName">
        /// The name of the property.
        /// </param>
        /// <returns>
        /// A property declaration built from the specified schema.
        /// </returns>
        private PropertyDeclarationSyntax CreatePropertyDeclaration(string propertyName)
        {
            PropertyInfo info = PropInfoDictionary[propertyName];

            PropertyDeclarationSyntax propDecl = SyntaxFactory.PropertyDeclaration(
                info.Type,
                propertyName.ToPascalCase())
                                                 .AddModifiers(GenerateSchemaPropertyModifiers(propertyName))
                                                 .AddAccessorListAccessors(GeneratePropertyAccessors());

            AttributeSyntax[] attributes = GeneratePropertyAttributes(propertyName, info.SerializedName, info.IsRequired, info.DefaultValue, info.Type);
            if (attributes.Length > 0)
            {
                propDecl = propDecl.AddAttributeLists(attributes
                                                      .Select(attr => SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(attr)))
                                                      .ToArray());
            }

            return(propDecl.WithLeadingTrivia(
                       SyntaxHelper.MakeDocComment(info.Description)));
        }
コード例 #5
0
        private string GenerateKindEnum(string enumName)
        {
            EnumDeclarationSyntax enumDeclaration =
                SyntaxFactory.EnumDeclaration(SyntaxFactory.Identifier(enumName))
                .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                .AddMembers(
                    SyntaxFactory.EnumMemberDeclaration("None")
                    .WithLeadingTrivia(
                        SyntaxHelper.MakeDocComment(Resources.KindEnumNoneDescription)))
                .AddMembers(
                    _generatedClassNames.Select(gcn => GenerateKindEnumMember(gcn)).ToArray());

            string summaryComment = string.Format(
                CultureInfo.CurrentCulture,
                Resources.KindEnumDescription,
                _nodeInterfaceName);

            return(enumDeclaration.Format(
                       _settings.CopyrightNotice,
                       null, // usings
                       _settings.SuffixedNamespaceName,
                       summaryComment));
        }
コード例 #6
0
        private string GenerateSyntaxInterface(string schemaName, string enumName, string syntaxInterfaceName)
        {
            PropertyDeclarationSyntax syntaxKindPropertyDeclaration =
                SyntaxFactory.PropertyDeclaration(
                    SyntaxFactory.ParseTypeName(enumName),
                    enumName)
                .AddAccessorListAccessors(SyntaxHelper.MakeGetAccessor())
                .WithLeadingTrivia(
                    SyntaxHelper.MakeDocComment(
                        string.Format(CultureInfo.CurrentCulture, Resources.SyntaxInterfaceKindDescription, syntaxInterfaceName)));

            MethodDeclarationSyntax deepCloneMethodDeclaration =
                SyntaxFactory.MethodDeclaration(
                    SyntaxFactory.ParseTypeName(_nodeInterfaceName),
                    "DeepClone")
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                .WithLeadingTrivia(
                    SyntaxHelper.MakeDocComment(Resources.SyntaxInterfaceDeepCloneDescription));

            InterfaceDeclarationSyntax interfaceDeclaration =
                SyntaxFactory.InterfaceDeclaration(_nodeInterfaceName)
                .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                .AddMembers(
                    syntaxKindPropertyDeclaration,
                    deepCloneMethodDeclaration);

            string summaryComment = string.Format(
                CultureInfo.CurrentCulture,
                Resources.SyntaxInterfaceDescription,
                schemaName);

            return(interfaceDeclaration.Format(
                       _settings.CopyrightNotice,
                       null, // usings
                       _settings.SuffixedNamespaceName,
                       summaryComment));
        }
コード例 #7
0
 private MemberDeclarationSyntax GenerateVisitMethod()
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.PredefinedType(
                    SyntaxFactory.Token(SyntaxKind.ObjectKeyword)),
                VisitMethodName)
            .AddModifiers(
                SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                SyntaxFactory.Token(SyntaxKind.VirtualKeyword))
            .AddParameterListParameters(
                SyntaxFactory.Parameter(
                    SyntaxFactory.Identifier(NodeParameterName))
                .WithType(
                    SyntaxFactory.ParseTypeName(_nodeInterfaceName)))
            .AddBodyStatements(
                SyntaxFactory.ReturnStatement(
                    SyntaxFactory.InvocationExpression(
                        SyntaxFactory.MemberAccessExpression(
                            SyntaxKind.SimpleMemberAccessExpression,
                            SyntaxFactory.ThisExpression(),
                            SyntaxFactory.IdentifierName(VisitActualMethodName)))
                    .AddArgumentListArguments(
                        SyntaxFactory.Argument(
                            SyntaxFactory.IdentifierName(NodeParameterName)))))
            .WithLeadingTrivia(
                SyntaxHelper.MakeDocComment(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.RewritingVisitorVisitMethodSummary,
                        _schemaName),
                    Resources.RewritingVisitorVisitMethodReturns,
                    new Dictionary <string, string>
     {
         [NodeParameterName] = Resources.RewritingVisitorVisitMethodNodeParameter
     })));
 }