Ejemplo n.º 1
0
        public static void ReplaceCallToAsync([NotNull] IInvocationExpression invocationExpression, [NotNull] CSharpElementFactory factory, bool useAsync)
        {
            var returnType          = invocationExpression.Type();
            var referenceExpression = invocationExpression.FirstChild as IReferenceExpression;

            if (referenceExpression?.NameIdentifier == null)
            {
                return;
            }

            var newMethodName = GenerateAsyncMethodName(referenceExpression.NameIdentifier.Name);

            var newReferenceExpression = referenceExpression.QualifierExpression == null
                ? factory.CreateReferenceExpression("$0", newMethodName)
                : factory.CreateReferenceExpression("$0.$1", referenceExpression.QualifierExpression, newMethodName);

            newReferenceExpression.SetTypeArgumentList(referenceExpression.TypeArgumentList);

            var callFormat = useAsync
                ? "await $0($1).ConfigureAwait(false)"
                : returnType.IsVoid() ? "$0($1).Wait()" : "$0($1).Result";
            var awaitExpression = factory.CreateExpression(callFormat, newReferenceExpression, invocationExpression.ArgumentList);

            invocationExpression.ReplaceBy(awaitExpression);
        }
            protected override IForStatement CreateStatement(CSharpElementFactory factory,
                                                       ICSharpExpression expression)
            {
                var hasLength = (LengthName != null);
                var template = hasLength ? "for(var x=$0;x>=0;x--)" : "for(var x=$0;x>0;x--)";
                var forStatement = (IForStatement) factory.CreateStatement(
                  template + EmbeddedStatementBracesTemplate, expression);

                var variable = (ILocalVariableDeclaration) forStatement.Initializer.Declaration.Declarators[0];
                var initializer = (IExpressionInitializer) variable.Initial;

                if (!hasLength)
                {
                  var value = initializer.Value.ReplaceBy(expression);
                  value.ReplaceBy(value);
                }
                else
                {
                  var lengthAccess = factory.CreateReferenceExpression("expr.$0", LengthName);
                  lengthAccess = initializer.Value.ReplaceBy(lengthAccess);
                  lengthAccess.QualifierExpression.NotNull().ReplaceBy(expression);
                  lengthAccess.ReplaceBy(factory.CreateExpression("$0 - 1", lengthAccess));
                }

                return forStatement;
            }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
            protected override IForStatement CreateStatement(CSharpElementFactory factory, ICSharpExpression expression)
            {
                var hasLength    = (LengthName != null);
                var template     = hasLength ? "for(var x=$0;x>=0;x--)" : "for(var x=$0;x>0;x--)";
                var forStatement = (IForStatement)factory.CreateStatement(
                    template + EmbeddedStatementBracesTemplate, expression);

                var variable    = (ILocalVariableDeclaration)forStatement.Initializer.Declaration.Declarators[0];
                var initializer = (IExpressionInitializer)variable.Initial;

                if (!hasLength)
                {
                    var value = initializer.Value.ReplaceBy(expression);
                    value.ReplaceBy(value);
                }
                else
                {
                    var lengthAccess = factory.CreateReferenceExpression("expr.$0", LengthName);
                    lengthAccess = initializer.Value.ReplaceBy(lengthAccess);
                    lengthAccess.QualifierExpression.NotNull().ReplaceBy(expression);
                    lengthAccess.ReplaceBy(factory.CreateExpression("$0 - 1", lengthAccess));
                }

                return(forStatement);
            }
Ejemplo n.º 5
0
        protected override ICSharpExpression CreateReplacementExpression(ICSharpExpression expression, IDeclaredElement declaredElement)
        {
            CSharpElementFactory factory = CSharpElementFactory.GetInstance(expression);

            return(factory.CreateReferenceExpression("$0.$1",
                                                     ((IReferenceExpression)expression).QualifierExpression,
                                                     declaredElement.ShortName));
        }
Ejemplo n.º 6
0
        public static void RenameOldUsages([NotNull] ICSharpExpression originExpression,
                                           [CanBeNull] IDeclaredElement localVariableDeclaredElement,
                                           [NotNull] string newName, [NotNull] CSharpElementFactory factory)
        {
            originExpression.GetPsiServices().Locks.AssertReadAccessAllowed();

            var statement = ExpressionStatementNavigator.GetByExpression(originExpression);

            if (statement != null)
            {
                statement.RemoveOrReplaceByEmptyStatement();
            }
            else
            {
                if (localVariableDeclaredElement == null)
                {
                    originExpression.ReplaceBy(factory.CreateReferenceExpression(newName));
                }
                else if (!newName.Equals(localVariableDeclaredElement.ShortName))
                {
                    var provider = DefaultUsagesProvider.Instance;
                    var usages   = provider.GetUsages(localVariableDeclaredElement,
                                                      originExpression.GetContainingNode <IMethodDeclaration>().NotNull("scope != null"));
                    originExpression.GetContainingStatement().NotNull("expression.GetContainingStatement() != null")
                    .RemoveOrReplaceByEmptyStatement();
                    foreach (var usage in usages)
                    {
                        if (usage.IsValid() && usage is IReferenceExpression node)
                        {
                            node.ReplaceBy(factory.CreateReferenceExpression(newName));
                        }
                    }
                }
                else
                {
                    DeclarationStatementNavigator.GetByVariableDeclaration(
                        LocalVariableDeclarationNavigator.GetByInitial(
                            ExpressionInitializerNavigator.GetByValue(
                                originExpression.GetContainingParenthesizedExpression())))
                    ?.RemoveOrReplaceByEmptyStatement();
                }
            }
        }
        public override MethodInvocation ProcessUsage(IReference reference)
        {
            var referenceExpression = reference.GetTreeNode() as IReferenceExpression;

            if (referenceExpression == null)
            {
                Driver.AddConflict(ReferenceConflict.CreateError(reference, "{0} can not be updated correctly.", "Usage"));
                return(null);
            }

            bool isExtensionMethod           = referenceExpression.IsExtensionMethod();
            IInvocationExpression invocation = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression);

            if (invocation == null)
            {
                Driver.AddConflict(ReferenceConflict.CreateError(reference, "{0} can not be updated correctly.", "Usage"));
                return(null);
            }

            ITreeNode element = GetArgument(invocation, isExtensionMethod);

            var   argument = element as ICSharpArgument;
            IType type     = argument != null?GetTypeOfValue(argument.Value) : GetTypeOfValue(element);

            if (type == null || !type.CanUseExplicitly(invocation))
            {
                Driver.AddConflict(ReferenceConflict.CreateError(
                                       reference, "Argument of {0} is not valid 'typeof' expression.", "usage"));
                return(null);
            }

            // we can rely on resolve result since method declaration is not yet changed.
            ResolveResultWithInfo resolveResult = reference.Resolve();
            ISubstitution         substitution  = resolveResult.Result.Substitution;
            var method = resolveResult.DeclaredElement as IMethod;

            if (method == null)
            {
                return(null);
            }

            if (argument != null)
            {
                invocation.RemoveArgument(argument);
                return(new MethodInvocation(reference, type, method, substitution));
            }

            CSharpElementFactory factory = CSharpElementFactory.GetInstance(invocation.GetPsiModule());
            IReferenceExpression newInvokedExpression =
                invocation.InvokedExpression.ReplaceBy(factory.CreateReferenceExpression("$0", Executer.Method));

            return(new MethodInvocation(newInvokedExpression.Reference, type, method, substitution));
        }
Ejemplo n.º 8
0
        private ICSharpExpression GetMappingTypeName(IType scalarType)
        {
            var columnTypeNameClass     = provider.GetType($"SKBKontur.Billy.Core.Common.Quering.ColumnTypeNames");
            var columnTypeNameClassType = columnTypeNameClass.GetTypeElement();

            // ReSharper disable once InconsistentNaming
            ICSharpExpression createExpression(string x) => columnTypeNameClassType != null
                ? factory.CreateReferenceExpression("$0.$1", columnTypeNameClassType.ShortName, x)
                : factory.CreateExpression($"ColumnTypeNames.{x}");

            if (scalarType.IsNullable())
            {
                scalarType = scalarType.GetNullableUnderlyingType();
            }

            if (scalarType.IsInt() || scalarType.IsEnumType())
            {
                return(createExpression(Constants.Int));
            }

            if (scalarType.IsGuid())
            {
                return(createExpression(Constants.UniqueIdentifier));
            }

            if (scalarType.IsString())
            {
                return(createExpression(Constants.NVarChar));
            }

            if (scalarType.IsBool())
            {
                return(createExpression(Constants.Bit));
            }

            if (scalarType.IsDateTime())
            {
                return(createExpression(Constants.DateTime2));
            }

            if (scalarType.IsLong())
            {
                return(createExpression(Constants.BigInt));
            }

            if (scalarType.IsDecimal())
            {
                return(createExpression(Constants.Decimal));
            }

            return(factory.CreateExpression("TODO"));
        }
     protected override void PlaceExpression(
 IForStatement forStatement, ICSharpExpression expression, CSharpElementFactory factory)
     {
         var condition = (IRelationalExpression) forStatement.Condition;
         if (LengthPropertyName == null)
         {
           condition.RightOperand.ReplaceBy(expression);
         }
         else
         {
           var lengthAccess = factory.CreateReferenceExpression("expr.$0", LengthPropertyName);
           lengthAccess = condition.RightOperand.ReplaceBy(lengthAccess);
           lengthAccess.QualifierExpression.NotNull().ReplaceBy(expression);
         }
     }
            protected override void PlaceExpression(
        IForStatement forStatement, ICSharpExpression expression, CSharpElementFactory factory)
            {
                var variable = (ILocalVariableDeclaration)forStatement.Initializer.Declaration.Declarators[0];
                var initializer = (IExpressionInitializer)variable.Initial;

                if (LengthPropertyName == null)
                {
                  var value = initializer.Value.ReplaceBy(expression);
                  value.ReplaceBy(factory.CreateExpression("$0 - 1", value));
                }
                else
                {
                  var lengthAccess = factory.CreateReferenceExpression("expr.$0", LengthPropertyName);
                  lengthAccess = initializer.Value.ReplaceBy(lengthAccess);
                  lengthAccess.QualifierExpression.NotNull().ReplaceBy(expression);
                  lengthAccess.ReplaceBy(factory.CreateExpression("$0 - 1", lengthAccess));
                }
            }
Ejemplo n.º 11
0
            protected override IForStatement CreateStatement(CSharpElementFactory factory, ICSharpExpression expression)
            {
                var template     = "for(var x=0;x<$0;x++)" + EmbeddedStatementBracesTemplate;
                var forStatement = (IForStatement)factory.CreateStatement(template, expression);

                var condition = (IRelationalExpression)forStatement.Condition;

                if (LengthName == null)
                {
                    condition.RightOperand.ReplaceBy(expression);
                }
                else
                {
                    var lengthAccess = factory.CreateReferenceExpression("expr.$0", LengthName);
                    lengthAccess = condition.RightOperand.ReplaceBy(lengthAccess);
                    lengthAccess.QualifierExpression.NotNull().ReplaceBy(expression);
                }

                return(forStatement);
            }
Ejemplo n.º 12
0
            protected override IForStatement CreateStatement(CSharpElementFactory factory,
                                                       ICSharpExpression expression)
            {
                var template = "for(var x=0;x<$0;x++)" + EmbeddedStatementBracesTemplate;
                var forStatement = (IForStatement) factory.CreateStatement(template, expression);

                var condition = (IRelationalExpression) forStatement.Condition;
                if (LengthName == null)
                {
                  condition.RightOperand.ReplaceBy(expression);
                }
                else
                {
                  var lengthAccess = factory.CreateReferenceExpression("expr.$0", LengthName);
                  lengthAccess = condition.RightOperand.ReplaceBy(lengthAccess);
                  lengthAccess.QualifierExpression.NotNull().ReplaceBy(expression);
                }

                return forStatement;
            }