Esempio n. 1
0
        public static bool IsExpressionAccessibleInMethod([NotNull] ICSharpExpression expression,
                                                          [NotNull] string methodName)
        {
            expression.GetPsiServices().Locks.AssertReadAccessAllowed();

            if (!expression.IsValid())
            {
                return(false);
            }

            var methodDeclaration = expression.GetContainingNode <IMethodDeclaration>();

            if (methodDeclaration == null)
            {
                return(false);
            }

            var statement = expression.GetContainingStatementLike();

            if (statement == null)
            {
                return(false);
            }

            return(IsAvailableToMoveFromMethodToMethod(expression, methodName));
        }
            protected override IExpressionStatement CreateStatement(CSharpElementFactory factory, ICSharpExpression expression)
            {
                var statement      = (IExpressionStatement)factory.CreateStatement("__ = expression;");
                var newDeclaration = CreateMemberDeclaration(factory);
                var assignment     = (IAssignmentExpression)statement.Expression;

                assignment.SetSource(expression);

                var psiServices       = expression.GetPsiServices();
                var suggestionManager = psiServices.Naming.Suggestion;
                var classDeclaration  = expression.GetContainingNode <IClassDeclaration>().NotNull();

                var suggestion = suggestionManager.CreateEmptyCollection(
                    PluralityKinds.Unknown, expression.Language, true, expression);

                suggestion.Add(expression, new EntryOptions {
                    SubrootPolicy          = SubrootPolicy.Decompose,
                    PredefinedPrefixPolicy = PredefinedPrefixPolicy.Remove
                });

                suggestion.Prepare(newDeclaration.DeclaredElement, new SuggestionOptions {
                    UniqueNameContext = (ITreeNode)classDeclaration.Body ?? classDeclaration
                });

                newDeclaration.SetName(suggestion.FirstName());
                myMemberNames = suggestion.AllNames();

                var memberAnchor = GetAnchorMember(classDeclaration.MemberDeclarations.ToList());
                var newMember    = classDeclaration.AddClassMemberDeclarationAfter(
                    newDeclaration, (IClassMemberDeclaration)memberAnchor);

                myMemberPointer = newMember.CreatePointer();
                return(statement);
            }
Esempio n. 3
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();
                }
            }
        }