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 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);
		}
Example #3
0
 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;
 }