private async Task <Document> RefactorAsync(
            Document document,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ConstructorDeclarationSyntax constructor = Constructor;

            var containingMember = constructor.Parent as MemberDeclarationSyntax;

            SyntaxList <MemberDeclarationSyntax> members = containingMember.GetMembers();

            SyntaxList <MemberDeclarationSyntax> newMembers = members.Replace(
                constructor,
                constructor.AddBodyStatements(CreateAssignments().ToArray()));

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            newMembers = newMembers.InsertRange(
                GetDeclarationIndex(members),
                CreateDeclarations(constructor, semanticModel, cancellationToken));

            return(await document.ReplaceNodeAsync(
                       containingMember,
                       containingMember.SetMembers(newMembers),
                       cancellationToken).ConfigureAwait(false));
        }
        private async Task <Document> RefactorAsync(
            Document document,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            ConstructorDeclarationSyntax constructor = Constructor;

            MemberDeclarationSyntax containingMember = constructor.GetParentMember();

            SyntaxList <MemberDeclarationSyntax> members = containingMember.GetMembers();

            SyntaxList <MemberDeclarationSyntax> newMembers = members.Replace(
                constructor,
                constructor.AddBodyStatements(CreateAssignments().ToArray()));

            newMembers = newMembers.InsertRange(
                GetDeclarationIndex(members),
                CreateDeclarations());

            SyntaxNode newRoot = root.ReplaceNode(
                containingMember,
                containingMember.SetMembers(newMembers));

            return(document.WithSyntaxRoot(newRoot));
        }
예제 #3
0
        private void CreateVariablesAndConstructorSyntax()
        {
            m_constructor = MySyntaxFactory.Constructor(m_scriptClassDeclaration);
            var variables = m_navigator.OfType <MyVisualSyntaxVariableNode>();

            foreach (var variableNode in variables)
            {
                m_fieldDeclarations.Add(variableNode.CreateFieldDeclaration());
                m_constructor = m_constructor.AddBodyStatements(variableNode.CreateInitializationSyntax());
            }
        }
예제 #4
0
        public void AddPropertyParameter(ButlerProperty bProperty)
        {
            if (!_owner.Properties.Contains(bProperty))
            {
                throw new Exception($"Property with name {bProperty.Name} cannot be set through {_owner.Name} constructor if it hasn't been defined in class.");
            }

            string propertyName  = bProperty.Name;
            string parameterName = propertyName.ToCamelCase();

            ParameterSyntax parameterInfo = SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameterName)).WithType(bProperty.Info.Type);

            Info = Info.AddParameterListParameters(parameterInfo);

            ExpressionSyntax           lhs = SyntaxFactory.IdentifierName(propertyName);
            ExpressionSyntax           rhs = SyntaxFactory.IdentifierName(parameterName);
            AssignmentExpressionSyntax assignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, lhs, rhs);

            Info = Info.AddBodyStatements(SyntaxFactory.ExpressionStatement(assignmentExpression));
        }
예제 #5
0
        public Func <SyntaxGenerator, ClassDeclarationSyntax, ClassDeclarationSyntax> GetAppendConstructorExpressionAction(string expression)
        {
            ClassDeclarationSyntax AppendConstructorExpression(SyntaxGenerator syntaxGenerator, ClassDeclarationSyntax node)
            {
                var constructor = node.Members.Where(c => Microsoft.CodeAnalysis.CSharp.CSharpExtensions.Kind(c) == SyntaxKind.ConstructorDeclaration).FirstOrDefault();

                if (constructor != null)
                {
                    ConstructorDeclarationSyntax constructorNode = (ConstructorDeclarationSyntax)constructor;
                    StatementSyntax statementExpression          = SyntaxFactory.ParseStatement(expression);
                    if (!statementExpression.FullSpan.IsEmpty)
                    {
                        constructorNode = constructorNode.AddBodyStatements(statementExpression);
                        node            = node.ReplaceNode(constructor, constructorNode).NormalizeWhitespace();
                    }
                }
                return(node);
            }

            return(AppendConstructorExpression);
        }
        private async Task <Document> RefactorAsync(
            Document document,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ConstructorDeclarationSyntax constructor = Constructor;

            MemberDeclarationListInfo info = SyntaxInfo.MemberDeclarationListInfo(constructor.Parent);

            SyntaxList <MemberDeclarationSyntax> members = info.Members;

            SyntaxList <MemberDeclarationSyntax> newMembers = members.Replace(
                constructor,
                constructor.AddBodyStatements(CreateAssignments().ToArray()));

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            newMembers = newMembers.InsertRange(
                GetDeclarationIndex(members),
                CreateDeclarations(constructor, semanticModel, cancellationToken));

            return(await document.ReplaceMembersAsync(info, newMembers, cancellationToken).ConfigureAwait(false));
        }
        public override SyntaxNode VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            // only append to constructor with no args
            if (node.ParameterList.Parameters == null |
                node.ParameterList.Parameters.Count == 0)
            {
                visitedConstructorNoArgs  = true;
                indexConstructorFuncNoArg = classParent.Members.IndexOf(node);

                string statementExp = "void func(){\nSystem.IO.File.AppendAllText(\"" + tempFileName + "\", \"" +
                                      node.Identifier.ValueText + "\\t\");\n}";
                SyntaxTree tree         = CSharpSyntaxTree.ParseText(statementExp);
                var        tempRoot     = (CompilationUnitSyntax)tree.GetRoot();
                var        tempMethod   = (MethodDeclarationSyntax)tempRoot.Members[0];
                var        newStatement = tempMethod.Body.Statements[0];

                var tempNode       = node.AddBodyStatements(LocalStatementCaller(CsFileModifier.METHOD_NAME));
                var firstStatement = tempNode.Body.Statements[0];
                return(tempNode.InsertNodesBefore(firstStatement, new List <SyntaxNode>()
                {
                    newStatement
                }));
            }
            else
            {
                visitedConstructorHasArgs = true;
                foreach (StatementSyntax statement in node.Body.Statements)
                {
                    string temp = statement.ToFullString();
                    if (statement.ToFullString().Contains(CsFileModifier.INIT_COMPONENT_STATEMENT))
                    {
                        this.containDefaultInitStatement = true;
                        break;
                    }
                }
            }
            return(base.VisitConstructorDeclaration(node));
        }
예제 #8
0
 public static ConstructorDeclarationSyntax AddBodyStatements(this ConstructorDeclarationSyntax syntax, IEnumerable <StatementSyntax> parameters)
 {
     return(syntax.AddBodyStatements(parameters.ToArray()));
 }
예제 #9
0
        private void CreateMethods()
        {
            // Generate Interface methods
            if (!string.IsNullOrEmpty(m_objectBuilder.Interface))
            {
                var methodNodes = m_navigator.OfType <MyVisualSyntaxInterfaceMethodNode>();
                foreach (var methodNode in methodNodes)
                {
                    var methodSyntax = methodNode.GetMethodDeclaration();
                    ProcessNodes(new[] { methodNode }, ref methodSyntax);
                    m_methodDeclarations.Add(methodSyntax);
                }
            }

            var events = m_navigator.OfType <MyVisualSyntaxEventNode>();

            events.AddRange(m_navigator.OfType <MyVisualSyntaxKeyEventNode>());
            // Generate Event methods
            // Take all events of same name and make a method out of theire bodies.
            while (events.Count > 0)
            {
                var firstEvent         = events[0];
                var eventsWithSameName = events.Where(@event => @event.ObjectBuilder.Name == firstEvent.ObjectBuilder.Name);
                var methodDeclaration  = MySyntaxFactory.PublicMethodDeclaration(
                    firstEvent.EventName,
                    SyntaxKind.VoidKeyword,
                    firstEvent.ObjectBuilder.OutputNames,
                    firstEvent.ObjectBuilder.OuputTypes);

                ProcessNodes(eventsWithSameName, ref methodDeclaration);
                // Bind with VisualScriptingProxy in constructor.
                m_constructor = m_constructor.AddBodyStatements(
                    MySyntaxFactory.DelegateAssignment(
                        firstEvent.ObjectBuilder.Name,
                        methodDeclaration.Identifier.ToString())
                    );
                // unBind from visualScriptingProxy in dispose method
                m_disposeMethod = m_disposeMethod.AddBodyStatements(
                    MySyntaxFactory.DelegateRemoval(
                        firstEvent.ObjectBuilder.Name,
                        methodDeclaration.Identifier.ToString())
                    );

                m_methodDeclarations.Add(methodDeclaration);

                events.RemoveAll(@event => eventsWithSameName.Contains(@event));
            }

            // There can be only one method from single input node.
            // Input nodes are of type Event.
            var inputs  = m_navigator.OfType <MyVisualSyntaxInputNode>();
            var outputs = m_navigator.OfType <MyVisualSyntaxOutputNode>();

            if (inputs.Count > 0)
            {
                Debug.Assert(inputs.Count == 1);

                var input = inputs[0];
                MethodDeclarationSyntax methodDeclaration = null;
                if (outputs.Count > 0)
                {
                    List <string> outputParamNames = new List <string>(outputs[0].ObjectBuilder.Inputs.Count);
                    List <string> outputParamTypes = new List <string>(outputs[0].ObjectBuilder.Inputs.Count);

                    foreach (var outputData in outputs[0].ObjectBuilder.Inputs)
                    {
                        outputParamNames.Add(outputData.Name);
                        outputParamTypes.Add(outputData.Type);
                    }

                    methodDeclaration = MySyntaxFactory.PublicMethodDeclaration("RunScript", SyntaxKind.BoolKeyword, input.ObjectBuilder.OutputNames, input.ObjectBuilder.OuputTypes, outputParamNames, outputParamTypes);
                }
                else
                {
                    methodDeclaration = MySyntaxFactory.PublicMethodDeclaration("RunScript", SyntaxKind.BoolKeyword, input.ObjectBuilder.OutputNames, input.ObjectBuilder.OuputTypes);
                }

                ProcessNodes(new[] { input }, ref methodDeclaration, new[] { SyntaxFactory.ReturnStatement(SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression)) });
                m_methodDeclarations.Add(methodDeclaration);
            }
        }