Beispiel #1
0
        private void ReplaceMethodToAsync(IFinder finder, IPsiModule psiModule, CSharpElementFactory factory, IMethodDeclaration method)
        {
            if (!method.IsValid())
            {
                return;
            }

            var methodDeclaredElement = method.DeclaredElement;

            if (methodDeclaredElement == null)
            {
                return;
            }

            var usages = finder.FindAllReferences(methodDeclaredElement);

            foreach (var usage in usages)
            {
                var invocation = usage.GetTreeNode().Parent as IInvocationExpression;
                var containingFunctionDeclarationIgnoringClosures = invocation?.InvokedExpression.GetContainingFunctionDeclarationIgnoringClosures();
                if (containingFunctionDeclarationIgnoringClosures == null)
                {
                    continue;
                }
                AsyncHelper.ReplaceCallToAsync(invocation, factory, containingFunctionDeclarationIgnoringClosures.IsAsync);
            }
            var invocationExpressions = method.Body.Descendants <IInvocationExpression>();

            foreach (var invocationExpression in invocationExpressions)
            {
                AsyncHelper.TryReplaceInvocationToAsync(invocationExpression, factory, psiModule);
            }

            AsyncHelper.ReplaceMethodSignatureToAsync(methodDeclaredElement, psiModule, method);
        }
Beispiel #2
0
        public static List <string> Get([NotNull] IMethodDeclaration method)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (!method.IsValid())
            {
                return((List <string>)EmptyList <string> .InstanceList);
            }
            var declared = method.DeclaredElement;

            if (declared != null)
            {
                var shortName = declared.ShortName;
                var newName   = shortName + Async;
                var names     = new List <string> {
                    newName
                };

                foreach (var pattern in Typos)
                {
                    if (shortName.EndsWith(pattern))
                    {
                        var result = shortName.SubstringBefore(pattern) + Async;
                        names.Add(result);
                    }
                }
                return(names);
            }
            return((List <string>)EmptyList <string> .InstanceList);
        }
        public static bool IsAvailable([CanBeNull] IMethodDeclaration methodDeclaration)
        {
            if (methodDeclaration == null)
            {
                return(false);
            }

            var declaredElement = methodDeclaration.DeclaredElement;

            return(declaredElement != null && methodDeclaration.IsValid() &&
                   !BurstCodeAnalysisUtil.IsBurstProhibitedFunction(declaredElement));
        }
        public static bool IsAvailable([CanBeNull] IMethodDeclaration methodDeclaration)
        {
            if (methodDeclaration == null)
            {
                return(false);
            }

            methodDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed();

            var declaredElement = methodDeclaration.DeclaredElement;

            return(declaredElement != null && methodDeclaration.IsValid() &&
                   !PerformanceCriticalCodeStageUtil.IsPerformanceCriticalRootMethod(methodDeclaration));
        }
Beispiel #5
0
        public static bool IsAvailable([CanBeNull] IMethodDeclaration methodDeclaration)
        {
            if (methodDeclaration == null)
            {
                return(false);
            }

            methodDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed();

            var declaredElement = methodDeclaration.DeclaredElement;

            return(declaredElement != null && methodDeclaration.IsValid() &&
                   !BurstCodeAnalysisUtil.IsBurstProhibitedFunction(declaredElement));
        }
Beispiel #6
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);
        }
Beispiel #7
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);
            }
            while (true)
            {
                var allInvocationReplaced = method
                                            .Body
                                            .Descendants <IInvocationExpression>()
                                            .ToEnumerable()
                                            .All(invocationExpression => !TryReplaceInvocationToAsync(invocationExpression));
                if (allInvocationReplaced)
                {
                    break;
                }
            }

            ReplaceMethodSignatureToAsync(methodDeclaredElement, method);
        }
 public bool IsValid()
 {
     return(MethodDeclaration.IsValid());
 }
 public bool IsValid() => myMethodDeclaration == null || myMethodDeclaration.IsValid();
 public bool IsValid()
 {
     return(myMethodDeclaration != null && myMethodDeclaration.IsValid());
 }