// Check that invocation immediately under declaration statement as initializer (Parenthesized Expression is allowed)
        private bool IsInvocationInitializer()
        {
            var expression                  = myInvocationExpression.GetContainingParenthesizedExpression();
            var currentInitializer          = ExpressionInitializerNavigator.GetByValue(expression);
            var selectedDeclarator          = LocalVariableDeclarationNavigator.GetByInitial(currentInitializer);
            var multiplyVariableDeclaration = MultipleLocalVariableDeclarationNavigator.GetByDeclarator(selectedDeclarator);

            return(DeclarationStatementNavigator.GetByDeclaration(multiplyVariableDeclaration) != null);
        }
示例#2
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();
                }
            }
        }