/// <summary> /// Inserts an element into the parent using the strategy defined by the /// sort configuration. /// </summary> /// <param name="parentElement">Parent element.</param> /// <param name="codeElement">Code element to insert.</param> public void InsertElement(ICodeElement parentElement, ICodeElement codeElement) { if (codeElement != null) { ICodeElement compareElement = null; int insertIndex = 0; if (parentElement.Children.Count > 0) { if (_comparer == null) { _comparer = CreateComparer(_sortBy); } for (int elementIndex = 0; elementIndex < parentElement.Children.Count; elementIndex++) { compareElement = parentElement.Children[elementIndex]; bool greaterOrEqual = (_elementType == ElementType.NotSpecified && _comparer.Compare(codeElement, compareElement) >= 0) || (_elementType != ElementType.NotSpecified && ((compareElement != null && compareElement.ElementType != _elementType) || _comparer.Compare(codeElement, compareElement) >= 0)); if (greaterOrEqual) { insertIndex++; } else { break; } } } parentElement.InsertChild(insertIndex, codeElement); } }
/// <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); } }
/// <summary> /// Arranges the specified code element into the parent. /// </summary> /// <param name="parentElement">Parent element to arrange within.</param> /// <param name="codeElement">Code element to arrange.</param> public void ArrangeElement(ICodeElement parentElement, ICodeElement codeElement) { InitializeChildrenArranger(); if (codeElement != null) { RegionElement region = null; string regionName = _regionConfiguration.Name; bool directivesEnabled = _regionConfiguration.DirectivesEnabled; foreach (ICodeElement childElement in parentElement.Children) { RegionElement regionElement = childElement as RegionElement; if (regionElement != null && regionElement.Name == regionName) { region = regionElement; break; } } if (region == null) { region = new RegionElement(); region.Name = regionName; region.DirectivesEnabled = directivesEnabled; if (parentElement.Children.Count == 0) { parentElement.AddChild(region); } else { // // Determine where to insert the new region // int insertIndex = 0; int compareIndex = _levelRegions.IndexOf(region.Name); for (int siblingIndex = 0; siblingIndex < parentElement.Children.Count; siblingIndex++) { RegionElement siblingRegion = parentElement.Children[siblingIndex] as RegionElement; if (siblingRegion != null) { insertIndex = siblingIndex; int siblingCompareIndex = _levelRegions.IndexOf(siblingRegion.Name); if (compareIndex <= siblingCompareIndex) { break; } else { insertIndex++; } } else { insertIndex++; } } parentElement.InsertChild(insertIndex, region); } } _childrenArranger.ArrangeElement(region, codeElement); } }