Exemple #1
0
        private void ReplaceMethodSignatureToAsync([NotNull] IParametersOwner parametersOwner, [NotNull] IMethodDeclaration methodDeclaration)
        {
            var returnType = parametersOwner.ReturnType;

            var           psiModule = methodDeclaration.GetPsiModule();
            IDeclaredType newReturnValue;

            if (returnType.IsVoid())
            {
                newReturnValue = TypeFactory.CreateTypeByCLRName("System.Threading.Tasks.Task", psiModule);
            }
            else
            {
                var task = TypeFactory.CreateTypeByCLRName("System.Threading.Tasks.Task`1", psiModule).GetTypeElement();
                if (task == null)
                {
                    return;
                }
                newReturnValue = TypeFactory.CreateType(task, returnType);
            }

            var name = GenerateAsyncMethodName(methodDeclaration.DeclaredName);

            SetSignature(methodDeclaration, newReturnValue, name);

            if (awaitEliderChecker.CanElide(methodDeclaration))
            {
                awaitElider.Elide(methodDeclaration);
            }
        }
Exemple #2
0
        private void ReplaceMethodToAsync(IMethodDeclaration method)
        {
            if (!method.IsValid())
            {
                return;
            }

            var methodDeclaredElement = method.DeclaredElement;

            if (methodDeclaredElement == null)
            {
                return;
            }

            var finder = method.GetPsiServices().Finder;
            var usages = finder.FindAllReferences(methodDeclaredElement);

            foreach (var usage in usages)
            {
                var invocation = usage.GetTreeNode().Parent as IInvocationExpression;
                asyncInvocationReplacer.ReplaceInvocation(invocation, GenerateAsyncMethodName(method.DeclaredName), invocation?.IsUnderAsyncDeclaration() ?? false);
            }

            //TODO: ugly hack. think
            while (true)
            {
                var allInvocationReplaced = method
                                            .DescendantsInScope <IInvocationExpression>()
                                            .All(invocationExpression => !invocationConverter.TryReplaceInvocationToAsync(invocationExpression));
                if (allInvocationReplaced)
                {
                    break;
                }
            }

            foreach (var parametersOwnerDeclaration in method
                     .Descendants <IParametersOwnerDeclaration>()
                     .ToEnumerable()
                     .Where(awaitEliderChecker.CanElide))
            {
                awaitElider.Elide(parametersOwnerDeclaration);
            }

            ReplaceMethodSignatureToAsync(methodDeclaredElement, method);
        }
        private bool TryConvertParameterFuncToAsync([NotNull] IInvocationExpression invocationExpression, [NotNull] ParameterCompareResult parameterCompareResult)
        {
            var arguments = invocationExpression.Arguments;

            invocationExpression.PsiModule.GetPsiServices().Transactions.StartTransaction("convertAsyncParameter");
            try
            {
                for (var i = 0; i < arguments.Count; i++)
                {
                    var compareResult = parameterCompareResult.ParameterResults[i];
                    if (compareResult.Action == ParameterCompareResultAction.NeedConvertToAsyncFunc)
                    {
                        var lambdaExpression = arguments[i].Value as ILambdaExpression;
                        if (lambdaExpression == null)
                        {
                            invocationExpression.PsiModule.GetPsiServices().Transactions.RollbackTransaction();
                            return(false);
                        }
                        lambdaExpression.SetAsync(true);
                        var innerInvocationExpressions = lambdaExpression.DescendantsInScope <IInvocationExpression>();
                        foreach (var innerInvocationExpression in innerInvocationExpressions)
                        {
                            if (!TryReplaceInvocationToAsync(innerInvocationExpression))
                            {
                                invocationExpression.PsiModule.GetPsiServices().Transactions.RollbackTransaction();
                                return(false);
                            }
                        }

                        if (awaitEliderChecker.CanElide(lambdaExpression))
                        {
                            awaitElider.Elide(lambdaExpression);
                        }
                    }
                }
            }
            catch (Exception)
            {
                invocationExpression.PsiModule.GetPsiServices().Transactions.RollbackTransaction();
                return(false);
            }

            invocationExpression.PsiModule.GetPsiServices().Transactions.CommitTransaction();
            return(true);
        }