コード例 #1
0
ファイル: OrderingRules.cs プロジェクト: sarvex/StyleCop
        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);
            }
        }
コード例 #2
0
        private static void ProcessImports(
            IList <IUsingDirective> originalImportsList,
            bool organiseUsings,
            bool expandUsings,
            ICSharpTypeAndNamespaceHolderDeclaration declaration)
        {
            if (originalImportsList == null || originalImportsList.Count == 0)
            {
                return;
            }

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

            sortedImportsList.AddRange(originalImportsList);

            if (organiseUsings)
            {
                sortedImportsList.Sort(new UsingStatementSorter());
            }

            bool alreadySorted   = true;
            bool alreadyExpanded = true;

            for (int i = 0; i < originalImportsList.Count; i++)
            {
                if (originalImportsList[i] != sortedImportsList[i])
                {
                    alreadySorted = false;
                    break;
                }

                IUsingAliasDirective aliasDirective = originalImportsList[i] as IUsingAliasDirective;
                if (aliasDirective != null)
                {
                    if (aliasDirective.ImportedSymbolName.GetText() != aliasDirective.GetFullyQualifiedNamespace())
                    {
                        alreadyExpanded = false;
                        break;
                    }
                }
            }

            if (alreadySorted && alreadyExpanded)
            {
                return;
            }

            foreach (IUsingDirective directive in sortedImportsList)
            {
                IUsingDirective newUsingDirective;

                if (expandUsings)
                {
                    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);
            }
        }