예제 #1
0
        public File AddNamespace(Action <NamespaceElement> namespaceAction)
        {
            var nsp = new NamespaceElement();

            namespaceAction(nsp);
            Elements.Add(nsp);
            return(this);
        }
예제 #2
0
        /// <summary>
        /// Test for ToString()
        /// </summary>
        protected override void DoToStringTest()
        {
            NamespaceElement element = new NamespaceElement();

            element.Name = "Test";

            string str = element.ToString();

            Assert.AreEqual("Test", str, "Unexpected value returned for ToString.");
        }
예제 #3
0
        public void DefaultArrangeNoUsingMoveTest()
        {
            CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration;

            configuration.Formatting.Usings.MoveTo = CodeLevel.None;

            CodeArranger arranger = new CodeArranger(configuration);

            ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(_testElements);

            //
            // Verify using statements were grouped and sorted correctly
            //
            Assert.AreEqual(3, arranged.Count, "An unexpected number of root elements were returned from Arrange.");

            RegionElement regionElement = arranged[0] as RegionElement;

            Assert.IsNotNull(regionElement, "Expected a region element.");
            Assert.AreEqual("Header", regionElement.Name);

            GroupElement groupElement = arranged[1] as GroupElement;

            Assert.IsNotNull(groupElement, "Expected a group element.");
            Assert.AreEqual("Namespace", groupElement.Name, "Unexpected group name.");
            Assert.AreEqual(1, groupElement.Children.Count, "Group contains an unexpected number of child elements.");

            groupElement = groupElement.Children[0] as GroupElement;
            Assert.IsNotNull(groupElement, "Expected a group element.");
            Assert.AreEqual("System", groupElement.Name, "Unexpected group name.");
            Assert.AreEqual(7, groupElement.Children.Count, "Group contains an unexpected number of child elements.");

            string lastUsingName = null;

            foreach (CodeElement groupedElement in groupElement.Children)
            {
                UsingElement usingElement = groupedElement as UsingElement;
                Assert.IsNotNull(usingElement, "Expected a using element.");

                string usingName = usingElement.Name;
                if (lastUsingName != null)
                {
                    Assert.AreEqual(
                        -1, lastUsingName.CompareTo(usingName), "Expected using statements to be sorted by name.");
                }
            }

            //
            // Verify the namespace arrangement
            //
            NamespaceElement namespaceElement = arranged[2] as NamespaceElement;

            Assert.IsNotNull(namespaceElement, "Expected a namespace element.");
        }
        public void Generate()
        {
            CSharpFile customTypeManagerGenerator = new CSharpFile();

            customTypeManagerGenerator.Name = string.Format("{0}TypeManager", name);

            customTypeManagerGenerator.cSharpFileElements.Add(new UsingElement(typeof(CustomTypeGenerator)));
            customTypeManagerGenerator.cSharpFileElements.Add(new UsingElement(typeof(CreateAssetMenuAttribute)));
            customTypeManagerGenerator.cSharpFileElements.Add(new UsingElement(typeof(SerializableAttribute)));
            customTypeManagerGenerator.cSharpFileElements.Add(new NewLineElement());

            NamespaceElement namespaceElement = new NamespaceElement(string.IsNullOrEmpty(@namespace) ? "CustomType" : @namespace);

            customTypeManagerGenerator.cSharpFileElements.Add(namespaceElement);

            ClassElement managerClass = new ClassElement(string.Format("{0}TypeManager", name));

            managerClass.ClassAttributes.Add(new CreateAssetMenuElement(name));
            managerClass.DerivedFrom = new SingletonTypeManagerElement(string.Format("{0}TypeManager", name));

            namespaceElement.cSharpFileElements.Add(managerClass);

            ClassElement typeClass = new ClassElement(name);

            typeClass.ClassAttributes.Add(new AttributeElement(typeof(SerializableAttribute)));
            typeClass.DerivedFrom = new CustomTypeEelemnt();
            namespaceElement.cSharpFileElements.Add(typeClass);


            customTypeManagerGenerator.Save(string.Format("{0}/{1}", path, name));

            CSharpFile propertyGenerator = new CSharpFile();

            propertyGenerator.cSharpFileElements.Add(new UsingElement(typeof(CustomPropertyDrawer)));
            propertyGenerator.cSharpFileElements.Add(new UsingElement(typeof(CustomTypeGenerator)));
            propertyGenerator.cSharpFileElements.Add(new UsingElement(typeof(CreateAssetMenuAttribute)));


            propertyGenerator.cSharpFileElements.Add(namespaceElement);
            ClassElement propertyClass = new ClassElement(string.Format("{0}PropertyDrower", name));

            propertyGenerator.Name    = propertyClass.Name;
            propertyClass.DerivedFrom = new TypePropertyDrowerElement(managerClass.Name);
            propertyClass.ClassAttributes.Add(new WeaponModeTypePropertyDrowerAttributeElement(typeClass.Name));
            namespaceElement.cSharpFileElements.Clear();
            namespaceElement.cSharpFileElements.Add(propertyClass);


            propertyGenerator.Save(string.Format("{0}/{1}/Editor", path, name));

            AssetDatabase.Refresh();
        }
예제 #5
0
        public void DefaultArrangeEnumerationTest()
        {
            List <ICodeElement> codeElements = new List <ICodeElement>();

            UsingElement usingElement = new UsingElement();

            usingElement.Name = "System";

            TypeElement enumElement = new TypeElement();

            enumElement.Type     = TypeElementType.Enum;
            enumElement.Access   = CodeAccess.Public;
            enumElement.Name     = "TestEnum";
            enumElement.BodyText = "Value1 = 1,\r\nValue2 = 2";

            NamespaceElement namespaceElement = new NamespaceElement();

            namespaceElement.Name = "TestNamespace";
            namespaceElement.AddChild(usingElement);
            namespaceElement.AddChild(enumElement);

            codeElements.Add(namespaceElement);

            CodeArranger arranger = new CodeArranger(CodeConfiguration.Default);

            ReadOnlyCollection <ICodeElement> arranged =
                arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned.");
            NamespaceElement namespaceElementTest = arranged[0] as NamespaceElement;

            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");

            Assert.AreEqual(2, namespaceElementTest.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");
            Assert.AreEqual(ElementType.Using, namespaceElement.Children[0].ElementType);

            RegionElement regionElement = namespaceElementTest.Children[1] as RegionElement;

            Assert.IsNotNull(regionElement, "Expected a region element.");
            Assert.AreEqual("Enumerations", regionElement.Name, "Unexpected region name.");

            Assert.AreEqual(1, regionElement.Children.Count,
                            "After arranging, an unexpected number of region elements were returned.");
            TypeElement typeElement = regionElement.Children[0] as TypeElement;

            Assert.IsNotNull(typeElement, "Expected a type element.");

            Assert.AreEqual(TypeElementType.Enum, typeElement.Type, "Unexpected type element type.");
            Assert.AreEqual(enumElement.Name, typeElement.Name, "Unexpected type element name.");
        }
예제 #6
0
        /// <summary>
        /// Arranges the code elements according to the configuration supplied
        /// in the constructor.
        /// </summary>
        /// <param name="originalElements">Original elements</param>
        /// <returns>An arranged collection of code elements.</returns>
        public ReadOnlyCollection <ICodeElement> Arrange(ReadOnlyCollection <ICodeElement> originalElements)
        {
            GroupElement rootElement = new GroupElement();

            if (originalElements != null)
            {
                List <ICodeElement> elements       = new List <ICodeElement>();
                NamespaceElement    firstNamespace = null;
                for (int elementIndex = 0; elementIndex < originalElements.Count; elementIndex++)
                {
                    ICodeElement element      = originalElements[elementIndex];
                    ICodeElement elementClone = element.Clone() as ICodeElement;
                    elements.Add(elementClone);

                    if (firstNamespace == null)
                    {
                        Action <ICodeElement> findFirstNamespace = delegate(ICodeElement processElement)
                        {
                            if (firstNamespace == null)
                            {
                                NamespaceElement namespaceElement = processElement as NamespaceElement;
                                if (namespaceElement != null)
                                {
                                    firstNamespace = namespaceElement;
                                }
                            }
                        };

                        ElementUtilities.ProcessElementTree(elementClone, findFirstNamespace);
                    }
                }

                MoveUsings(elements, firstNamespace);

                foreach (ICodeElement element in elements)
                {
                    ArrangerChain.ArrangeElement(rootElement, element);
                }
            }

            List <ICodeElement> arranged = new List <ICodeElement>(rootElement.Children);

            foreach (ICodeElement arrangedElement in arranged)
            {
                // Remove the root element as the parent.
                arrangedElement.Parent = null;
            }

            return(arranged.AsReadOnly());
        }
예제 #7
0
        private AddDocumentContainer AddModel(IEnumerable <PropertyElement> properties)
        {
            string projectName = "TestApp";
            List <UsingElement> usingElements = new List <UsingElement>()
            {
                new UsingElement("System.ComponentModel.DataAnnotations")
            };

            var namespaceElement = new NamespaceElement($"{projectName}");
            var classElement     = new ClassElement(this.ModelName, "public", properties);
            var documentElement  = new DocumentElement(usingElements, namespaceElement, classElement);

            return(new AddDocumentContainer(projectName, this.ModelName, documentElement));
        }
예제 #8
0
        /// <summary>
        /// Processes a namespace element.
        /// </summary>
        /// <param name="element">Namespace code element.</param>
        public override void VisitNamespaceElement(NamespaceElement element)
        {
            this.WriteComments(element.HeaderComments);

            StringBuilder builder = new StringBuilder(DefaultBlockLength);

            builder.Append(VBKeyword.Namespace);
            builder.Append(' ');
            builder.Append(element.Name);

            WriteIndented(builder.ToString());
            WriteBeginBlock();
            WriteBlockChildren(element);
            WriteEndBlock(element);
            Writer.WriteLine();
            Writer.WriteLine();
        }
예제 #9
0
 /// <summary>
 /// Processes a namespace element.
 /// </summary>
 /// <param name="element">Namespace code element.</param>
 public abstract void VisitNamespaceElement(NamespaceElement element);
예제 #10
0
 public string Filename(NamespaceElement @namespace, string prefix = "N", string suffix = "html")
 {
     return string.Format("{0}_{1}.{2}", prefix, @namespace.Namespace, suffix);
 }
예제 #11
0
        public void MoveUsingsToFileTest()
        {
            List <ICodeElement> codeElements = new List <ICodeElement>();

            UsingElement using1 = new UsingElement();

            using1.Name      = "System";
            using1.IsMovable = true;

            codeElements.Add(using1);

            NamespaceElement namespaceElement = new NamespaceElement();

            namespaceElement.Name = "TestNamespace";
            codeElements.Add(namespaceElement);

            // Nested region and groups
            RegionElement region = new RegionElement();

            region.Name = "Region";
            namespaceElement.AddChild(region);
            GroupElement group = new GroupElement();

            group.Name = "Group";
            region.AddChild(group);

            UsingElement using2 = new UsingElement();

            using2.Name      = "System.IO";
            using2.IsMovable = true;

            group.AddChild(using2);

            UsingElement using3 = new UsingElement();

            using3.Name      = "System.Collections";
            using3.IsMovable = true;
            namespaceElement.AddChild(using3);

            TypeElement class1 = new TypeElement();

            class1.Name = "Class1";
            namespaceElement.AddChild(class1);

            TypeElement class2 = new TypeElement();

            class2.Name = "Class2";
            namespaceElement.AddChild(class2);

            CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration;
            CodeArranger      arranger;

            //
            // Move to file.
            //
            configuration.Formatting.Usings.MoveTo = CodeLevel.File;
            arranger = new CodeArranger(configuration);
            ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned.");

            GroupElement fileGroup = arranged[0] as GroupElement;

            Assert.IsNotNull(fileGroup);
            GroupElement innerGroup = fileGroup.Children[0] as GroupElement;

            Assert.AreEqual("System", innerGroup.Children[0].Name);
            Assert.AreEqual("System.Collections", innerGroup.Children[1].Name);
            Assert.AreEqual("System.IO", innerGroup.Children[2].Name);

            NamespaceElement namespaceElementTest = arranged[1] as NamespaceElement;

            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");
            Assert.AreEqual(2, namespaceElementTest.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");

            RegionElement typeRegion = namespaceElementTest.Children[1] as RegionElement;

            Assert.IsNotNull(typeRegion);
            Assert.AreEqual("Class1", typeRegion.Children[0].Name);
            Assert.AreEqual("Class2", typeRegion.Children[1].Name);
        }
예제 #12
0
        public void MoveUsingsBasicTest()
        {
            List <ICodeElement> codeElements = new List <ICodeElement>();

            UsingElement using1 = new UsingElement();

            using1.Name      = "System";
            using1.IsMovable = true;

            UsingElement using2 = new UsingElement();

            using2.Name      = "System.IO";
            using2.IsMovable = true;

            UsingElement using3 = new UsingElement();

            using3.Name      = "System.Collections";
            using3.IsMovable = true;

            codeElements.Add(using1);
            codeElements.Add(using2);

            NamespaceElement namespaceElement = new NamespaceElement();

            namespaceElement.Name = "TestNamespace";
            namespaceElement.AddChild(using3);

            codeElements.Add(namespaceElement);

            CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration;
            CodeArranger      arranger;

            //
            // Do not move.
            //
            configuration.Formatting.Usings.MoveTo = CodeLevel.None;
            arranger = new CodeArranger(configuration);
            ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned.");

            GroupElement fileGroup = arranged[0] as GroupElement;

            Assert.IsNotNull(fileGroup);
            GroupElement innerGroup = fileGroup.Children[0] as GroupElement;

            Assert.AreEqual("System", innerGroup.Children[0].Name);
            Assert.AreEqual("System.IO", innerGroup.Children[1].Name);

            NamespaceElement namespaceElementTest = arranged[1] as NamespaceElement;

            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");
            Assert.AreEqual(1, namespaceElementTest.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");
            GroupElement namespaceGroup = namespaceElementTest.Children[0] as GroupElement;

            Assert.IsNotNull(namespaceGroup);
            innerGroup = namespaceGroup.Children[0] as GroupElement;
            Assert.AreEqual("System.Collections", innerGroup.Children[0].Name);

            //
            // Move to file level;
            //
            configuration.Formatting.Usings.MoveTo = CodeLevel.File;
            arranger = new CodeArranger(configuration);
            arranged = arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned.");

            fileGroup = arranged[0] as GroupElement;
            Assert.IsNotNull(fileGroup);
            innerGroup = fileGroup.Children[0] as GroupElement;
            Assert.AreEqual("System", innerGroup.Children[0].Name);
            Assert.AreEqual("System.Collections", innerGroup.Children[1].Name);
            Assert.AreEqual("System.IO", innerGroup.Children[2].Name);

            namespaceElementTest = arranged[1] as NamespaceElement;
            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");

            //
            // Move to namespace.
            //
            configuration.Formatting.Usings.MoveTo = CodeLevel.Namespace;
            arranger = new CodeArranger(configuration);
            arranged = arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned.");

            namespaceElementTest = arranged[0] as NamespaceElement;
            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");
            Assert.AreEqual(1, namespaceElementTest.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");
            namespaceGroup = namespaceElementTest.Children[0] as GroupElement;
            Assert.IsNotNull(namespaceGroup);
            innerGroup = namespaceGroup.Children[0] as GroupElement;
            Assert.AreEqual("System", innerGroup.Children[0].Name);
            Assert.AreEqual("System.Collections", innerGroup.Children[1].Name);
            Assert.AreEqual("System.IO", innerGroup.Children[2].Name);

            //
            // Move back to file level;
            //
            configuration.Formatting.Usings.MoveTo = CodeLevel.File;
            arranger = new CodeArranger(configuration);
            arranged = arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned.");

            fileGroup = arranged[0] as GroupElement;
            Assert.IsNotNull(fileGroup);
            innerGroup = fileGroup.Children[0] as GroupElement;
            Assert.AreEqual("System", innerGroup.Children[0].Name);
            Assert.AreEqual("System.Collections", innerGroup.Children[1].Name);
            Assert.AreEqual("System.IO", innerGroup.Children[2].Name);

            namespaceElementTest = arranged[1] as NamespaceElement;
            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");
        }
예제 #13
0
        public void DefaultArrangeStructLayoutTest()
        {
            List <ICodeElement> codeElements = new List <ICodeElement>();

            TypeElement structElement = new TypeElement();

            structElement.Type   = TypeElementType.Structure;
            structElement.Access = CodeAccess.Public;
            structElement.Name   = "TestStructure";
            structElement.AddAttribute(new AttributeElement("System.Runtime.InteropServices.StructLayout"));

            NamespaceElement namespaceElement = new NamespaceElement();

            namespaceElement.Name = "TestNamespace";
            namespaceElement.AddChild(structElement);

            FieldElement fieldElement1 = new FieldElement();

            fieldElement1.Type   = "int";
            fieldElement1.Access = CodeAccess.Public;
            fieldElement1.Name   = "z";
            structElement.AddChild(fieldElement1);

            FieldElement fieldElement2 = new FieldElement();

            fieldElement2.Type   = "int";
            fieldElement2.Access = CodeAccess.Public;
            fieldElement2.Name   = "x";
            structElement.AddChild(fieldElement2);

            FieldElement fieldElement3 = new FieldElement();

            fieldElement3.Type   = "int";
            fieldElement3.Access = CodeAccess.Public;
            fieldElement3.Name   = "y";
            structElement.AddChild(fieldElement3);

            codeElements.Add(namespaceElement);

            CodeArranger arranger = new CodeArranger(CodeConfiguration.Default);

            ReadOnlyCollection <ICodeElement> arranged =
                arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned.");
            NamespaceElement namespaceElementTest = arranged[0] as NamespaceElement;

            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");

            Assert.AreEqual(1, namespaceElementTest.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");
            RegionElement typeRegionElement = namespaceElementTest.Children[0] as RegionElement;

            Assert.IsNotNull(typeRegionElement, "Expected a region element.");
            Assert.AreEqual("Types", typeRegionElement.Name);

            Assert.AreEqual(1, typeRegionElement.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");
            TypeElement typeElement = typeRegionElement.Children[0] as TypeElement;

            Assert.IsNotNull(typeElement, "Expected a type element.");
            Assert.AreEqual(TypeElementType.Structure, typeElement.Type, "Unexpected type element type.");
            Assert.AreEqual(structElement.Name, typeElement.Name, "Unexpected type element name.");

            Assert.AreEqual(1, typeElement.Children.Count, "An unexpected number of class child elements were returned.");
            RegionElement regionElement = typeElement.Children[0] as RegionElement;

            Assert.IsNotNull(regionElement, "Expected a region element but was {0}.", regionElement.ElementType);
            Assert.AreEqual("Fixed Fields", regionElement.Name, "Unexpected region name.");

            Assert.AreEqual(3, regionElement.Children.Count, "Unexpected number of region child elements.");

            // The fields should not have been sorted
            Assert.AreEqual(fieldElement1.Name, regionElement.Children[0].Name);
            Assert.AreEqual(fieldElement2.Name, regionElement.Children[1].Name);
            Assert.AreEqual(fieldElement3.Name, regionElement.Children[2].Name);
        }
예제 #14
0
        public void DefaultArrangeSimpleClassTest()
        {
            List <ICodeElement> codeElements = new List <ICodeElement>();

            TypeElement classElement = new TypeElement();

            classElement.Type   = TypeElementType.Class;
            classElement.Access = CodeAccess.Public;
            classElement.Name   = "TestClass";

            NamespaceElement namespaceElement = new NamespaceElement();

            namespaceElement.Name = "TestNamespace";
            namespaceElement.AddChild(classElement);

            MethodElement methodElement = new MethodElement();

            methodElement.Type   = "void";
            methodElement.Access = CodeAccess.Public;
            methodElement.Name   = "DoSomething";
            classElement.AddChild(methodElement);

            FieldElement fieldElement = new FieldElement();

            fieldElement.Type   = "bool";
            fieldElement.Access = CodeAccess.Private;
            fieldElement.Name   = "_val";
            classElement.AddChild(fieldElement);

            PropertyElement propertyElement = new PropertyElement();

            propertyElement.Type     = "bool";
            propertyElement.Access   = CodeAccess.Public;
            propertyElement.Name     = "Value";
            propertyElement.BodyText = "return _val";
            classElement.AddChild(propertyElement);

            codeElements.Add(namespaceElement);

            CodeArranger arranger = new CodeArranger(CodeConfiguration.Default);

            ReadOnlyCollection <ICodeElement> arranged =
                arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned.");
            NamespaceElement namespaceElementTest = arranged[0] as NamespaceElement;

            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");

            Assert.AreEqual(1, namespaceElementTest.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");
            RegionElement typeRegionElement = namespaceElementTest.Children[0] as RegionElement;

            Assert.IsNotNull(typeRegionElement, "Expected a region element.");
            Assert.AreEqual("Types", typeRegionElement.Name);

            Assert.AreEqual(1, typeRegionElement.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");
            TypeElement typeElement = typeRegionElement.Children[0] as TypeElement;

            Assert.IsNotNull(typeElement, "Expected a type element.");

            Assert.AreEqual(TypeElementType.Class, typeElement.Type, "Unexpected type element type.");
            Assert.AreEqual(classElement.Name, typeElement.Name, "Unexpected type element name.");

            Assert.AreEqual(3, typeElement.Children.Count, "An unexpected number of class child elements were returned.");
            List <RegionElement> regionElements = new List <RegionElement>();

            foreach (ICodeElement classChildElement in typeElement.Children)
            {
                RegionElement regionElement = classChildElement as RegionElement;
                Assert.IsNotNull(
                    regionElement, "Expected a region element but was {0}.", classChildElement.ElementType);
                regionElements.Add(regionElement);
            }

            Assert.AreEqual("Fields", regionElements[0].Name, "Unexpected region element name.");
            Assert.AreEqual("Properties", regionElements[1].Name, "Unexpected region element name.");
            Assert.AreEqual("Methods", regionElements[2].Name, "Unexpected region element name.");

            GroupElement fieldGroupElement = regionElements[0].Children[0].Children[0] as GroupElement;

            Assert.IsNotNull(fieldGroupElement, "Expected a group element for fields.");

            foreach (ICodeElement codeElement in fieldGroupElement.Children)
            {
                FieldElement fieldElementTest = codeElement as FieldElement;
                Assert.IsNotNull(
                    fieldElementTest,
                    "Expected a field element but was type {0}: {1}",
                    codeElement.ElementType,
                    codeElement);
            }

            Assert.AreEqual(1, regionElements[1].Children.Count);
            foreach (ICodeElement codeElement in regionElements[1].Children[0].Children)
            {
                PropertyElement propertyElementTest = codeElement as PropertyElement;
                Assert.IsNotNull(
                    propertyElementTest,
                    "Expected a property element but was type {0}: {1}",
                    codeElement.ElementType,
                    codeElement);
            }

            Assert.AreEqual(1, regionElements[2].Children.Count);
            foreach (ICodeElement codeElement in regionElements[2].Children[0].Children)
            {
                MethodElement methodElementTest = codeElement as MethodElement;
                Assert.IsNotNull(
                    methodElementTest,
                    "Expected a method element but was type {0}: {1}",
                    codeElement.ElementType,
                    codeElement);
            }
        }
예제 #15
0
        /// <summary>
        /// Moves using directives if configured to do so.
        /// </summary>
        /// <param name="elements">List of top-level code elements.</param>
        /// <param name="namespaceElement">Namespace namespace to use when moving usings.</param>
        private void MoveUsings(List <ICodeElement> elements, NamespaceElement namespaceElement)
        {
            CodeLevel moveUsingsTo = _configuration.Formatting.Usings.MoveTo;

            List <ICodeElement> tempElements;

            if (moveUsingsTo != CodeLevel.None && namespaceElement != null)
            {
                if (moveUsingsTo == CodeLevel.Namespace)
                {
                    tempElements = new List <ICodeElement>(elements);

                    for (int elementIndex = 0; elementIndex < tempElements.Count; elementIndex++)
                    {
                        UsingElement usingElement = tempElements[elementIndex] as UsingElement;
                        if (usingElement != null && usingElement.IsMovable)
                        {
                            if (elements.Contains(usingElement))
                            {
                                elements.Remove(usingElement);
                            }
                            tempElements.Remove(usingElement);
                            namespaceElement.InsertChild(0, usingElement);
                            elementIndex--;
                        }
                        else
                        {
                            if (tempElements[elementIndex] is RegionElement ||
                                tempElements[elementIndex] is GroupElement)
                            {
                                tempElements.AddRange(tempElements[elementIndex].Children);
                            }
                            else if (tempElements[elementIndex] is ConditionDirectiveElement)
                            {
                                ConditionDirectiveElement condition = tempElements[elementIndex] as ConditionDirectiveElement;
                                while (condition != null)
                                {
                                    if (namespaceElement.Parent == condition)
                                    {
                                        tempElements.AddRange(tempElements[elementIndex].Children);
                                        break;
                                    }
                                    condition = condition.ElseCondition;
                                }
                            }
                        }
                    }
                }
                else if (moveUsingsTo == CodeLevel.File)
                {
                    tempElements = new List <ICodeElement>();

                    for (int elementIndex = 0; elementIndex < namespaceElement.Children.Count; elementIndex++)
                    {
                        UsingElement usingElement = namespaceElement.Children[elementIndex] as UsingElement;
                        if (usingElement != null && usingElement.IsMovable)
                        {
                            namespaceElement.RemoveChild(usingElement);
                            elements.Insert(0, usingElement);
                            elementIndex--;
                        }
                        else if (namespaceElement.Children[elementIndex] is RegionElement ||
                                 namespaceElement.Children[elementIndex] is GroupElement)
                        {
                            foreach (ICodeElement childElement in namespaceElement.Children[elementIndex].Children)
                            {
                                if (childElement is UsingElement ||
                                    childElement is RegionElement ||
                                    childElement is GroupElement)
                                {
                                    tempElements.Add(childElement);
                                }
                            }
                        }
                    }

                    for (int elementIndex = 0; elementIndex < tempElements.Count; elementIndex++)
                    {
                        UsingElement usingElement = tempElements[elementIndex] as UsingElement;
                        if (usingElement != null && usingElement.IsMovable)
                        {
                            tempElements.Remove(usingElement);
                            elements.Insert(0, usingElement);
                            elementIndex--;
                        }
                        else if (tempElements[elementIndex] is RegionElement ||
                                 tempElements[elementIndex] is GroupElement)
                        {
                            tempElements.AddRange(tempElements[elementIndex].Children);
                        }
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException(
                              string.Format(
                                  "Unknown code level '{0}'.",
                                  moveUsingsTo));
                }
            }
        }