Ejemplo n.º 1
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);
        }