Beispiel #1
0
        private static void ProcessImports(
            IList <IUsingDirective> newImportsList,
            AlphabeticalUsingsStyle organiseUsingsFormatOption,
            ExpandUsingsStyle expandUsingsFormatOption,
            ICSharpTypeAndNamespaceHolderDeclaration declaration)
        {
            if (newImportsList == null || newImportsList.Count == 0)
            {
                return;
            }

            List <IUsingDirective> arrayList = new List <IUsingDirective>();

            arrayList.AddRange(newImportsList);

            if (organiseUsingsFormatOption == AlphabeticalUsingsStyle.Alphabetical)
            {
                arrayList.Sort(new UsingStatementSorter());
            }

            foreach (IUsingDirective directive in arrayList)
            {
                IUsingDirective newUsingDirective;

                if (expandUsingsFormatOption == ExpandUsingsStyle.FullyQualify)
                {
                    if (directive is IUsingAliasDirective)
                    {
                        IUsingAliasDirective aliasDirective = directive as IUsingAliasDirective;
                        newUsingDirective =
                            CSharpElementFactory.GetInstance(declaration.GetPsiModule())
                            .CreateUsingDirective(aliasDirective.AliasName + " = " + directive.GetFullyQualifiedNamespace());

                        IUsingAliasDirective n = newUsingDirective as IUsingAliasDirective;
                        n.SetImportedSymbolName(aliasDirective.ImportedSymbolName);
                    }
                    else
                    {
                        newUsingDirective = CSharpElementFactory.GetInstance(declaration.GetPsiModule()).CreateUsingDirective(directive.GetFullyQualifiedNamespace());
                    }
                }
                else
                {
                    newUsingDirective = directive.CopyWithResolve();
                }

                declaration.RemoveImport(directive);
                declaration.AddImportBefore(newUsingDirective, null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Orders the files usings statements.
        /// </summary>
        /// <param name="options">
        /// The options to use.
        /// </param>
        /// <param name="file">
        /// The file to process.
        /// </param>
        public static void OrderUsings(OrderingOptions options, ICSharpFile file)
        {
            AlphabeticalUsingsStyle organiseUsingsFormatOption = options.AlphabeticalUsingDirectives;
            ExpandUsingsStyle       expandUsingsFormatOption   = options.ExpandUsingDirectives;

            // Exit if both options are to ignore
            if (organiseUsingsFormatOption == AlphabeticalUsingsStyle.Ignore && expandUsingsFormatOption == ExpandUsingsStyle.Ignore)
            {
                return;
            }

            foreach (ICSharpNamespaceDeclaration namespaceDeclaration in file.NamespaceDeclarations)
            {
                ProcessImports(namespaceDeclaration.Imports, organiseUsingsFormatOption, expandUsingsFormatOption, namespaceDeclaration);
            }

            ProcessImports(file.Imports, organiseUsingsFormatOption, expandUsingsFormatOption, file);
        }