BuildStatement() public static method

public static BuildStatement ( IEnumerable statements, IStatementBlock parent, WhitespaceKindLookup whitespaceLookup ) : StatementSyntax
statements IEnumerable
parent IStatementBlock
whitespaceLookup WhitespaceKindLookup
return StatementSyntax
示例#1
0
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT   = item as IUsingStatement;
            var statement = RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup);
            var node      = SyntaxFactory.UsingStatement(statement);

            if (itemAsT.Variable != null)
            {
                //var typeSyntax = BuildSyntaxWorker.GetVariableTypeSyntax(itemAsT.Variable);
                var typeSyntax = BuildSyntaxWorker.GetVariableTypeSyntax(
                    itemAsT.Variable.IsImplicitlyTyped, itemAsT.Variable.Type);
                var expressionSyntax  = RDom.CSharp.GetSyntaxNode(itemAsT.Variable.Initializer);
                var equalsValueClause = SyntaxFactory.EqualsValueClause((ExpressionSyntax)expressionSyntax);
                equalsValueClause = BuildSyntaxHelpers.AttachWhitespace(equalsValueClause, itemAsT.Variable.Whitespace2Set, WhitespaceLookup);

                var nodeDeclarator = SyntaxFactory.VariableDeclarator(itemAsT.Variable.Name);
                nodeDeclarator = nodeDeclarator.WithInitializer(equalsValueClause);
                nodeDeclarator = BuildSyntaxHelpers.AttachWhitespace(nodeDeclarator, itemAsT.Variable.Whitespace2Set, WhitespaceLookup);
                var nodeDeclaratorInList = SyntaxFactory.SeparatedList(SyntaxFactory.List <VariableDeclaratorSyntax>(new VariableDeclaratorSyntax[] { (VariableDeclaratorSyntax)nodeDeclarator }));
                var nodeDeclaration      = SyntaxFactory.VariableDeclaration(typeSyntax, nodeDeclaratorInList);
                node = node.WithDeclaration(nodeDeclaration);
            }
            else
            {
                var expressionSyntax = RDom.CSharp.GetSyntaxNode(itemAsT.Expression) as ExpressionSyntax;
                node = node.WithExpression(expressionSyntax);
            }

            node = BuildSyntaxHelpers.AttachWhitespace(node, itemAsT.Whitespace2Set, WhitespaceLookup);
            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT = item as IConversionOperator;

            var kind       = itemAsT.IsImplicit ? SyntaxKind.ImplicitKeyword : SyntaxKind.ExplicitKeyword;
            var opToken    = SyntaxFactory.Token(kind);
            var typeSyntax = (TypeSyntax)RDom.CSharp.GetSyntaxGroup(itemAsT.Type).First();
            var modifiers  = BuildSyntaxHelpers.BuildModfierSyntax(itemAsT);
            var node       = SyntaxFactory.ConversionOperatorDeclaration(opToken, typeSyntax)
                             .WithModifiers(modifiers);

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

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

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

            //node = node.WithLeadingTrivia(BuildSyntaxHelpers.LeadingTrivia(item));

            var parameterList = itemAsT.Parameters
                                .SelectMany(x => RDom.CSharp.GetSyntaxGroup(x))
                                .OfType <ParameterSyntax>()
                                .ToList();
            var parameterListSyntax = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameterList));

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

            node = node.WithBody((BlockSyntax)RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup));

            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var        itemAsT = item as T;
            SyntaxNode node;

            if (itemAsT.Condition == null)
            // TODO: Isn't condition null in a ForEach?
            {
                node = SyntaxFactory.EmptyStatement();
            }                                      // This shold not happen
            else
            {
                var statement = RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup);
                var condition = (ExpressionSyntax)RDom.CSharp.GetSyntaxNode(itemAsT.Condition);
                node = MakeSyntax(itemAsT, condition, statement);
            }

            var leadingTrivia = BuildSyntaxHelpers.LeadingTrivia(itemAsT, OutputContext);

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

            return(node.PrepareForBuildSyntaxOutput(itemAsT, OutputContext));
        }
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT          = item as ILockStatement;
            var statement        = RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup);
            var expressionSyntax = RDom.CSharp.GetSyntaxNode(itemAsT.Expression) as ExpressionSyntax;
            var node             = SyntaxFactory.LockStatement(expressionSyntax, statement);

            node = BuildSyntaxHelpers.AttachWhitespace(node, itemAsT.Whitespace2Set, WhitespaceLookup);
            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
示例#5
0
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT   = item as ICheckedStatement;
            var statement = RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup) as BlockSyntax;
            var kind      = itemAsT.Unchecked ? SyntaxKind.UncheckedStatement : SyntaxKind.CheckedStatement;

            var node = SyntaxFactory.CheckedStatement(kind, statement);


            node = BuildSyntaxHelpers.AttachWhitespace(node, itemAsT.Whitespace2Set, WhitespaceLookup);
            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var         itemAsT = item as IConstructor;
            var         parent  = item.Parent as IClass;
            SyntaxToken nameSyntax;

            if (parent == null)
            {
                nameSyntax = SyntaxFactory.Identifier("unknown_name");
            }
            else
            {
                nameSyntax = SyntaxFactory.Identifier(parent.Name);
            }

            var modifiers = BuildSyntaxHelpers.BuildModfierSyntax(itemAsT);
            var node      = SyntaxFactory.ConstructorDeclaration(nameSyntax)
                            .WithModifiers(modifiers);

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

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

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

            var parameterList = itemAsT.Parameters
                                .SelectMany(x => RDom.CSharp.GetSyntaxGroup(x))
                                .OfType <ParameterSyntax>()
                                .ToList();

            var parameterListSyntax = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameterList));

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

            //node = node.WithLeadingTrivia(BuildSyntaxHelpers.LeadingTrivia(item));

            node = node.WithBody((BlockSyntax)RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup));

            var initializer = BuildInitializer(itemAsT);

            if (initializer != null)
            {
                initializer = BuildSyntaxHelpers.AttachWhitespace(initializer,
                                                                  itemAsT.Whitespace2Set, WhitespaceLookup, LanguagePart.Initializer);
                node = node.WithInitializer(initializer);
            }

            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
示例#7
0
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT        = item as IAccessor;
            var parentProperty = item.Parent as RDomProperty; // .NET specific

            if (itemAsT == null || parentProperty == null)
            {
                throw new InvalidOperationException();
            }
            var kind = (itemAsT.AccessorType == AccessorType.Get)
                     ? SyntaxKind.GetAccessorDeclaration : SyntaxKind.SetAccessorDeclaration;
            AccessorDeclarationSyntax node;

            if (itemAsT.Statements.Any() || !parentProperty.CanBeAutoProperty)
            {
                var statementBlock = (BlockSyntax)RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup);
                node = SyntaxFactory.AccessorDeclaration(kind, statementBlock);
            }
            else
            {
                node = SyntaxFactory.AccessorDeclaration(kind).WithSemicolonToken(
                    SyntaxFactory.Token(
                        SyntaxKind.SemicolonToken));
            }

            if (itemAsT.AccessModifier != parentProperty.AccessModifier)
            {
                var modifiers = item.BuildModfierSyntax();
                node = node.WithModifiers(modifiers);
            }

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

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

            if (attributeList.Any())
            {
                node = node.WithAttributeLists(attributeList);
            }

            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT    = item as IDestructor;
            var nameSyntax = SyntaxFactory.Identifier(itemAsT.Name);

            var modifiers = BuildSyntaxHelpers.BuildModfierSyntax(itemAsT);
            var node      = SyntaxFactory.DestructorDeclaration(nameSyntax)
                            .WithModifiers(modifiers);

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

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

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

            //node = node.WithLeadingTrivia(BuildSyntaxHelpers.LeadingTrivia(item));

            node = node.WithBody((BlockSyntax)RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup));

            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }
示例#9
0
 private StatementSyntax GetStatement(IStatementBlock itemAsT)
 {
     return(RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup));
 }
示例#10
0
        public override IEnumerable <SyntaxNode> BuildSyntax(IDom item)
        {
            var itemAsT    = item as IMethod;
            var nameSyntax = SyntaxFactory.Identifier(itemAsT.Name);

            var returnTypeSyntax = (TypeSyntax)RDom.CSharp.GetSyntaxGroup(itemAsT.ReturnType).First();
            var modifiers        = BuildSyntaxHelpers.BuildModfierSyntax(itemAsT);
            var node             = SyntaxFactory.MethodDeclaration(returnTypeSyntax, nameSyntax)
                                   .WithModifiers(modifiers);

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

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

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

            var parameterList = itemAsT.Parameters
                                .SelectMany(x => RDom.CSharp.GetSyntaxGroup(x))
                                .OfType <ParameterSyntax>()
                                .ToList();

            if (itemAsT.IsExtensionMethod)
            {
                // this this is a normal list, ref semantics
                var firstParam = parameterList.FirstOrDefault();
                parameterList.Remove(firstParam);
                if (firstParam == null)
                {
                    throw new InvalidOperationException("Extension methods must have at least one parameter");
                }
                // I'm cheating a bit here. Since the This keyword is an indicator of extension state on the method
                // I'm hardcoding a single space. I don't see the complexity of dealing with this as worth it unless
                // there's gnashing of teeth over this single space. The use of "this" on the parameter is not universal
                // and VB marks the method.
                var thisModifier = SyntaxFactory.Token(SyntaxKind.ThisKeyword)
                                   .WithTrailingTrivia(SyntaxFactory.ParseTrailingTrivia(" "));
                var paramModifiers = firstParam.Modifiers.Insert(0, thisModifier);
                firstParam = firstParam.WithModifiers(paramModifiers);
                parameterList.Insert(0, firstParam);
            }
            var parameterListSyntax = SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameterList));

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

            node = node.WithBody((BlockSyntax)RoslynCSharpUtilities.BuildStatement(itemAsT.Statements, itemAsT, WhitespaceLookup));

            // This works oddly because it uncollapses the list
            // This code is largely repeated in interface and class factories, but is very hard to refactor because of shallow Roslyn (Microsoft) architecture
            var typeParamsAndConstraints = itemAsT.TypeParameters
                                           .SelectMany(x => RDom.CSharp.GetSyntaxGroup(x))
                                           .ToList();

            node = BuildSyntaxHelpers.BuildTypeParameterSyntax(
                itemAsT, node, WhitespaceLookup,
                (x, p) => x.WithTypeParameterList(p),
                (x, c) => x.WithConstraintClauses(c));
            //var typeParameterSyntaxList = BuildSyntaxHelpers.GetTypeParameterSyntaxList(
            //            typeParamsAndConstraints, itemAsT.Whitespace2Set, WhitespaceLookup);
            //if (typeParameterSyntaxList != null)
            //{
            //    node = node.WithTypeParameterList(typeParameterSyntaxList);
            //    var clauses = BuildSyntaxHelpers.GetTypeParameterConstraintList(
            //              typeParamsAndConstraints, itemAsT.Whitespace2Set, WhitespaceLookup);
            //    if (clauses.Any())
            //    { node = node.WithConstraintClauses(clauses); }
            //}

            return(node.PrepareForBuildSyntaxOutput(item, OutputContext));
        }