Example #1
0
        public static BaseListSyntax GetBaseList(IHasImplementedInterfaces item)
        {
            var list    = new List <BaseTypeSyntax>();
            var asClass = item as IClass;

            if (asClass != null)
            {
                if (asClass.BaseType != null)
                {
                    var baseTypeSyntax = (TypeSyntax)RDom.CSharp.GetSyntaxNode(asClass.BaseType);
                    var baseSyntax     = SyntaxFactory.SimpleBaseType(baseTypeSyntax);
                    list.Add(baseSyntax);
                }
            }
            foreach (var interf in item.ImplementedInterfaces)
            {
                var interfTypeSyntax = (TypeSyntax)RDom.CSharp.GetSyntaxNode(interf);
                var baseSyntax       = SyntaxFactory.SimpleBaseType(interfTypeSyntax);
                list.Add(baseSyntax);
            }

            var colonToken = SyntaxFactory.Token(SyntaxKind.ColonToken);

            colonToken = BuildSyntaxHelpers.AttachWhitespaceToToken(colonToken, item.Whitespace2Set[LanguageElement.BaseListPrefix]);

            return(list.Any()
                  ? SyntaxFactory.BaseList(colonToken, SyntaxFactory.SeparatedList <BaseTypeSyntax>(list))
                  : null);
        }
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT    = item as IAssignmentStatement;
            var leftSyntax = RDom.CSharp.GetSyntaxNode(itemAsT.Left);

            leftSyntax = BuildSyntaxHelpers.AttachWhitespaceToFirstAndLast(leftSyntax,
                                                                           itemAsT.Whitespace2Set[LanguageElement.LeftExpression]);

            var expressionSyntax = RDom.CSharp.GetSyntaxNode(itemAsT.Expression);

            expressionSyntax = BuildSyntaxHelpers.AttachWhitespaceToFirstAndLast(expressionSyntax,
                                                                                 itemAsT.Whitespace2Set[LanguageElement.Expression]);

            var syntaxKind = Mappings.SyntaxKindFromAssignmentOperator(itemAsT.Operator);
            var opToken    = SyntaxFactory.Token(Mappings.SyntaxTokenKindFromAssignmentOperator(itemAsT.Operator));

            opToken = BuildSyntaxHelpers.AttachWhitespaceToToken(opToken, itemAsT.Whitespace2Set[LanguageElement.AssignmentOperator]);

            var assignmentSyntax = SyntaxFactory.AssignmentExpression(syntaxKind,
                                                                      (ExpressionSyntax)leftSyntax,
                                                                      opToken,
                                                                      (ExpressionSyntax)expressionSyntax);

            assignmentSyntax = BuildSyntaxHelpers.AttachWhitespace(assignmentSyntax, itemAsT.Whitespace2Set, WhitespaceLookup);
            var node = SyntaxFactory.ExpressionStatement(assignmentSyntax);

            node = BuildSyntaxHelpers.AttachWhitespace(node, itemAsT.Whitespace2Set, WhitespaceLookup);
            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
Example #3
0
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT          = item as IField;
            var nameSyntax       = SyntaxFactory.Identifier(itemAsT.Name);
            var returnTypeSyntax = (TypeSyntax)RDom.CSharp.GetSyntaxGroup(itemAsT.ReturnType).First();
            var modifiers        = BuildSyntaxHelpers.BuildModfierSyntax(itemAsT);

            if (itemAsT.IsReadOnly)
            {
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword));
            }
            if (itemAsT.IsConstant)
            {
                modifiers = modifiers.Remove(modifiers.Where(x => x.Kind() == SyntaxKind.StaticKeyword).First());
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.ConstKeyword));
            }
            if (itemAsT.IsVolatile)
            {
                modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.VolatileKeyword));
            }
            var declaratorNode = SyntaxFactory.VariableDeclarator(nameSyntax);

            if (itemAsT.Initializer != null)
            {
                var expressionSyntax = (ExpressionSyntax)RDom.CSharp.GetSyntaxNode(itemAsT.Initializer);
                expressionSyntax = BuildSyntaxHelpers.AttachWhitespaceToFirstAndLast(expressionSyntax, itemAsT.Whitespace2Set[LanguageElement.Expression]);
                var equalsToken = SyntaxFactory.Token(SyntaxKind.EqualsToken);
                equalsToken = BuildSyntaxHelpers.AttachWhitespaceToToken(equalsToken, itemAsT.Whitespace2Set[LanguageElement.EqualsAssignmentOperator]);
                var equalsValueClause = SyntaxFactory.EqualsValueClause(equalsToken, expressionSyntax);
                declaratorNode = declaratorNode.WithInitializer(equalsValueClause);
            }
            declaratorNode = BuildSyntaxHelpers.AttachWhitespace(declaratorNode, itemAsT.Whitespace2Set, WhitespaceLookup);

            var variableNode = SyntaxFactory.VariableDeclaration(returnTypeSyntax)
                               .WithVariables(
                SyntaxFactory.SingletonSeparatedList(declaratorNode));
            //variableNode = BuildSyntaxHelpers.AttachWhitespace(variableNode, itemAsField.Whitespace2Set, WhitespaceLookup);
            var node = SyntaxFactory.FieldDeclaration(variableNode)
                       .WithModifiers(modifiers);

            node = BuildSyntaxHelpers.AttachWhitespace(node, itemAsT.Whitespace2Set, WhitespaceLookup);

            var attributes = BuildSyntaxWorker.BuildAttributeSyntax(itemAsT.Attributes);

            if (attributes.Any())
            {
                node = node.WithAttributeLists(BuildSyntaxHelpers.WrapInAttributeList(attributes));
            }

            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
Example #4
0
        public static BaseListSyntax GetBaseList(IEnum item)
        {
            if (item.UnderlyingType != null)
            {
                var underlyingTypeSyntax = (TypeSyntax)RDom.CSharp.GetSyntaxNode(item.UnderlyingType);
                underlyingTypeSyntax = BuildSyntaxHelpers.AttachWhitespace(
                    underlyingTypeSyntax, item.UnderlyingType.Whitespace2Set, whitespaceLookup);

                var colonToken = SyntaxFactory.Token(SyntaxKind.ColonToken);
                colonToken = BuildSyntaxHelpers.AttachWhitespaceToToken(colonToken, item.Whitespace2Set[LanguageElement.BaseListPrefix]);
                return(SyntaxFactory.BaseList(colonToken,
                                              SyntaxFactory.SingletonSeparatedList <BaseTypeSyntax>(SyntaxFactory.SimpleBaseType(underlyingTypeSyntax))));
            }
            return(null);
        }
        private IEnumerable <SyntaxNode> BuildSyntaxLocal(IVariableDeclaration itemAsT, TypeSyntax typeSyntax)
        {
            var node = SyntaxFactory.VariableDeclarator(itemAsT.Name);

            if (itemAsT.Initializer != null)
            {
                var expressionSyntax = (ExpressionSyntax)RDom.CSharp.GetSyntaxNode(itemAsT.Initializer);
                expressionSyntax = BuildSyntaxHelpers.AttachWhitespaceToFirstAndLast(expressionSyntax, itemAsT.Whitespace2Set[LanguageElement.Expression]);
                var equalsToken = SyntaxFactory.Token(SyntaxKind.EqualsToken);
                equalsToken = BuildSyntaxHelpers.AttachWhitespaceToToken(equalsToken, itemAsT.Whitespace2Set[LanguageElement.EqualsAssignmentOperator]);
                var equalsValueClause = SyntaxFactory.EqualsValueClause(equalsToken, expressionSyntax);
                //equalsValueClause = BuildSyntaxHelpers.AttachWhitespace(equalsValueClause, itemAsT.Whitespace2Set, WhitespaceLookup);
                node = node.WithInitializer(equalsValueClause);
            }

            node = BuildSyntaxHelpers.AttachWhitespace(node, itemAsT.Whitespace2Set, WhitespaceLookup);
            // Not a statement, so should not call PrepareForBuildSyntaxOutput
            return(new SyntaxNode[] { node });
        }
        // Not sure if this belongs here or in BuildSyntaxHelpers
        public TypeSyntax TypeSyntaxFromType(IReferencedType type)
        {
            // Type syntax is realy ugly to build, so I build it and then hack hte type arg whitespace
            var typeName = CleanName(type);

            if (type.TypeArguments.Any())
            {
                typeName += "<";
                foreach (var tArg in type.TypeArguments)
                {
                    var display = BuildSyntax(tArg).FirstOrDefault().ToFullString();
                    typeName += display;
                    typeName += (tArg == type.TypeArguments.Last()
                              ? ""
                              : ", ");
                    //typeName += tArg.QualifiedName +
                    //             (tArg == type.TypeArguments.Last()
                    //               ? ""
                    //               : ", ");
                }
                typeName += ">";
            }
            if (type.IsArray)
            {
                typeName += "[]";
            }
            var node        = SyntaxFactory.ParseTypeName(typeName);
            var typeArgNode = node.ChildNodes().OfType <TypeArgumentListSyntax>().FirstOrDefault();

            if (typeArgNode != null)
            {
                var newStartToken = BuildSyntaxHelpers.AttachWhitespaceToToken(typeArgNode.LessThanToken,
                                                                               type.Whitespace2Set[LanguageElement.TypeParameterStartDelimiter]);
                var newEndToken = BuildSyntaxHelpers.AttachWhitespaceToToken(typeArgNode.GreaterThanToken,
                                                                             type.Whitespace2Set[LanguageElement.TypeParameterEndDelimiter]);
                var newTypeArgNode = typeArgNode.WithLessThanToken(newStartToken).WithGreaterThanToken(newEndToken);
                //var newTypeArgNode = typeArgNode.WithLessThanToken(newStartToken);
                node = node.ReplaceNode(typeArgNode, newTypeArgNode);
            }
            return(node);
        }