Ejemplo n.º 1
0
        private void CalculateCode(ParentingStatement parentingStatement)
        {
            _NewCode = null;
            if (parentingStatement == null)
                return;

            LanguageElement parentToReplace = null;
            ParentToSingleStatement grandparent = parentingStatement.Parent as ParentToSingleStatement;
            if (grandparent != null)
            {
                if (!grandparent.HasBlock)
                {
                    ElementCloneOptions cloneOptions = new ElementCloneOptions() { CloneNodes = false };
                    parentToReplace = grandparent.Clone(cloneOptions) as LanguageElement;
                }
            }
            LanguageElement endNode;
            LanguageElement startNode;
            parentingStatement.GetFullBlockNodes(out startNode, out endNode);
            LanguageElement elementToCheck = startNode;
            ElementBuilder elementBuilder = new ElementBuilder();
            if (parentToReplace != null)
                elementBuilder.AddNode(null, parentToReplace);

            while (elementToCheck != null)
            {
                ParentingStatement parent = elementToCheck as ParentingStatement;
                if (parent != null && parent.ElementType != LanguageElementType.Catch)
                {
                    foreach (IElement element in parent.Nodes)
                    {
                        LanguageElement child = element.Clone() as LanguageElement;
                        if (child != null)
                            elementBuilder.AddNode(parentToReplace, child);
                    }
                }
                if (elementToCheck == endNode)
                    break;

                elementToCheck = elementToCheck.NextCodeSibling;
            }

            if (parentToReplace != null)
                _DeleteRange = parentToReplace.Range;
            else
                _DeleteRange = new SourceRange(startNode.Range.Top, endNode.Range.Bottom);

            _NewCode = elementBuilder.GenerateCode();

            if (_NewCode.EndsWith(Environment.NewLine))
                _NewCode = _NewCode.Remove(_NewCode.Length - Environment.NewLine.Length);
        }
Ejemplo n.º 2
0
        public string BuildCode(IEnumerable <NamespaceReference> namespaceReferences, Namespace namespaceScope, TypeDeclaration typeDeclaration)
        {
            ElementBuilder builder = new ElementBuilder();

            foreach (NamespaceReference namespaceReference in namespaceReferences)
            {
                builder.AddNode(null, namespaceReference);
            }

            builder.AddNode(null, new EmptyStatement());
            builder.AddNode(null, namespaceScope);
            builder.AddNode(namespaceScope, typeDeclaration);

            return(builder.GenerateCode());
        }
        private string BuildCode(IEnumerable<NamespaceReference> namespaceReferences, Namespace namespaceScope, TypeDeclaration typeDeclaration)
        {
            ElementBuilder builder = new ElementBuilder();

            foreach (NamespaceReference namespaceReference in namespaceReferences)
            {
                builder.AddNode(null, namespaceReference);
            }

            builder.AddNode(null, new EmptyStatement());
            builder.AddNode(null, namespaceScope);
            builder.AddNode(namespaceScope, typeDeclaration);

            return builder.GenerateCode();
        }
        /// <summary>
        /// Gets the code for namespace reference.
        /// </summary>
        /// <returns>
        /// The code for namespace reference.
        /// </returns>
        public string CreateCodeForNamespaceReference()
        {
            var elementBuilder = new ElementBuilder();

            elementBuilder.AddNode(null, this.ContractsNamespaceReference);
            return(elementBuilder.GenerateCode(this.TextDocument.Language));
        }
Ejemplo n.º 5
0
 private void cpDisposeFields_CheckAvailability(object sender, CheckContentAvailabilityEventArgs ea)
 {
     IClassElement iClassElement = ea.ClassInterfaceOrStruct as IClassElement;
       if (iClassElement == null)
     return;
       if (!AlreadyImplementsIDisposable(iClassElement))
     return;
       // We DO implement IDisposable! Let's make sure all the fields are disposed....
       IIfStatement parentIfDisposing;
       IList<IFieldElement> disposableFields = GetDisposableFieldsThatHaveNotBeenDisposed(null, ea.ClassInterfaceOrStruct.GetSourceFile(), iClassElement, out parentIfDisposing);
       if (disposableFields.Count > 0 && parentIfDisposing != null)
       {
     If ifParent = LanguageElementRestorer.ConvertToLanguageElement(parentIfDisposing) as If;
     if (ifParent != null)
     {
       ea.Available = true;
       ElementBuilder elementBuilder = new ElementBuilder();
       If newIfStatement = ifParent.Clone() as If;
       elementBuilder.AddNode(null, newIfStatement);
       foreach (BaseVariable disposableField in disposableFields)
     AddFieldDisposeBlock(elementBuilder, newIfStatement, disposableField);
       _CodeForNewIfDisposingBlock = elementBuilder.GenerateCode();
       _OldIfDisposingBlockRange = ifParent.Range;
     }
       }
 }
        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();
            }
        }
 /// <summary>
 /// Gets the code for namespace reference.
 /// </summary>
 /// <returns>
 /// The code for namespace reference.
 /// </returns>
 public string CreateCodeForNamespaceReference()
 {      
   var elementBuilder = new ElementBuilder();
   elementBuilder.AddNode(null, this.ContractsNamespaceReference);
   return elementBuilder.GenerateCode(this.TextDocument.Language);
 }
        private void CalculateCode(ParentingStatement parentingStatement)
        {
            _NewCode = null;
            if (parentingStatement == null)
            {
                return;
            }

            LanguageElement         parentToReplace = null;
            ParentToSingleStatement grandparent     = parentingStatement.Parent as ParentToSingleStatement;

            if (grandparent != null)
            {
                if (!grandparent.HasBlock)
                {
                    ElementCloneOptions cloneOptions = new ElementCloneOptions()
                    {
                        CloneNodes = false
                    };
                    parentToReplace = grandparent.Clone(cloneOptions) as LanguageElement;
                }
            }
            LanguageElement endNode;
            LanguageElement startNode;

            parentingStatement.GetFullBlockNodes(out startNode, out endNode);
            LanguageElement elementToCheck = startNode;
            ElementBuilder  elementBuilder = new ElementBuilder();

            if (parentToReplace != null)
            {
                elementBuilder.AddNode(null, parentToReplace);
            }

            while (elementToCheck != null)
            {
                ParentingStatement parent = elementToCheck as ParentingStatement;
                if (parent != null && parent.ElementType != LanguageElementType.Catch)
                {
                    foreach (IElement element in parent.Nodes)
                    {
                        LanguageElement child = element.Clone() as LanguageElement;
                        if (child != null)
                        {
                            elementBuilder.AddNode(parentToReplace, child);
                        }
                    }
                }
                if (elementToCheck == endNode)
                {
                    break;
                }

                elementToCheck = elementToCheck.NextCodeSibling;
            }

            if (parentToReplace != null)
            {
                _DeleteRange = parentToReplace.Range;
            }
            else
            {
                _DeleteRange = new SourceRange(startNode.Range.Top, endNode.Range.Bottom);
            }

            _NewCode = elementBuilder.GenerateCode();

            if (_NewCode.EndsWith(Environment.NewLine))
            {
                _NewCode = _NewCode.Remove(_NewCode.Length - Environment.NewLine.Length);
            }
        }