Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeTreeRequest" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="rawCodeItems">The raw code items.</param>
 /// <param name="sortOrder">The sort order.</param>
 /// <param name="nameFilter">The name filter.</param>
 internal CodeTreeRequest(Document document, SetCodeItems rawCodeItems, CodeSortOrder sortOrder, string nameFilter = null)
 {
     Document     = document;
     RawCodeItems = rawCodeItems;
     SortOrder    = sortOrder;
     NameFilter   = nameFilter;
 }
Example #2
0
 /// <summary>
 /// Clears any hierarchy information from the specified code items.
 /// </summary>
 /// <param name="codeItems">The code items.</param>
 private static void ClearHierarchyInformation(SetCodeItems codeItems)
 {
     foreach (var codeItem in codeItems.OfType<ICodeItemParent>())
     {
         codeItem.Children.Clear();
     }
 }
Example #3
0
 /// <summary>
 /// Clears any hierarchy information from the specified code items.
 /// </summary>
 /// <param name="codeItems">The code items.</param>
 private static void ClearHierarchyInformation(SetCodeItems codeItems)
 {
     foreach (var codeItem in codeItems.OfType <ICodeItemParent>())
     {
         codeItem.Children.Clear();
     }
 }
        /// <summary>
        /// Reorganizes the specified document.
        /// </summary>
        /// <param name="document">The document for reorganizing.</param>
        internal void Reorganize(Document document)
        {
            if (!_codeReorganizationAvailabilityLogic.CanReorganize(document, true))
            {
                return;
            }

            new UndoTransactionHelper(_package, $"CodeMaid Reorganize for '{document.Name}'").Run(
                delegate
            {
                OutputWindowHelper.DiagnosticWriteLine($"CodeReorganizationManager.Reorganize started for '{document.FullName}'");
                _package.IDE.StatusBar.Text = $"CodeMaid is reorganizing '{document.Name}'...";

                // Retrieve all relevant code items (excluding using statements).
                var rawCodeItems = _codeModelManager.RetrieveAllCodeItems(document).Where(x => !(x is CodeItemUsingStatement));

                // Build the code tree based on the current file sort order.
                var codeItems = new SetCodeItems(rawCodeItems);
                var codeTree  = CodeTreeBuilder.RetrieveCodeTree(new CodeTreeRequest(document, codeItems, CodeSortOrder.File));

                // Recursively reorganize the code tree.
                RecursivelyReorganize(codeTree);

                _package.IDE.StatusBar.Text = $"CodeMaid reorganized '{document.Name}'.";
                OutputWindowHelper.DiagnosticWriteLine($"CodeReorganizationManager.Reorganize completed for '{document.FullName}'");
            });
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeTreeRequest" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="rawCodeItems">The raw code items.</param>
 /// <param name="sortOrder">The sort order.</param>
 /// <param name="nameFilter">The name filter.</param>
 internal CodeTreeRequest(Document document, SetCodeItems rawCodeItems, CodeSortOrder sortOrder, string nameFilter)
 {
     Document = document;
     RawCodeItems = rawCodeItems;
     SortOrder = sortOrder;
     NameFilter = nameFilter;
 }
 /// <summary>
 /// Retrieves code items from each specified code element into the specified code items set.
 /// </summary>
 /// <param name="codeItems">The code items set for accumulation.</param>
 /// <param name="codeElements">The CodeElements to walk.</param>
 private static void RetrieveCodeItemsFromElements(SetCodeItems codeItems, CodeElements codeElements)
 {
     foreach (CodeElement child in codeElements)
     {
         RetrieveCodeItemsRecursively(codeItems, child);
     }
 }
Example #7
0
 /// <summary>
 /// Walks the given FileCodeModel, turning CodeElements into code items within the specified
 /// code items set.
 /// </summary>
 /// <param name="codeItems">The code items set for accumulation.</param>
 /// <param name="fcm">The FileCodeModel to walk.</param>
 private static void RetrieveCodeItems(SetCodeItems codeItems, FileCodeModel fcm)
 {
     if (fcm != null && fcm.CodeElements != null)
     {
         RetrieveCodeItemsFromElements(codeItems, fcm.CodeElements);
     }
 }
Example #8
0
 /// <summary>
 /// Walks the given FileCodeModel, turning CodeElements into code items within the specified
 /// code items set.
 /// </summary>
 /// <param name="codeItems">The code items set for accumulation.</param>
 /// <param name="fcm">The FileCodeModel to walk.</param>
 private static void RetrieveCodeItems(SetCodeItems codeItems, FileCodeModel fcm)
 {
     if (fcm != null && fcm.CodeElements != null)
     {
         RetrieveCodeItemsFromElements(codeItems, fcm.CodeElements);
     }
 }
Example #9
0
        /// <summary>
        /// Builds a code tree from the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>The built code tree, otherwise null.</returns>
        internal static SetCodeItems RetrieveCodeTree(CodeTreeRequest request)
        {
            ClearHierarchyInformation(request.RawCodeItems);

            SetCodeItems codeItems = null;

            switch (request.SortOrder)
            {
            case CodeSortOrder.Alpha:
                codeItems = OrganizeCodeItemsByAlphaSortOrder(request.RawCodeItems);
                break;

            case CodeSortOrder.File:
                codeItems = OrganizeCodeItemsByFileSortOrder(request.RawCodeItems);
                break;

            case CodeSortOrder.Type:
                codeItems = OrganizeCodeItemsByTypeSortOrder(request.RawCodeItems);
                break;
            }

            if (!string.IsNullOrWhiteSpace(request.NameFilter))
            {
                RecursivelyFilter(codeItems, request.NameFilter);
            }

            return(codeItems);
        }
Example #10
0
        /// <summary>
        /// Recursively sorts the specified code items by the specified sort comparer.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        /// <param name="sortComparer">The sort comparer.</param>
        private static void RecursivelySort(SetCodeItems codeItems, IComparer <BaseCodeItem> sortComparer)
        {
            codeItems.Sort(sortComparer);

            foreach (var codeItem in codeItems.OfType <ICodeItemParent>())
            {
                RecursivelySort(codeItem.Children, sortComparer);
            }
        }
Example #11
0
        /// <summary>
        /// Updates the code items whose outlining is being synchronized by this manager.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        public void UpdateCodeItems(SetCodeItems codeItems)
        {
            TearDownCodeItemParents();

            // Retrieve and cache an updated list of code item parents.
            _codeItemParents = RecursivelyGetAllCodeItemParents(codeItems);

            InitializeCodeItemParents();
        }
Example #12
0
        /// <summary>
        /// Update the view model's raw set of code items based on the specified code items.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        private void UpdateViewModelRawCodeItems(SetCodeItems codeItems)
        {
            // Create a copy of the original collection, filtering out undesired items.
            var filteredCodeItems = new SetCodeItems(
                codeItems.Where(x => !(x is CodeItemUsingStatement || x is CodeItemNamespace)));

            _viewModel.RawCodeItems = filteredCodeItems;
            _viewModel.IsLoading = false;
            _viewModel.IsRefreshing = false;
        }
Example #13
0
        /// <summary>
        /// Walks the given document and constructs a <see cref="SetCodeItems" /> of CodeItems
        /// within it including regions.
        /// </summary>
        /// <param name="document">The document to walk.</param>
        /// <returns>The set of code items within the document, including regions.</returns>
        internal SetCodeItems RetrieveAllCodeItems(Document document)
        {
            var codeItems = new SetCodeItems();

            var fileCodeModel = RetrieveFileCodeModel(document.ProjectItem);
            RetrieveCodeItems(codeItems, fileCodeModel);

            codeItems.AddRange(_codeModelHelper.RetrieveCodeRegions(document.GetTextDocument()));

            return codeItems;
        }
Example #14
0
        /// <summary>
        /// Walks the given document and constructs a <see cref="SetCodeItems" /> of CodeItems
        /// within it including regions.
        /// </summary>
        /// <param name="document">The document to walk.</param>
        /// <returns>The set of code items within the document, including regions.</returns>
        internal SetCodeItems RetrieveAllCodeItems(Document document)
        {
            var codeItems = new SetCodeItems();

            var fileCodeModel = RetrieveFileCodeModel(document.ProjectItem);

            RetrieveCodeItems(codeItems, fileCodeModel);

            codeItems.AddRange(_codeModelHelper.RetrieveCodeRegions(document.GetTextDocument()));

            return(codeItems);
        }
Example #15
0
        /// <summary>
        /// Walks the given document and constructs a <see cref="SetCodeItems" /> of CodeItems
        /// within it including regions.
        /// </summary>
        /// <param name="document">The document to walk.</param>
        /// <returns>The set of code items within the document, including regions.</returns>
        internal SetCodeItems RetrieveAllCodeItems(Document document)
        {
            var codeItems = new SetCodeItems();

            if (document.ProjectItem != null)
            {
                RetrieveCodeItems(codeItems, document.ProjectItem.FileCodeModel);
            }

            codeItems.AddRange(_codeModelHelper.RetrieveCodeRegions(document.GetTextDocument()));

            return codeItems;
        }
Example #16
0
        /// <summary>
        /// Recursive method for creating a code item for the specified code element, adding it to
        /// the specified code items set and recursing into all of the code element's children.
        /// </summary>
        /// <param name="codeItems">The code items set for accumulation.</param>
        /// <param name="codeElement">The CodeElement to walk (add and recurse).</param>
        private static void RetrieveCodeItemsRecursively(SetCodeItems codeItems, CodeElement codeElement)
        {
            var parentCodeItem = FactoryCodeItems.CreateCodeItemElement(codeElement);

            if (parentCodeItem != null)
            {
                codeItems.Add(parentCodeItem);
            }

            if (codeElement.Children != null)
            {
                RetrieveCodeItemsFromElements(codeItems, codeElement.Children);
            }
        }
Example #17
0
 /// <summary>
 /// Retrieves code items from each specified code element into the specified code items set.
 /// </summary>
 /// <param name="codeItems">The code items set for accumulation.</param>
 /// <param name="codeElements">The CodeElements to walk.</param>
 private static void RetrieveCodeItemsFromElements(SetCodeItems codeItems, CodeElements codeElements)
 {
     if (Settings.Default.General_Multithread)
     {
         Parallel.ForEach(codeElements.OfType <CodeElement>(), child => RetrieveCodeItemsRecursively(codeItems, child));
     }
     else
     {
         foreach (CodeElement child in codeElements)
         {
             RetrieveCodeItemsRecursively(codeItems, child);
         }
     }
 }
Example #18
0
        /// <summary>
        /// Organizes the specified code items by file sort order.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByFileSortOrder(IEnumerable <BaseCodeItem> rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                // Sort the raw list of code items by starting position.
                var sortedCodeItems = rawCodeItems.OrderBy(x => x.StartOffset);
                var codeItemStack   = new Stack <BaseCodeItem>();

                foreach (var codeItem in sortedCodeItems)
                {
                    while (true)
                    {
                        if (!codeItemStack.Any())
                        {
                            organizedCodeItems.Add(codeItem);
                            codeItemStack.Push(codeItem);
                            break;
                        }

                        var top = codeItemStack.Peek();
                        if (codeItem.EndOffset < top.EndOffset)
                        {
                            var topParent = top as ICodeItemParent;
                            if (topParent != null)
                            {
                                topParent.Children.Add(codeItem);
                                codeItemStack.Push(codeItem);
                                break;
                            }

                            if (codeItem is CodeItemRegion)
                            {
                                // Skip regions within non-parentable items (e.g. in methods).
                                break;
                            }
                        }

                        codeItemStack.Pop();
                    }
                }
            }

            return(organizedCodeItems);
        }
Example #19
0
        /// <summary>
        /// Organizes the specified code items by alpha sort order.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByAlphaSortOrder(SetCodeItems rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                var codeItemsWithoutRegions = rawCodeItems.Where(x => !(x is CodeItemRegion));

                var structuredCodeItems = OrganizeCodeItemsByFileSortOrder(codeItemsWithoutRegions);
                organizedCodeItems.AddRange(structuredCodeItems);

                // Sort the list of code items by name recursively.
                RecursivelySort(organizedCodeItems, new CodeItemNameComparer());
            }

            return(organizedCodeItems);
        }
        /// <summary>
        /// Recursively gets the children in a depth-first fashion for the specified parent without
        /// delving into nested element parents.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <returns>The recursive set of children.</returns>
        public static SetCodeItems GetChildrenRecursive(this ICodeItemParent parent)
        {
            var children = new SetCodeItems();

            foreach (var child in parent.Children)
            {
                children.Add(child);

                var childAsParent = child as ICodeItemParent;
                if (childAsParent != null && !(child is BaseCodeItemElementParent))
                {
                    children.AddRange(childAsParent.GetChildrenRecursive());
                }
            }

            return children;
        }
Example #21
0
        /// <summary>
        /// Recursively filter specified code items by the name.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        /// <param name="nameFilter">The name filter.</param>
        private static void RecursivelyFilter(SetCodeItems codeItems, string nameFilter)
        {
            codeItems.RemoveAll(codeItem =>
            {
                var codeItemParent = codeItem as ICodeItemParent;
                if (codeItemParent != null)
                {
                    RecursivelyFilter(codeItemParent.Children, nameFilter);
                    if (codeItemParent.Children.Any())
                    {
                        return(false);
                    }
                }

                return(codeItem.Name.IndexOf(nameFilter, StringComparison.InvariantCultureIgnoreCase) < 0);
            });
        }
Example #22
0
        /// <summary>
        /// Organizes the specified code items by alpha sort order.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByAlphaSortOrder(SetCodeItems rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                var codeItemsWithoutRegions = rawCodeItems.Where(x => !(x is CodeItemRegion));

                var structuredCodeItems = OrganizeCodeItemsByFileSortOrder(codeItemsWithoutRegions);
                organizedCodeItems.AddRange(structuredCodeItems);

                // Sort the list of code items by name recursively.
                RecursivelySort(organizedCodeItems, new CodeItemNameComparer());
            }

            return organizedCodeItems;
        }
        /// <summary>
        /// Recursively gets the children in a depth-first fashion for the specified parent without
        /// delving into nested element parents.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <returns>The recursive set of children.</returns>
        public static SetCodeItems GetChildrenRecursive(this ICodeItemParent parent)
        {
            var children = new SetCodeItems();

            foreach (var child in parent.Children)
            {
                children.Add(child);

                var childAsParent = child as ICodeItemParent;
                if (childAsParent != null && !(child is BaseCodeItemElementParent))
                {
                    children.AddRange(childAsParent.GetChildrenRecursive());
                }
            }

            return(children);
        }
Example #24
0
        /// <summary>
        /// Update the view model's raw set of code items based on the specified code items.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        private void UpdateViewModelRawCodeItems(SetCodeItems codeItems)
        {
            // Create a copy of the original collection, filtering out undesired items.
            var filteredCodeItems = new SetCodeItems(
                codeItems.Where(x => !(x is CodeItemUsingStatement || x is CodeItemNamespace)));

            _viewModel.RawCodeItems = filteredCodeItems;
            _viewModel.IsLoading    = false;
            _viewModel.IsRefreshing = false;

            ////接口代码同步
            //if (Settings.Default.Digging_SynchronizeRealize&&filteredCodeItems[0].Kind==KindCodeItem.Class&&filteredCodeItems[0].Name.EndsWith("Service"))
            //{
            //    #region 接口同步代码实现
            //    //ITextTemplating vst4 = GetService(typeof(STextTemplating)) as ITextTemplating;
            //    //ITextTemplatingSessionHost host = vst4 as ITextTemplatingSessionHost;

            //    //host.Session = host.CreateSession();
            //    //host.Session["parameter1"] = "接口模板的实现:";
            //    //host.Session["parameter2"] = DateTime.Now;
            //    //string input = System.IO.File.ReadAllText("Interface.tt");
            //    //string output = vst4.ProcessTemplate("", input);
            //    System.Threading.Tasks.Task.Run(()=>{
            //            if (Package.ActiveDocument!=null)
            //            {
            //                ProjectItem curItem = Package.ActiveDocument.ProjectItem;
            //                if (curItem == null)
            //                {
            //                    return;
            //                }
            //                string strInterName = "I"+curItem.Name;
            //                //var s = curItem.ContainingProject.ProjectItems.Cast<ProjectItem>().ToList();
            //                if (!curItem.ContainingProject.ProjectItems.IsExists(strInterName))
            //                {
            //                    Solution3 sln = curItem.DTE.Solution as Solution3;
            //                    string strInterTemplateItemPath = sln.GetProjectItemTemplate("Interface.zip", "CSharp");
            //                    curItem.ProjectItems.AddFromTemplate(strInterTemplateItemPath,  strInterName);
            //                }
            //                addInterface(curItem);
            //            }
            //    });
            //    #endregion
            //}
        }
Example #25
0
        /// <summary>
        /// Reorganizes the specified document.
        /// </summary>
        /// <param name="document">The document for reorganizing.</param>
        /// <param name="isAutoSave">A flag indicating if occurring due to auto-save.</param>
        internal void Reorganize(Document document, bool isAutoSave)
        {
            if (!CanReorganize(document))
            {
                return;
            }

            _undoTransactionHelper.Run(
                () => !(isAutoSave && Settings.Default.General_SkipUndoTransactionsDuringAutoCleanupOnSave),
                delegate
            {
                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorderManager.Reorganize started for '{0}'", document.FullName));

                _package.IDE.StatusBar.Text = string.Format("CodeMaid is reorganizing '{0}'...", document.Name);

                // Retrieve all relevant code items (excluding using statements).
                var rawCodeItems = _codeModelManager.RetrieveAllCodeItems(document).Where(x => !(x is CodeItemUsingStatement));

                // Build the code tree based on the current file layout.
                var codeItems = new SetCodeItems(rawCodeItems);
                var codeTree  = CodeTreeBuilder.RetrieveCodeTree(new CodeTreeRequest(document, codeItems, TreeLayoutMode.FileLayout));

                // Recursively reorganize the code tree.
                RecursivelyReorganize(codeTree);

                _package.IDE.StatusBar.Text = string.Format("CodeMaid reorganized '{0}'.", document.Name);

                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorderManager.Reorganize completed for '{0}'", document.FullName));
            },
                delegate(Exception ex)
            {
                OutputWindowHelper.ExceptionWriteLine(
                    string.Format("Stopped reorganizing '{0}'", document.Name), ex);
                _package.IDE.StatusBar.Text = string.Format("CodeMaid stopped reorganizing '{0}'.  See output window for more details.", document.Name);
            });
        }
Example #26
0
        /// <summary>
        /// Builds a code tree from the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>The built code tree, otherwise null.</returns>
        internal static SetCodeItems RetrieveCodeTree(CodeTreeRequest request)
        {
            ClearHierarchyInformation(request.RawCodeItems);

            SetCodeItems codeItems = null;

            switch (request.LayoutMode)
            {
            case TreeLayoutMode.AlphaLayout:
                codeItems = OrganizeCodeItemsByAlphaLayout(request.RawCodeItems);
                break;

            case TreeLayoutMode.FileLayout:
                codeItems = OrganizeCodeItemsByFileLayout(request.RawCodeItems);
                break;

            case TreeLayoutMode.TypeLayout:
                codeItems = OrganizeCodeItemsByTypeLayout(request.RawCodeItems);
                break;
            }

            return(codeItems);
        }
Example #27
0
        /// <summary>
        /// Builds a code tree from the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>The built code tree, otherwise null.</returns>
        internal static SetCodeItems RetrieveCodeTree(CodeTreeRequest request)
        {
            ClearHierarchyInformation(request.RawCodeItems);

            SetCodeItems codeItems = null;

            switch (request.SortOrder)
            {
            case CodeSortOrder.Alpha:
                codeItems = OrganizeCodeItemsByAlphaSortOrder(request.RawCodeItems);
                break;

            case CodeSortOrder.File:
                codeItems = OrganizeCodeItemsByFileSortOrder(request.RawCodeItems);
                break;

            case CodeSortOrder.Type:
                codeItems = OrganizeCodeItemsByTypeSortOrder(request.RawCodeItems);
                break;
            }

            return(codeItems);
        }
Example #28
0
        /// <summary>
        /// Organizes the specified code items by type sort order.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByTypeSortOrder(SetCodeItems rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                var codeItemsWithoutRegions = rawCodeItems.Where(x => !(x is CodeItemRegion));

                var structuredCodeItems = OrganizeCodeItemsByFileSortOrder(codeItemsWithoutRegions);
                organizedCodeItems.AddRange(structuredCodeItems);

                // Sort the list of code items by type recursively.
                RecursivelySort(organizedCodeItems, new CodeItemTypeComparer());

                // Group the list of code items by type recursively.
                foreach (var codeItem in organizedCodeItems.OfType <ICodeItemParent>())
                {
                    RecursivelyGroupByType(codeItem);
                }
            }

            return(organizedCodeItems);
        }
        /// <summary>
        /// Reorganizes the specified document.
        /// </summary>
        /// <param name="document">The document for reorganizing.</param>
        internal void Reorganize(Document document)
        {
            if (!_codeReorganizationAvailabilityLogic.CanReorganize(document))
            {
                return;
            }

            _undoTransactionHelper.Run(
                delegate
            {
                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorganizationManager.Reorganize started for '{0}'", document.FullName));

                _package.IDE.StatusBar.Text = string.Format("CodeMaid is reorganizing '{0}'...", document.Name);

                // Retrieve all relevant code items (excluding using statements).
                var rawCodeItems = _codeModelManager.RetrieveAllCodeItems(document).Where(x => !(x is CodeItemUsingStatement));

                // Build the code tree based on the current file sort order.
                var codeItems = new SetCodeItems(rawCodeItems);
                var codeTree  = CodeTreeBuilder.RetrieveCodeTree(new CodeTreeRequest(document, codeItems, CodeSortOrder.File));

                // Recursively reorganize the code tree.
                RecursivelyReorganize(codeTree);

                _package.IDE.StatusBar.Text = string.Format("CodeMaid reorganized '{0}'.", document.Name);

                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorganizationManager.Reorganize completed for '{0}'", document.FullName));
            },
                delegate(Exception ex)
            {
                OutputWindowHelper.ExceptionWriteLine(
                    string.Format("Stopped reorganizing '{0}'", document.Name), ex);
                _package.IDE.StatusBar.Text = string.Format("CodeMaid stopped reorganizing '{0}'.  See output window for more details.", document.Name);
            });
        }
Example #30
0
        /// <summary>
        /// Organizes the specified code items by file layout.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByFileLayout(IEnumerable <BaseCodeItem> rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                // Sort the raw list of code items by starting position.
                var sortedCodeItems = rawCodeItems.OrderBy(x => x.StartOffset);
                var codeItemStack   = new Stack <BaseCodeItem>();

                foreach (var codeItem in sortedCodeItems)
                {
                    while (true)
                    {
                        if (!codeItemStack.Any())
                        {
                            organizedCodeItems.Add(codeItem);
                            codeItemStack.Push(codeItem);
                            break;
                        }

                        var top = codeItemStack.Peek() as ICodeItemParent;
                        if (top != null && codeItem.EndOffset < top.EndOffset)
                        {
                            top.Children.Add(codeItem);
                            codeItemStack.Push(codeItem);
                            break;
                        }

                        codeItemStack.Pop();
                    }
                }
            }

            return(organizedCodeItems);
        }
Example #31
0
        /// <summary>
        /// Recursive method for creating a code item for the specified code element, adding it to
        /// the specified code items set and recursing into all of the code element's children.
        /// </summary>
        /// <param name="codeItems">The code items set for accumulation.</param>
        /// <param name="codeElement">The CodeElement to walk (add and recurse).</param>
        private static void RetrieveCodeItemsRecursively(SetCodeItems codeItems, CodeElement codeElement)
        {
            var parentCodeItem = FactoryCodeItems.CreateCodeItemElement(codeElement);
            if (parentCodeItem != null)
            {
                codeItems.Add(parentCodeItem);
            }

            if (codeElement.Children != null)
            {
                RetrieveCodeItemsFromElements(codeItems, codeElement.Children);
            }
        }
Example #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeTreeRequest" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="rawCodeItems">The raw code items.</param>
 /// <param name="sortOrder">The sort order.</param>
 internal CodeTreeRequest(Document document, SetCodeItems rawCodeItems, CodeSortOrder sortOrder)
     : this(document, rawCodeItems, sortOrder, null)
 {
 }
Example #33
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeTreeRequest" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="rawCodeItems">The raw code items.</param>
 /// <param name="sortOrder">The sort order.</param>
 internal CodeTreeRequest(Document document, SetCodeItems rawCodeItems, CodeSortOrder sortOrder)
 {
     Document     = document;
     RawCodeItems = rawCodeItems;
     SortOrder    = sortOrder;
 }
Example #34
0
 /// <summary>
 /// Retrieves code items from each specified code element into the specified code items set.
 /// </summary>
 /// <param name="codeItems">The code items set for accumulation.</param>
 /// <param name="codeElements">The CodeElements to walk.</param>
 private static void RetrieveCodeItemsFromElements(SetCodeItems codeItems, CodeElements codeElements)
 {
     foreach (CodeElement child in codeElements)
     {
         RetrieveCodeItemsRecursively(codeItems, child);
     }
 }
        /// <summary>
        /// Reorganizes the specified document.
        /// </summary>
        /// <param name="document">The document for reorganizing.</param>
        /// <param name="isAutoSave">A flag indicating if occurring due to auto-save.</param>
        internal void Reorganize(Document document, bool isAutoSave)
        {
            if (!_codeReorganizationAvailabilityLogic.CanReorganize(document)) return;

            _undoTransactionHelper.Run(
                () => !(isAutoSave && Settings.Default.General_SkipUndoTransactionsDuringAutoCleanupOnSave),
                delegate
                {
                    OutputWindowHelper.DiagnosticWriteLine(
                        string.Format("CodeReorganizationManager.Reorganize started for '{0}'", document.FullName));

                    _package.IDE.StatusBar.Text = string.Format("CodeMaid is reorganizing '{0}'...", document.Name);

                    // Retrieve all relevant code items (excluding using statements).
                    var rawCodeItems = _codeModelManager.RetrieveAllCodeItems(document).Where(x => !(x is CodeItemUsingStatement));

                    // Build the code tree based on the current file sort order.
                    var codeItems = new SetCodeItems(rawCodeItems);
                    var codeTree = CodeTreeBuilder.RetrieveCodeTree(new CodeTreeRequest(document, codeItems, CodeSortOrder.File));

                    // Recursively reorganize the code tree.
                    RecursivelyReorganize(codeTree);

                    _package.IDE.StatusBar.Text = string.Format("CodeMaid reorganized '{0}'.", document.Name);

                    OutputWindowHelper.DiagnosticWriteLine(
                        string.Format("CodeReorganizationManager.Reorganize completed for '{0}'", document.FullName));
                },
                delegate(Exception ex)
                {
                    OutputWindowHelper.ExceptionWriteLine(
                        string.Format("Stopped reorganizing '{0}'", document.Name), ex);
                    _package.IDE.StatusBar.Text = string.Format("CodeMaid stopped reorganizing '{0}'.  See output window for more details.", document.Name);
                });
        }
Example #36
0
        /// <summary>
        /// Recursively filter specified code items by the name.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        /// <param name="nameFilter">The name filter.</param>
        private static void RecursivelyFilter(SetCodeItems codeItems, string nameFilter)
        {
            codeItems.RemoveAll(codeItem =>
            {
                var codeItemParent = codeItem as ICodeItemParent;
                if (codeItemParent != null)
                {
                    RecursivelyFilter(codeItemParent.Children, nameFilter);
                    if (codeItemParent.Children.Any())
                    {
                        return false;
                    }
                }

                return codeItem.Name.IndexOf(nameFilter, StringComparison.InvariantCultureIgnoreCase) < 0;
            });
        }
Example #37
0
        /// <summary>
        /// Organizes the specified code items by type sort order.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByTypeSortOrder(SetCodeItems rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                var codeItemsWithoutRegions = rawCodeItems.Where(x => !(x is CodeItemRegion));

                var structuredCodeItems = OrganizeCodeItemsByFileSortOrder(codeItemsWithoutRegions);
                organizedCodeItems.AddRange(structuredCodeItems);

                // Sort the list of code items by type recursively.
                RecursivelySort(organizedCodeItems, new CodeItemTypeComparer());

                // Group the list of code items by type recursively.
                foreach (var codeItem in organizedCodeItems.OfType<ICodeItemParent>())
                {
                    RecursivelyGroupByType(codeItem);
                }
            }

            return organizedCodeItems;
        }
Example #38
0
        /// <summary>
        /// Update the view model's raw set of code items based on the specified code items.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        private void UpdateViewModelRawCodeItems(SetCodeItems codeItems)
        {
            // Create a copy of the original collection, filtering out undesired items.
            var filteredCodeItems = new SetCodeItems(
                codeItems.Where(x => !(x is CodeItemUsingStatement || x is CodeItemNamespace)));

            _viewModel.RawCodeItems = filteredCodeItems;
            _viewModel.IsLoading = false;
            _viewModel.IsRefreshing = false;
        }
Example #39
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeModel" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 internal CodeModel(Document document)
 {
     CodeItems         = new SetCodeItems();
     Document          = document;
     IsBuiltWaitHandle = new ManualResetEvent(false);
 }
Example #40
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SnapshotCodeItems" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="codeItems">The code items.</param>
 internal SnapshotCodeItems(Document document, SetCodeItems codeItems)
 {
     Document = document;
     CodeItems = codeItems;
 }
        /// <summary>
        /// Abstract initialization code for <see cref="BaseCodeItemElementParent" />.
        /// </summary>
        protected BaseCodeItemElementParent()
        {
            Children = new SetCodeItems();

            _Namespace = new Lazy<string>(() => null);
        }
Example #42
0
        /// <summary>
        /// Recursively sorts the specified code items by the specified sort comparer.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        /// <param name="sortComparer">The sort comparer.</param>
        private static void RecursivelySort(SetCodeItems codeItems, IComparer<BaseCodeItem> sortComparer)
        {
            codeItems.Sort(sortComparer);

            foreach (var codeItem in codeItems.OfType<ICodeItemParent>())
            {
                RecursivelySort(codeItem.Children, sortComparer);
            }
        }
        /// <summary>
        /// Recursively retrives all code item parents within the specified code items.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        /// <returns>The code item parents.</returns>
        private static IEnumerable<ICodeItemParent> RecursivelyGetAllCodeItemParents(SetCodeItems codeItems)
        {
            if (codeItems == null)
            {
                return Enumerable.Empty<ICodeItemParent>();
            }

            var parents = codeItems.OfType<ICodeItemParent>().ToList();

            return parents.Union(parents.SelectMany(x => RecursivelyGetAllCodeItemParents(x.Children)));
        }
        /// <summary>
        /// Updates the code items whose outlining is being synchronized by this manager.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        public void UpdateCodeItems(SetCodeItems codeItems)
        {
            TearDownCodeItemParents();

            // Retrieve and cache an updated list of code item parents.
            _codeItemParents = RecursivelyGetAllCodeItemParents(codeItems);

            InitializeCodeItemParents();
        }
Example #45
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeModel" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 internal CodeModel(Document document)
 {
     CodeItems = new SetCodeItems();
     Document = document;
     IsBuiltWaitHandle = new ManualResetEvent(false);
 }
Example #46
0
        /// <summary>
        /// Organizes the specified code items by file sort order.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByFileSortOrder(IEnumerable<BaseCodeItem> rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                // Sort the raw list of code items by starting position.
                var sortedCodeItems = rawCodeItems.OrderBy(x => x.StartOffset);
                var codeItemStack = new Stack<BaseCodeItem>();

                foreach (var codeItem in sortedCodeItems)
                {
                    while (true)
                    {
                        if (!codeItemStack.Any())
                        {
                            organizedCodeItems.Add(codeItem);
                            codeItemStack.Push(codeItem);
                            break;
                        }

                        var top = codeItemStack.Peek();
                        if (codeItem.EndOffset < top.EndOffset)
                        {
                            var topParent = top as ICodeItemParent;
                            if (topParent != null)
                            {
                                topParent.Children.Add(codeItem);
                                codeItemStack.Push(codeItem);
                                break;
                            }

                            if (codeItem is CodeItemRegion)
                            {
                                // Skip regions within non-parentable items (e.g. in methods).
                                break;
                            }
                        }

                        codeItemStack.Pop();
                    }
                }
            }

            return organizedCodeItems;
        }
Example #47
0
        /// <summary>
        /// Organizes the specified code items by file layout.
        /// </summary>
        /// <param name="rawCodeItems">The raw code items.</param>
        /// <returns>The organized code items.</returns>
        private static SetCodeItems OrganizeCodeItemsByFileLayout(IEnumerable<BaseCodeItem> rawCodeItems)
        {
            var organizedCodeItems = new SetCodeItems();

            if (rawCodeItems != null)
            {
                // Sort the raw list of code items by starting position.
                var sortedCodeItems = rawCodeItems.OrderBy(x => x.StartOffset);
                var codeItemStack = new Stack<BaseCodeItem>();

                foreach (var codeItem in sortedCodeItems)
                {
                    while (true)
                    {
                        if (!codeItemStack.Any())
                        {
                            organizedCodeItems.Add(codeItem);
                            codeItemStack.Push(codeItem);
                            break;
                        }

                        var top = codeItemStack.Peek() as ICodeItemParent;
                        if (top != null && codeItem.EndOffset < top.EndOffset)
                        {
                            top.Children.Add(codeItem);
                            codeItemStack.Push(codeItem);
                            break;
                        }

                        codeItemStack.Pop();
                    }
                }
            }

            return organizedCodeItems;
        }
Example #48
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeItemRegion" /> class.
 /// </summary>
 public CodeItemRegion()
 {
     Children = new SetCodeItems();
 }
Example #49
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeTreeRequest" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="rawCodeItems">The raw code items.</param>
 /// <param name="layoutMode">The layout mode.</param>
 internal CodeTreeRequest(Document document, SetCodeItems rawCodeItems, TreeLayoutMode layoutMode)
 {
     Document = document;
     RawCodeItems = rawCodeItems;
     LayoutMode = layoutMode;
 }
Example #50
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeTreeRequest" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="rawCodeItems">The raw code items.</param>
 /// <param name="layoutMode">The layout mode.</param>
 internal CodeTreeRequest(Document document, SetCodeItems rawCodeItems, TreeLayoutMode layoutMode)
 {
     Document     = document;
     RawCodeItems = rawCodeItems;
     LayoutMode   = layoutMode;
 }
Example #51
0
        /// <summary>
        /// Recursively retrives all code item parents within the specified code items.
        /// </summary>
        /// <param name="codeItems">The code items.</param>
        /// <returns>The code item parents.</returns>
        private static IEnumerable <ICodeItemParent> RecursivelyGetAllCodeItemParents(SetCodeItems codeItems)
        {
            if (codeItems == null)
            {
                return(Enumerable.Empty <ICodeItemParent>());
            }

            var parents = codeItems.OfType <ICodeItemParent>().ToList();

            return(parents.Union(parents.SelectMany(x => RecursivelyGetAllCodeItemParents(x.Children))));
        }
Example #52
0
 /// <summary>
 /// Retrieves code items from each specified code element into the specified code items set.
 /// </summary>
 /// <param name="codeItems">The code items set for accumulation.</param>
 /// <param name="codeElements">The CodeElements to walk.</param>
 private static void RetrieveCodeItemsFromElements(SetCodeItems codeItems, CodeElements codeElements)
 {
     if (Settings.Default.General_Multithread)
     {
         Parallel.ForEach(codeElements.OfType<CodeElement>(), child => RetrieveCodeItemsRecursively(codeItems, child));
     }
     else
     {
         foreach (CodeElement child in codeElements)
         {
             RetrieveCodeItemsRecursively(codeItems, child);
         }
     }
 }