Exemple #1
0
        public static string GetFullName(this INamespaceDeclaration declaration, string termSeparator = ".")
        {
            Stack <INamespaceDeclaration> hierarchy = new Stack <INamespaceDeclaration>();
            INamespaceDeclaration         current   = declaration;

            while (current != null)
            {
                if (hierarchy.Contains(current))
                {
                    throw new InvalidOperationException("Circular namespace hierarchy.");
                }
                hierarchy.Push(current);
                current = current.Parent as INamespaceDeclaration;
            }
            StringBuilder result = new StringBuilder();
            bool          first  = true;

            while (hierarchy.Count > 0)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    result.Append(termSeparator);
                }
                result.Append(hierarchy.Pop().Name);
            }
            return(result.ToString());
        }
Exemple #2
0
        private static DocumentRange GetRange(IDeclaration declaration)
        {
            INamespaceDeclaration namespaceDeclaration = declaration as INamespaceDeclaration;

            if (namespaceDeclaration != null)
            {
                return(namespaceDeclaration.GetDeclaredNameDocumentRange());
            }
            return(declaration.GetNameDocumentRange());
        }
        public override void Execute(SolutionModel solutionModel, SelectionContext context)
        {
            FileModel fileModel;
            CodeSpan  selection;

            if (!solutionModel.IsEditorSelection(context, out fileModel, out selection))
            {
                return;
            }

            IUsingDirectiveSection mainSection = fileModel.InnerMost <IUsingDirectiveSection>(selection);

            if (mainSection.Exists)
            {
                HashSet <IUsingDirectiveSection> sections = new HashSet <IUsingDirectiveSection>();
                foreach (IUsingDirectiveSection section in fileModel.All <IUsingDirectiveSection>())
                {
                    INamespaceDeclaration nameSpaceItems = section.Enclosing <INamespaceDeclaration>();

                    //we are interested only in top level namespaces
                    if (nameSpaceItems.Exists && !nameSpaceItems.Enclosing <INamespaceDeclaration>().Exists)
                    {
                        sections.Add(section);
                    }
                }

                foreach (IUsingDirectiveSection section in sections)
                {
                    section.Insert(mainSection.Directives, fileModel);
                }
            }

            foreach (IUsingDirective directive in mainSection.Directives)
            {
                directive.Remove();
            }
        }