private void AddReferenceToNewNamespace(ElementReferenceExpression namespaceReferenceExpression, TextDocument Doc)
 {
     var NamespaceReference = new NamespaceReference(namespaceReferenceExpression.FullSignature);
     var Code = CodeRush.CodeMod.GenerateCode(NamespaceReference, false);
     var LastNamespaceReference = CodeRush.Refactoring.FindAllNamespaceReferences(Doc.FileNode).LastOrDefault();
     if (LastNamespaceReference != null)
     {
         Doc.QueueInsert(LastNamespaceReference.Range.Start.OffsetPoint(1, 0), Code);
     }
     else
     {
         var FirstNamespace = Doc.FileNode.FindChildByElementType(LanguageElementType.Namespace);
         Doc.QueueInsert(FirstNamespace.Range.Start, Code);
     }
 }
        private static bool StaticContextIsMet(ElementReferenceExpression expression, Property property)
        {
            if (!property.IsStatic)
            {
                return(false);
            }
            if (expression.LastChild == null)
            {
                return(true);
            }
            IElement lastChildDeclaration   = expression.LastChild.GetDeclaration();
            IElement parentClassDeclaration = property.GetParentClassInterfaceOrStruct().GetDeclaration();

            return(lastChildDeclaration.RootNamespaceFullName == parentClassDeclaration.RootNamespaceFullName);
        }
Exemple #3
0
        private static string GetGeneratedCode(LanguageElement originalCall)
        {
            if (!(originalCall is IHasArguments))
            {
                return(null);
            }
            ExpressionCollection originalArguments = (originalCall as IHasArguments).Arguments;

            if (originalArguments == null)
            {
                return(null);
            }
            LanguageElement replacementCall = originalCall.Clone() as LanguageElement;

            (replacementCall as IHasArguments).Arguments.Clear();

            IWithParameters declaration = GetValidDeclaration(originalCall);

            if (declaration == null ||
                !(declaration.Parameters.Count == originalArguments.Count ||
                  (declaration.Parameters.Count == originalArguments.Count + 1 && HasParamArray(declaration)) ||
                  (declaration.Parameters.Count == originalArguments.Count + 1 && IsExtensionMethod(declaration))
                  ))
            {
                return(null);
            }
            int startParamIndex = 0;

            if (IsExtensionMethod(declaration))
            {
                startParamIndex = 1;
            }

            for (int i = 0; i < originalArguments.Count; i++)
            {
                Expression originalArgument = originalArguments[i];
                AttributeVariableInitializer namedArgument = originalArgument as AttributeVariableInitializer;
                if (namedArgument == null)
                {
                    ElementReferenceExpression leftSide = new ElementReferenceExpression(declaration.Parameters[startParamIndex + i].Name);
                    namedArgument           = new AttributeVariableInitializer();
                    namedArgument.LeftSide  = leftSide;
                    namedArgument.RightSide = originalArgument.Clone() as Expression;
                }
                (replacementCall as IHasArguments).Arguments.Add(namedArgument);
            }
            return(CodeRush.CodeMod.GenerateCode(replacementCall, true));
        }
Exemple #4
0
        internal void ReplaceGetElementProperty(ApplyContentEventArgs ea)
        {
            ElementReferenceExpression ex = ea.Element as ElementReferenceExpression;
            var    elementName            = ex.Name;
            string source = CodeRush.Language.GenerateElement(ex);

            if (elementName == "value")
            {
                source = GetSource(ex.FirstNode as MethodCallExpression).Replace("get(0)", "val()");
            }
            if (elementName == "checked")
            {
                source = GetSource(ex.FirstNode as MethodCallExpression).Replace("get(0)", "is(\":checked\")");
            }
            ea.TextDocument.Replace(ex.Range, source, "Convert to jQuery");
        }
Exemple #5
0
        private Set GetOnPropertyChangedLambdaSetter(ElementBuilder eb, Property propertyClone, string varName)
        {
            Set setter      = eb.BuildSetter();
            If  ifStatement = eb.BuildIf(eb.OpNotEquals("value", varName));

            ifStatement.AddNode(eb.BuildAssignment(varName, "value"));
            ExpressionCollection args   = new ExpressionCollection();
            LambdaExpression     lambda = new LambdaExpression();
            var propAccess = new ElementReferenceExpression(propertyClone.Name);

            lambda.AddNode(propAccess);
            args.Add(lambda);
            var propChangedCall = eb.BuildMethodCall("RaisePropertyChanged", args, null);

            ifStatement.AddNode(propChangedCall);
            setter.AddNode(ifStatement);
            return(setter);
        }
        private void ChangeToBaseCallRefactoringProvider_CheckAvailability(object sender, CheckContentAvailabilityEventArgs ea)
        {
            ElementReferenceExpression expression = null;
            var @return = GetElement <Return>(ea.Element);

            if (@return != null)
            {
                expression = @return.Expression as ElementReferenceExpression;
            }
            var assignment = GetElement <Assignment>(ea.Element);

            if (assignment != null)
            {
                expression = assignment.LeftSide as ElementReferenceExpression;
            }
            if (expression != null && expression.InsideProperty)
            {
                Property property = expression.GetProperty();
                ea.Available = ExpressionReferencesPropertyInInstanceContext(expression, property) && PropertyIsOverriden(property);
            }
        }
    private static string GetGeneratedCode(LanguageElement originalCall)
		{
      if (!(originalCall is IHasArguments))
				return null;
      ExpressionCollection originalArguments = (originalCall as IHasArguments).Arguments;
			if (originalArguments == null)
				return null;
      LanguageElement replacementCall = originalCall.Clone() as LanguageElement;
      (replacementCall as IHasArguments).Arguments.Clear();
      
      IWithParameters declaration = GetValidDeclaration(originalCall);

      if (declaration == null ||
        !(declaration.Parameters.Count == originalArguments.Count || 
        (declaration.Parameters.Count == originalArguments.Count + 1 && HasParamArray(declaration))
        || (declaration.Parameters.Count == originalArguments.Count + 1 && IsExtensionMethod(declaration))
        ))
				return null;
      int startParamIndex = 0;
      if (IsExtensionMethod(declaration))
        startParamIndex = 1;

			for (int i = 0; i < originalArguments.Count; i++)
			{
        Expression originalArgument = originalArguments[i];
        AttributeVariableInitializer namedArgument = originalArgument as AttributeVariableInitializer;
        if (namedArgument == null)
        {
          ElementReferenceExpression leftSide = new ElementReferenceExpression(declaration.Parameters[startParamIndex + i].Name);
          namedArgument = new AttributeVariableInitializer();
          namedArgument.LeftSide = leftSide;
          namedArgument.RightSide = originalArgument.Clone() as Expression;
        }
        (replacementCall as IHasArguments).Arguments.Add(namedArgument);
			}
			return CodeRush.CodeMod.GenerateCode(replacementCall, true);
		}
 private static void AddDisposeImplementer(ElementBuilder elementBuilder)
 {
     Method disposeMethod = elementBuilder.AddMethod(null, null, STR_Dispose);
       // If implicit interface implementation is supported by the language?
       disposeMethod.Visibility = MemberVisibility.Public;
       Expression newCollection = new ElementReferenceExpression("IDisposable.Dispose");
       disposeMethod.AddImplementsExpression(newCollection);
       PrimitiveExpression booleanTrue = GetBooleanLiteral(true);
       ExpressionCollection argumentsCollection = new ExpressionCollection();
       argumentsCollection.Add(booleanTrue);
       elementBuilder.AddMethodCall(disposeMethod, STR_Dispose, argumentsCollection, null);
       string thisReference = CodeRush.Language.GenerateElement(new ThisReferenceExpression());
       elementBuilder.AddMethodCall(disposeMethod, "GC.SuppressFinalize", new string[]
       { thisReference
       });
 }
        private void targetPicker1_TargetSelected(object sender, TargetSelectedEventArgs ea)
        {
            ElementBuilder elementBuilder = new ElementBuilder();
            bool missingDefaultConstructor = _TypeElement.HasDefaultConstructor();
            if (missingDefaultConstructor)
            {
                Method defaultConstructor = elementBuilder.BuildConstructor(null);
                defaultConstructor.Visibility = MemberVisibility.Public;
                defaultConstructor.Name = _TypeElement.Name;
                elementBuilder.AddNode(null, defaultConstructor);
            }
            Method constructor = elementBuilder.BuildConstructor(null);
            constructor.Visibility = MemberVisibility.Public;
            constructor.Name = _TypeElement.Name;
            elementBuilder.AddNode(null, constructor);

            foreach (Expression initializer in _InitializerExpression.Initializers)
            {
                MemberInitializerExpression memberInitializerExpression = initializer as MemberInitializerExpression;
                if (memberInitializerExpression == null)
                    continue;

                string parameterName = FormatAsParameter(memberInitializerExpression.Name);
                IElement initializerType = memberInitializerExpression.GetInitializerType();
                if (initializerType != null)
                {
                    Param param = new Param(initializerType.Name, parameterName);
                    constructor.Parameters.Add(param);

                    Assignment assignment = new Assignment();
                    ElementReferenceExpression leftSide = new ElementReferenceExpression(memberInitializerExpression.Name);
                    if (CodeRush.Language.IdentifiersMatch(memberInitializerExpression.Name, parameterName))
                    {
                        // Old way of building a "this.Xxxx" expression:
                        //string selfQualifier = CodeRush.Language.GenerateElement(new ThisReferenceExpression());
                        //leftSide = new ElementReferenceExpression(selfQualifier +  CodeRush.Language.MemberAccessOperator + memberInitializerExpression.Name);

                        // Recommended way of building a "this.Xxxx" expression:
                        leftSide = new QualifiedElementReference(memberInitializerExpression.Name);
                        leftSide.Qualifier = new ThisReferenceExpression();
                    }
                    assignment.LeftSide = leftSide;
                    assignment.Expression = new ElementReferenceExpression(parameterName);
                    constructor.AddNode(assignment);
                }
            }
            string newConstructorCode = elementBuilder.GenerateCode();
            // Use FileChange for multi-file changes...
            FileChange newConstructor = new FileChange();
            newConstructor.Path = _TypeElement.FirstFile.Name;
            newConstructor.Range = new SourceRange(ea.Location.SourcePoint, ea.Location.SourcePoint);
            newConstructor.Text = newConstructorCode;
            _Changes.Add(newConstructor);

            DevExpress.DXCore.TextBuffers.ICompoundAction action = CodeRush.TextBuffers.NewMultiFileCompoundAction("Create Constructor from Initializer");
            try
            {
                CodeRush.File.ApplyChanges(_Changes, true, false);
            }
            finally
            {
                action.Close();
                _Changes.Clear();
            }
        }
 private Set GetOnPropertyChangedLambdaSetter(ElementBuilder eb, Property propertyClone, string varName)
 {
     Set setter = eb.BuildSetter();
     If ifStatement = eb.BuildIf(eb.OpNotEquals("value", varName));
     ifStatement.AddNode(eb.BuildAssignment(varName, "value"));
     ExpressionCollection args = new ExpressionCollection();
     LambdaExpression lambda = new LambdaExpression();
     var propAccess = new ElementReferenceExpression(propertyClone.Name);
     lambda.AddNode(propAccess);
     args.Add(lambda);
     var propChangedCall = eb.BuildMethodCall("RaisePropertyChanged", args, null);
     ifStatement.AddNode(propChangedCall);
     setter.AddNode(ifStatement);
     return setter;
 }
 private static bool ExpressionReferencesPropertyInInstanceOrStaticContext(ElementReferenceExpression expression, Property property)
 {
     return(ExpressionReferencesProperty(expression, property) &&
            (InstanceContextIsMet(expression, property) ||
             StaticContextIsMet(expression, property)));
 }
 private static bool ExpressionReferencesParentPropertyInInstanceOrStaticContext(ElementReferenceExpression expression)
 {
     return(expression != null && expression.InsideProperty &&
            ExpressionReferencesPropertyInInstanceOrStaticContext(expression, expression.GetProperty()));
 }
 private static bool InstanceContextIsMet(ElementReferenceExpression expression, Property property)
 {
     return(!property.IsStatic &&
            (expression.LastChild == null || expression.LastChild is ThisReferenceExpression));
 }
 private static bool ExpressionReferencesProperty(ElementReferenceExpression expression, Property property)
 {
     return(expression != null &&
            property != null &&
            expression.Name == property.Name);
 }