示例#1
0
        /// <summary>
        /// Inserts the element within the parent.
        /// </summary>
        /// <param name="parentElement">Parent element to insert into.</param>
        /// <param name="codeElement">Code element to insert.</param>
        public void InsertElement(ICodeElement parentElement, ICodeElement codeElement)
        {
            GroupElement group = null;

            string groupName = GetGroupName(_groupBy.By, codeElement);

            foreach (ICodeElement childElement in parentElement.Children)
            {
                GroupElement groupElement = childElement as GroupElement;
                if (groupElement != null && groupElement.Name == groupName)
                {
                    group = groupElement;
                    break;
                }
            }

            if (group == null)
            {
                group                 = new GroupElement();
                group.Name            = groupName;
                group.SeparatorType   = _groupBy.SeparatorType;
                group.CustomSeparator = _groupBy.CustomSeparator;

                int insertIndex = parentElement.Children.Count;

                if (_groupBy.Direction != SortDirection.None)
                {
                    // Sort the groups by the attribute selected for grouping
                    for (int compareIndex = parentElement.Children.Count - 1;
                         compareIndex >= 0;
                         compareIndex--)
                    {
                        GroupElement siblingGroup = parentElement.Children[insertIndex - 1] as GroupElement;
                        if (siblingGroup != null && siblingGroup.Children.Count > 0)
                        {
                            // This may not be the most accurate way to do this, but just compare
                            // against the first element in the sibling group.
                            ICodeElement compareElement = siblingGroup.Children[0];

                            // For nested groups, we need to drill down to find the first element.
                            while (compareElement is GroupElement && compareElement.Children.Count > 0)
                            {
                                compareElement = compareElement.Children[0];
                            }

                            // Create the element comparer if necessary
                            if (_sortComparer == null)
                            {
                                _sortComparer = new ElementComparer(_groupBy.By, _groupBy.Direction);
                            }

                            int compareValue = _sortComparer.Compare(codeElement, compareElement);

                            // System using directives should always be placed first in the file.
                            if (compareValue < 0 && (!(codeElement is UsingElement) || siblingGroup.Name != "System") ||
                                (codeElement is UsingElement && groupName == "System"))
                            {
                                insertIndex = compareIndex;
                            }
                        }
                    }
                }

                if (insertIndex < parentElement.Children.Count)
                {
                    parentElement.InsertChild(insertIndex, group);
                }
                else
                {
                    parentElement.AddChild(group);
                }
            }

            if (_innerInserter != null)
            {
                _innerInserter.InsertElement(group, codeElement);
            }
            else
            {
                group.AddChild(codeElement);
            }
        }
示例#2
0
        /// <summary>
        /// Arranges the element in within the code tree represented in the specified
        /// builder.
        /// </summary>
        /// <param name="parentElement">The parent element.</param>
        /// <param name="codeElement">The code element.</param>
        public virtual void ArrangeElement(ICodeElement parentElement, ICodeElement codeElement)
        {
            if (codeElement.Children.Count > 0)
            {
                if (_childrenArranger == null)
                {
                    _childrenArranger = ElementArrangerFactory.CreateChildrenArranger(_elementConfiguration);
                }

                if (_childrenArranger != null)
                {
                    List <ICodeElement> children = new List <ICodeElement>(codeElement.Children);
                    codeElement.ClearChildren();

                    foreach (ICodeElement childElement in children)
                    {
                        ArrangeChildElement(codeElement, childElement);
                    }

                    //
                    // For condition directives, arrange the children of each node in the list.
                    //
                    ConditionDirectiveElement conditionDirective = codeElement as ConditionDirectiveElement;
                    if (conditionDirective != null)
                    {
                        //
                        // Skip the first instance since we've already arranged those child elements.
                        //
                        conditionDirective = conditionDirective.ElseCondition;
                    }
                    while (conditionDirective != null)
                    {
                        children = new List <ICodeElement>(conditionDirective.Children);
                        conditionDirective.ClearChildren();

                        foreach (ICodeElement childElement in children)
                        {
                            ArrangeChildElement(conditionDirective, childElement);
                        }
                        conditionDirective = conditionDirective.ElseCondition;
                    }
                }
            }

            if (_inserter == null)
            {
                _inserter = CreateElementInserter(
                    _elementConfiguration.ElementType,
                    _elementConfiguration.SortBy,
                    _elementConfiguration.GroupBy,
                    _parentConfiguration);
            }

            // For Type elements, if interdependent static fields are present, correct their
            // ordering.
            TypeElement typeElement = codeElement as TypeElement;

            if (typeElement != null &&
                (typeElement.Type == TypeElementType.Class || typeElement.Type == TypeElementType.Structure || typeElement.Type == TypeElementType.Module))
            {
                CorrectStaticFieldDependencies(typeElement);
            }

            _inserter.InsertElement(parentElement, codeElement);
        }