Exemplo n.º 1
0
        private void ConvertArrayCreationWithoutInitializer(CSharpElementFactory factory)
        {
            var currentCreation = myArrayCreationExpression;

            var firstSize  = currentCreation.Sizes[0].NotNull("currentCreation.Sizes[0] != null").CopyWithResolve();
            var secondSize = currentCreation.Sizes[1].NotNull("currentCreation.Sizes[1] != null").CopyWithResolve();

            IArrayCreationExpression newCreation;

            if (currentCreation.TypeUsage == null)
            {
                newCreation = factory.CreateExpression("new [$0][]", currentCreation.Sizes[0]) as IArrayCreationExpression;
            }
            else
            {
                newCreation = factory.CreateExpression("new $0[$1][]", currentCreation.TypeUsage, currentCreation.Sizes[0]) as IArrayCreationExpression;
            }

            Assertion.Assert(newCreation != null, "newCreation != null");
            newCreation = currentCreation.ReplaceBy(newCreation);

            if (myVariableDeclaration is ILocalVariableDeclaration)
            {
                var statement = newCreation.GetContainingStatement().NotNull("statement != null");

                var forInitializer = factory.CreateStatement(
                    "for (int index = 0; index < $0; index++) {$1[index] = new $2[$3];}",
                    firstSize, factory.CreateReferenceExpression(myVariableDeclaration.DeclaredName), myType.ElementType, secondSize);

                StatementUtil.InsertStatement(forInitializer, ref statement, false);
            }
        }
        private static void HandleExpressionBody(IBlock body, CSharpElementFactory factory, IType type, string name,
                                                 DisposableMarker <IReferenceExpression> marker, IReferenceExpression originValue)
        {
            var statement = body.Statements.First().NotNull("body.Statements.First() != null");

            StatementUtil.InsertStatement(factory.CreateStatement("$0 $1;", type, name), ref statement, true);

            var updatedReference = marker.Find(body).NotNull("marker.Find(body) != null");

            updatedReference.ReplaceBy(factory.CreateExpression("($0 = $1)", name, originValue.Copy()));
        }
Exemplo n.º 3
0
 private IDeclarationStatement GetDeclarationStatement(IList <ICSharpExpression> usages, IExpectedTypeConstraint typeConstraint)
 {
     try
     {
         var factory        = CSharpElementFactory.GetInstance(_referenceExpression.GetPsiModule());
         var statement      = CreateStubDeclaration(factory, typeConstraint);
         var insertLocation = CSharpExpressionUtil.GetStatementToBeVisibleFromAll(usages);
         return(StatementUtil.InsertStatement(statement, ref insertLocation, true));
     }
     catch (Exception ex)
     {
         File.AppendAllText("c:\\temp\\MillimanPluginErrors.txt", "Exception on " + DateTime.Now + "\n" + ex + "\n\n");
         throw;
     }
 }
Exemplo n.º 4
0
            protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
            {
                var anchor = myLoopStatement as ICSharpStatement;

                var declaredElement = MonoBehaviourMoveUtil.GetDeclaredElementFromParentDeclaration(myToMove);
                var baseName        = myVariableName ?? MonoBehaviourMoveUtil.CreateBaseName(myToMove, declaredElement);
                var name            = NamingUtil.GetUniqueName(myToMove, baseName, NamedElementKinds.Locals,
                                                               collection => collection.Add(myToMove.Type(), new EntryOptions()),
                                                               de => !de.Equals(declaredElement));

                var factory        = CSharpElementFactory.GetInstance(myToMove);
                var originMyToMove = myToMove.CopyWithResolve();

                MonoBehaviourMoveUtil.RenameOldUsages(myToMove, declaredElement, name, factory);

                ICSharpStatement declaration = factory.CreateStatement("var $0 = $1;", name, originMyToMove);

                StatementUtil.InsertStatement(declaration, ref anchor, true);
                return(null);
            }
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var factory  = CSharpElementFactory.GetInstance(myHighlightedReference);
            var property = (myHighlightedReference.Reference.Resolve().DeclaredElement as IProperty).NotNull();

            // save declaration (in case of expression lambda)
            var declaration = myHighlightedReference.GetContainingFunctionLikeDeclarationOrClosure();
            var type        = property.Type;

            var name = GetUniqueName(myHighlightedReference, property.ShortName);

            IReferenceExpression originValue = myHighlightedReference.Copy();

            for (var i = 0; i < myReferences.Count; i++)
            {
                var reference = myReferences[i];
                myReferences[i] = reference.ReplaceBy(factory.CreateReferenceExpression("$0", name));
            }

            var firstReference = myReferences[0];

            if (declaration is IExpressionBodyOwnerDeclaration expressionBodyOwnerDeclaration &&
                expressionBodyOwnerDeclaration.GetCodeBody().ExpressionBody != null)
            {
                using (var marker = new DisposableMarker <IReferenceExpression>(firstReference))
                {
                    var body = expressionBodyOwnerDeclaration.EnsureStatementMemberBody();
                    HandleExpressionBody(body, factory, type, name, marker, originValue);
                }

                return(null);
            }

            if (declaration is ILambdaExpression lambdaExpression &&
                lambdaExpression.GetCodeBody().ExpressionBody != null)
            {
                using (var marker = new DisposableMarker <IReferenceExpression>(firstReference))
                {
                    var body = lambdaExpression.EnsureStatementLambda();
                    HandleExpressionBody(body, factory, type, name, marker, originValue);
                }
                return(null);
            }

            Assertion.Assert(myCacheAnchor is ICSharpStatement, "myInlineCache is IStatement");
            var statementCacheAnchor = (ICSharpStatement)myCacheAnchor;

            if (myInlineCache) // replace first read with assignment expression
            {
                foreach (var reference in myReferences)
                {
                    if (reference.GetContainingStatement() != myCacheAnchor)
                    {
                        continue;
                    }

                    // is write first???
                    // example: var x = (transform.position = Vector3.Up) + transform.position + transform.position ...
                    // if yes, we have already save our variable in cycle above, if no use inline to cache.
                    if (AssignmentExpressionNavigator.GetByDest(reference.GetContainingParenthesizedExpression()) == null)
                    {
                        reference.ReplaceBy(factory.CreateExpression("($0 = $1)", name, originValue.Copy()));
                    }
                    break;
                }

                var cacheStatement = factory.CreateStatement("$0 $1;", type, name);
                StatementUtil.InsertStatement(cacheStatement, ref statementCacheAnchor, true);
            }
            else
            {
                var cacheStatement = factory.CreateStatement("var $0 = $1;", name, originValue.Copy());
                StatementUtil.InsertStatement(cacheStatement, ref statementCacheAnchor, true);
            }

            if (myRestoreAnchor != null)
            {
                Assertion.Assert(myRestoreAnchor is ICSharpStatement, "myRestoreAnchor is IStatement");
                var statementRestoreAnchor = (ICSharpStatement)myRestoreAnchor;
                if (myInlineRestore)
                {
                    var size = myReferences.Count;
                    for (int i = size - 1; i >= 0; i--)
                    {
                        var reference = myReferences[i];
                        if (reference.GetContainingStatement() == myRestoreAnchor)
                        {
                            reference.ReplaceBy(factory.CreateReferenceExpression("$0", originValue));
                            break;
                        }
                    }
                }
                else
                {
                    var restoreStatement = factory.CreateStatement("$0 = $1;", originValue, name);
                    StatementUtil.InsertStatement(restoreStatement, ref statementRestoreAnchor, false);
                }
            }

            return(null);
        }