Code arranger configuration information.
Наследование: ConfigurationElement
Пример #1
0
        /// <summary>
        /// Override Clone so that we can force resolution of element references.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public override object Clone()
        {
            CodeConfiguration clone = base.Clone() as CodeConfiguration;

            clone.ResolveReferences();

            return(clone);
        }
Пример #2
0
        public void ArrangeNestedRegionTest()
        {
            List<ICodeElement> elements = new List<ICodeElement>();

            TypeElement type = new TypeElement();
            type.Type = TypeElementType.Class;
            type.Name = "TestClass";

            FieldElement field = new FieldElement();
            field.Name = "val";
            field.Type = "int";

            type.AddChild(field);
            elements.Add(type);

            // Create a configuration with a nested region
            CodeConfiguration codeConfiguration = new CodeConfiguration();

            ElementConfiguration typeConfiguration = new ElementConfiguration();
            typeConfiguration.ElementType = ElementType.Type;

            RegionConfiguration regionConfiguration1 = new RegionConfiguration();
            regionConfiguration1.Name = "Region1";

            RegionConfiguration regionConfiguration2 = new RegionConfiguration();
            regionConfiguration2.Name = "Region2";

            ElementConfiguration fieldConfiguration = new ElementConfiguration();
            fieldConfiguration.ElementType = ElementType.Field;

            regionConfiguration2.Elements.Add(fieldConfiguration);
            regionConfiguration1.Elements.Add(regionConfiguration2);
            typeConfiguration.Elements.Add(regionConfiguration1);
            codeConfiguration.Elements.Add(typeConfiguration);

            CodeArranger arranger = new CodeArranger(codeConfiguration);

            ReadOnlyCollection<ICodeElement> arrangedElements = arranger.Arrange(elements.AsReadOnly());

            Assert.AreEqual(1, arrangedElements.Count, "Unexpected number of arranged elements.");

            TypeElement arrangedType = arrangedElements[0] as TypeElement;
            Assert.IsNotNull(arrangedType, "Expected a type element after arranging.");
            Assert.AreEqual(1, arrangedType.Children.Count, "Unexpected number of arranged child elements.");

            RegionElement arrangedRegion1 = arrangedType.Children[0] as RegionElement;
            Assert.IsNotNull(arrangedRegion1, "Expected a region element after arranging.");
            Assert.AreEqual(regionConfiguration1.Name, arrangedRegion1.Name);
            Assert.AreEqual(1, arrangedRegion1.Children.Count, "Unexpected number of arranged child elements.");

            RegionElement arrangedRegion2 = arrangedRegion1.Children[0] as RegionElement;
            Assert.IsNotNull(arrangedRegion2, "Expected a region element after arranging.");
            Assert.AreEqual(regionConfiguration2.Name, arrangedRegion2.Name);
            Assert.AreEqual(1, arrangedRegion2.Children.Count, "Unexpected number of arranged child elements.");

            FieldElement arrangedFieldElement = arrangedRegion2.Children[0] as FieldElement;
            Assert.IsNotNull(arrangedFieldElement, "Expected a field element after arranging.");
        }
Пример #3
0
        /// <summary>
        /// Creates a new code arranger with the specified configuration.
        /// </summary>
        /// <param name="configuration">Configuration to use for arranging code members.</param>
        public CodeArranger(CodeConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            // Clone the configuration information so we don't have to worry about it
            // changing during processing.
            _configuration = configuration.Clone() as CodeConfiguration;
        }
Пример #4
0
        /// <summary>
        /// Creates a new FileManager.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        public ProjectManager(CodeConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            _configuration = configuration;

            Initialize();
        }
Пример #5
0
        /// <summary>
        /// Loads a configuration from a stream.
        /// </summary>
        /// <param name="stream">The sream to load the configuration from.</param>
        /// <param name="resolveReferences">
        /// Whether or not element references should be resolved.
        /// </param>
        /// <returns>The code configuration if succesful, otherwise null.</returns>
        public static CodeConfiguration Load(Stream stream, bool resolveReferences)
        {
            CodeConfiguration configuration =
                _serializer.Deserialize(stream) as CodeConfiguration;

            if (resolveReferences)
            {
                configuration.ResolveReferences();
            }

            configuration.Upgrade();

            return(configuration);
        }
Пример #6
0
        public void CreateTest()
        {
            CodeConfiguration configuration = new CodeConfiguration();

            Assert.IsNotNull(configuration.Elements, "Elements collection should not be null.");
            Assert.AreEqual(0, configuration.Elements.Count, "Elements collection should be empty.");

            //
            // Test the default tab configuration
            //
            Assert.IsNotNull(configuration.Formatting.Tabs, "Tabs configuration should not be null.");
            Assert.AreEqual(TabStyle.Spaces, configuration.Formatting.Tabs.TabStyle, "Unexpected default tab style.");
            Assert.AreEqual(4, configuration.Formatting.Tabs.SpacesPerTab, "Unexpected defatult number of spaces per tab.");
        }
Пример #7
0
        /// <summary>
        /// Creates a clone of this instance.
        /// </summary>
        /// <returns>A clone of the instance.</returns>
        protected override ConfigurationElement DoClone()
        {
            CodeConfiguration clone = new CodeConfiguration();

            clone._encoding   = Encoding.Clone() as EncodingConfiguration;
            clone._formatting = Formatting.Clone() as FormattingConfiguration;

            foreach (HandlerConfiguration handler in Handlers)
            {
                HandlerConfiguration handlerClone = handler.Clone() as HandlerConfiguration;
                clone.Handlers.Add(handlerClone);
            }

            return(clone);
        }
Пример #8
0
        /// <summary>
        /// Creates a new VBWriteVisitor.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="configuration">The configuration.</param>
        protected CodeWriteVisitor(TextWriter writer, CodeConfiguration configuration)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            Debug.Assert(configuration != null, "Configuration should not be null.");

            _writer = writer;
            _configuration = configuration;
        }
Пример #9
0
 /// <summary>
 /// Creates a new VBWriteVisitor.
 /// </summary>
 /// <param name="writer">Text writer.</param>
 /// <param name="configuration">Code configuration.</param>
 public VBWriteVisitor(TextWriter writer, CodeConfiguration configuration)
     : base(writer, configuration)
 {
 }
Пример #10
0
        public void UpgradeProjectExtensionsTest()
        {
            string filename = Path.GetTempFileName();
            try
            {
                CodeConfiguration oldConfiguration = new CodeConfiguration();

                SourceHandlerConfiguration sourceHandler = new SourceHandlerConfiguration();
                ExtensionConfiguration oldExtension = new ExtensionConfiguration();
                oldExtension.Name = "csproj";
                sourceHandler.ProjectExtensions.Add(oldExtension);
                oldConfiguration.Handlers.Add(sourceHandler);
                oldConfiguration.Save(filename);

                CodeConfiguration newConfiguration = CodeConfiguration.Load(filename);
                Assert.AreEqual(2, newConfiguration.Handlers.Count, "New handler was not created.");
                ProjectHandlerConfiguration projectHandlerConfiguration =
                    newConfiguration.Handlers[0] as ProjectHandlerConfiguration;
                Assert.IsNotNull(projectHandlerConfiguration, "Expected a project handler config to be created.");
                Assert.IsNull(projectHandlerConfiguration.AssemblyName);
                Assert.AreEqual(typeof(MSBuildProjectParser).FullName, projectHandlerConfiguration.ParserType);
                Assert.AreEqual(1, projectHandlerConfiguration.ProjectExtensions.Count, "Unexpected number of project extensions.");
                Assert.AreEqual(oldExtension.Name, projectHandlerConfiguration.ProjectExtensions[0].Name);
            }
            finally
            {
                try
                {
                    File.Delete(filename);
                }
                catch
                {
                }
            }
        }
Пример #11
0
        public void SerializeAndDeserializeTest()
        {
            CodeConfiguration origConfig = new CodeConfiguration();

            ElementConfiguration elementConfiguration1 = new ElementConfiguration();
            elementConfiguration1.ElementType = ElementType.Using;
            elementConfiguration1.Id = "TestId";
            origConfig.Elements.Add(elementConfiguration1);

            ElementConfiguration elementConfiguration2 = new ElementConfiguration();
            elementConfiguration2.ElementType = ElementType.Namespace;
            origConfig.Elements.Add(elementConfiguration2);

            ElementReferenceConfiguration elementReferenceConfiguration = new ElementReferenceConfiguration();
            elementReferenceConfiguration.Id = "TestId";
            origConfig.Elements.Add(elementReferenceConfiguration);

            RegionConfiguration regionConfiguration = new RegionConfiguration();
            regionConfiguration.Name = "Test Region";
            origConfig.Elements.Add(regionConfiguration);

            origConfig.ResolveReferences();
            Assert.AreEqual(
                elementConfiguration1.Elements.Count,
                elementReferenceConfiguration.ReferencedElement.Elements.Count,
                "Element reference was not resolved.");

            string tempFile = Path.GetTempFileName();
            try
            {
                //
                // Save the configuration to an XML file
                //
                origConfig.Save(tempFile);

                //
                // Load the configuration from the XML file
                //
                CodeConfiguration loadedConfig = CodeConfiguration.Load(tempFile);
                Assert.IsNotNull(loadedConfig, "Loaded configuration should not be null.");

                Assert.AreEqual(origConfig.Elements.Count, loadedConfig.Elements.Count, "An unexpected number of config elements were deserialized.");

                for (int index = 0; index < origConfig.Elements.Count; index++)
                {
                    if (origConfig.Elements[index] is ElementConfiguration)
                    {
                        ElementConfiguration origElement =
                            origConfig.Elements[index] as ElementConfiguration;
                        ElementConfiguration loadedElement =
                            loadedConfig.Elements[index] as ElementConfiguration;

                        Assert.AreEqual(origElement.ElementType, loadedElement.ElementType, "Unexpected element type.");
                    }
                    else if (origConfig.Elements[index] is ElementReferenceConfiguration)
                    {
                        ElementReferenceConfiguration origElement =
                            origConfig.Elements[index] as ElementReferenceConfiguration;
                        ElementReferenceConfiguration loadedElement =
                            loadedConfig.Elements[index] as ElementReferenceConfiguration;

                        Assert.AreEqual(origElement.Id, loadedElement.Id, "Unexpected element type.");
                        Assert.AreEqual(
                            origElement.ReferencedElement.Id,
                            loadedElement.ReferencedElement.Id,
                            "Unexpected referenced element.");
                    }
                    else if (origConfig.Elements[index] is RegionConfiguration)
                    {
                        RegionConfiguration origRegion =
                            origConfig.Elements[index] as RegionConfiguration;
                        RegionConfiguration loadedRegion =
                            loadedConfig.Elements[index] as RegionConfiguration;

                        Assert.AreEqual(origRegion.Name, loadedRegion.Name, "Unexpected region name.");
                    }
                }
            }
            finally
            {
                File.Delete(tempFile);
            }
        }
Пример #12
0
        /// <summary>
        /// Loads the configuration file that specifies how elements will be arranged.
        /// </summary>
        /// <param name="configFile">The config file.</param>
        private void LoadConfiguration(string configFile)
        {
            if (_configuration == null)
            {
                if (configFile != null)
                {
                    _configuration = CodeConfiguration.Load(configFile);
                }
                else
                {
                    _configuration = CodeConfiguration.Default;
                }

                _projectManager = new ProjectManager(_configuration);
                _encoding = _configuration.Encoding.GetEncoding();
            }
        }
Пример #13
0
        /// <summary>
        /// Creates a clone of this instance.
        /// </summary>
        /// <returns>A clone of the instance.</returns>
        protected override ConfigurationElement DoClone()
        {
            CodeConfiguration clone = new CodeConfiguration();

            clone._encoding = Encoding.Clone() as EncodingConfiguration;
            clone._formatting = Formatting.Clone() as FormattingConfiguration;

            foreach (HandlerConfiguration handler in Handlers)
            {
                HandlerConfiguration handlerClone = handler.Clone() as HandlerConfiguration;
                clone.Handlers.Add(handlerClone);
            }

            return clone;
        }