Esempio n. 1
0
        public SyntaxTree CreateSyntaxTree()
        {
            // The call to CreateType must come before the call to namespaces.GetUsings.
            // This is because CreateType builds the list of referenced namespaces.
            MemberDeclarationSyntax typeDeclaration = CreateType();

            if (Type.Docs != null)
            {
                DocCommentGeneratorBase <Type> generator = new TypeDocCommentGenerator(Type);
                SyntaxTriviaList trivia = SF.TriviaList(generator.CreateDocComment());

                typeDeclaration = typeDeclaration.WithLeadingTrivia(trivia);
            }

            NamespaceDeclarationSyntax namespaceDeclaration = SF.NamespaceDeclaration(
                Symbols.GetNamespaceSyntax(Type),
                SF.List <ExternAliasDirectiveSyntax>(),
                SF.List <UsingDirectiveSyntax>(),
                SF.List(new[] { typeDeclaration })
                );

            return(SF.SyntaxTree(
                       SF.CompilationUnit(
                           SF.List <ExternAliasDirectiveSyntax>(),
                           Namespaces.GetUsings(),
                           SF.List <AttributeListSyntax>(),
                           SF.List <MemberDeclarationSyntax>(new[] { namespaceDeclaration })
                           ).NormalizeWhitespace(elasticTrivia: true)
                       ));
        }
Esempio n. 2
0
        public static ParameterListSyntax GetParameterListSyntax(this Method method, INamespaceSet namespaces, ISymbolMap symbols)
        {
            method     = method ?? throw new ArgumentNullException(nameof(method));
            namespaces = namespaces ?? throw new ArgumentNullException(nameof(namespaces));
            symbols    = symbols ?? throw new ArgumentNullException(nameof(symbols));

            return(SF.ParameterList(SF.SeparatedList(GetParameters())));

            IEnumerable <ParameterSyntax> GetParameters()
            {
                if (method.Parameters == null)
                {
                    yield break;
                }

                foreach (Parameter parameter in method.Parameters)
                {
                    namespaces.Add(parameter.Type);

                    yield return(SF.Parameter(
                                     SF.List <AttributeListSyntax>(),
                                     SF.TokenList(),
                                     symbols.GetTypeSyntax(parameter.Type),
                                     symbols.GetNameSyntaxToken(parameter),
                                     null
                                     ));
                }
            }
        }
Esempio n. 3
0
        private static SyntaxNode Normalize(SyntaxNode root, IEnumerable <SyntaxNode> members, Scope scope)
        {
            var appClass = root
                           .DescendantNodes()
                           .OfType <ClassDeclarationSyntax>()
                           .Where(@class => @class.Identifier.ToString() == "testclass")
                           .FirstOrDefault();

            if (appClass == null)
            {
                appClass = CSharp.ClassDeclaration("testclass");
            }

            return(CSharp.CompilationUnit()
                   .WithMembers(CSharp.List(new[] { (MemberDeclarationSyntax)
                                                    appClass
                                                    .WithMembers(CSharp.List(
                                                                     members.Select(
                                                                         member => {
                    var method = member as MethodDeclarationSyntax;
                    if (method == null)
                    {
                        return member;
                    }

                    return method.WithParameterList(
                        method
                        .ParameterList
                        .AddParameters(CSharp.Parameter(
                                           CSharp.ParseToken("result"))
                                       .WithType(CSharp.ParseTypeName("Dictionary<string, object>"))));
                }))) })));
        }
Esempio n. 4
0
 private SyntaxNode typeExtension(SyntaxNode node, SyntacticalExtension <SyntaxNode> extension)
 {
     return(CSharp.ClassDeclaration(extension.Identifier)
            .WithMembers(CSharp.List <MemberDeclarationSyntax>(new[] {
         CSharp.MethodDeclaration(CSharp.ParseTypeName("int"), "myMethod")
         .WithBody((BlockSyntax)extension.Body)
     })));
 }
Esempio n. 5
0
        public SyntaxList <UsingDirectiveSyntax> GetUsings()
        {
            // Don't add a redundant using for the current namespace.
            _referencedNamespaces.Remove(_currentNamespace);

            IEnumerable <UsingDirectiveSyntax> usings = _referencedNamespaces
                                                        .Select(n => SF.UsingDirective(n))
                                                        .OrderBy(u => u.Name.ToString().ToUpperInvariant());

            return(SF.List(usings));
        }
Esempio n. 6
0
        protected override SyntaxNode addModules(SyntaxNode root, IEnumerable <string> modules)
        {
            var compilationUnit = (CompilationUnitSyntax)root;

            return(compilationUnit
                   .WithUsings(CSharp.List(
                                   compilationUnit.Usings.Union(
                                       modules
                                       .Select(module => CSharp.UsingDirective(
                                                   CSharp.ParseName(module)))))));
        }
Esempio n. 7
0
        protected override SyntaxNode addModules(SyntaxNode root, IEnumerable <string> modules)
        {
            var compilationUnit = root.AncestorsAndSelf()
                                  .Where(a => a is CompilationUnitSyntax)
                                  .Select(a => a as CompilationUnitSyntax)
                                  .FirstOrDefault()
                                  ?? CSharp.CompilationUnit()
                                  .WithMembers(CSharp.List(new[] {
                (MemberDeclarationSyntax)root
            }));

            return(compilationUnit
                   .WithUsings(CSharp.List(
                                   compilationUnit.Usings.Union(
                                       modules
                                       .Select(module => CSharp
                                               .UsingDirective(CSharp
                                                               .ParseName(module))
                                               .WithTrailingTrivia(CSharp.EndOfLine(string.Empty))))))
                   ?? root);
        }
Esempio n. 8
0
 public BlockSyntax Build()
 => SF.Block(SF.List(_statements.Select(s => s.Build())));
Esempio n. 9
0
        public TypeSyntax GetTypeSyntax(TypeReference typeReference)
        {
            bool isOptional = (typeReference ?? throw new ArgumentNullException(nameof(typeReference))).IsOptional == true;

            if (typeReference.Primitive != null)
            {
                switch (typeReference.Primitive.Value)
                {
                case PrimitiveType.Any:
                    return(SF.ParseTypeName("object"));

                case PrimitiveType.Boolean:
                    return(SF.ParseTypeName(isOptional ? "bool?" : "bool"));

                case PrimitiveType.Date:
                    return(SF.ParseTypeName(isOptional ? "DateTime?" : "DateTime"));

                case PrimitiveType.Json:
                    return(SF.ParseTypeName("JObject"));

                case PrimitiveType.Number:
                    return(SF.ParseTypeName(isOptional ? "double?" : "double"));

                case PrimitiveType.String:
                    return(SF.ParseTypeName("string"));

                default:
                    throw new ArgumentException($"Unexpected primitive type {typeReference.Primitive.Value}", nameof(typeReference));
                }
            }

            if (typeReference.Collection != null)
            {
                TypeSyntax elementType = GetTypeSyntax(typeReference.Collection.ElementType);

                switch (typeReference.Collection.Kind)
                {
                case CollectionKind.Array:
                    return(SF.ArrayType(
                               elementType,
                               SF.List(new[] { SF.ArrayRankSpecifier() })
                               ));

                case CollectionKind.Map:
                    return(SF.ParseTypeName($"IDictionary<string, {elementType}>"));

                default:
                    throw new ArgumentException($"Unexpected collection type {typeReference.Collection.Kind}", nameof(typeReference));
                }
            }

            if (typeReference.Union != null)
            {
                return(SF.ParseTypeName("object"));
            }

            if (typeReference.FullyQualifiedName != null)
            {
                Type type = GetTypeFromFullyQualifiedName(typeReference.FullyQualifiedName);

                return(SF.ParseTypeName(GetName(type)));
            }

            throw new ArgumentException("Invalid type reference", nameof(typeReference));
        }
Esempio n. 10
0
 public ParameterSyntax Build()
 => _type != null
          ? SF.Parameter(SF.List <AttributeListSyntax>(), SF.TokenList(), ParseType(_type), SF.ParseToken(_name), null)
          : SF.Parameter(SF.List <AttributeListSyntax>(), SF.TokenList(), null, SF.ParseToken(_name), null);
Esempio n. 11
0
 public PropertyDeclarationSyntax Build()
 => SF.PropertyDeclaration(ParseType(_type), _name)
 .AddModifiers(_modifiers.Build().ToArray())
 .WithAccessorList(SF.AccessorList(SF.List(_accessors.Select(a => a.Build()))));