/// <summary>
        /// Called to execute the command.
        /// </summary>
        protected override void OnExecute()
        {
            base.OnExecute();

            var spade = Package.Spade;
            if (spade != null)
            {
                var region = new CodeItemRegion { Name = "New Region" };
                var startPoint = spade.SelectedItems.OrderBy(x => x.StartOffset).First().StartPoint;
                var endPoint = spade.SelectedItems.OrderBy(x => x.EndOffset).Last().EndPoint;

                _undoTransactionHelper.Run(() =>
                {
                    // Create the new region.
                    _generateRegionLogic.InsertEndRegionTag(region, endPoint);
                    _generateRegionLogic.InsertRegionTag(region, startPoint);

                    // Move to that element.
                    TextDocumentHelper.MoveToCodeItem(spade.Document, region, Settings.Default.Digging_CenterOnWhole);

                    // Highlight the line of text for renaming.
                    var textDocument = spade.Document.GetTextDocument();
                    textDocument.Selection.EndOfLine(true);
                    textDocument.Selection.SwapAnchor();
                });

                spade.Refresh();
            }
        }
Пример #2
0
        /// <summary>
        /// Inserts an #endregion tag for the specified region following the specified end point.
        /// </summary>
        /// <param name="region">The region to end.</param>
        /// <param name="endPoint">The end point.</param>
        /// <returns>The updated cursor.</returns>
        public EditPoint InsertEndRegionTag(CodeItemRegion region, EditPoint endPoint)
        {
            var cursor = endPoint.CreateEditPoint();

            // If the cursor is not preceeded only by whitespace, insert a new line.
            var firstNonWhitespaceIndex = cursor.GetLine().TakeWhile(char.IsWhiteSpace).Count();
            if (cursor.DisplayColumn > firstNonWhitespaceIndex + 1)
            {
                cursor.Insert(Environment.NewLine);
            }

            cursor.Insert("#endregion");

            if (Settings.Default.Cleaning_UpdateEndRegionDirectives)
            {
                cursor.Insert(" " + region.Name);
            }

            // If the cursor is not followed only by whitespace, insert a new line.
            var lastNonWhitespaceIndex = cursor.GetLine().TrimEnd().Length;
            if (cursor.DisplayColumn < lastNonWhitespaceIndex + 1)
            {
                cursor.Insert(Environment.NewLine);
                cursor.LineUp();
                cursor.EndOfLine();
            }

            endPoint.SmartFormat(cursor);

            region.EndPoint = cursor.CreateEditPoint();

            var regionWrapper = new[] { region };
            _insertBlankLinePaddingLogic.InsertPaddingBeforeEndRegionTags(regionWrapper);
            _insertBlankLinePaddingLogic.InsertPaddingAfterEndRegionTags(regionWrapper);

            return cursor;
        }
Пример #3
0
        /// <summary>
        /// Removes the region tags from the specified region.
        /// </summary>
        /// <param name="region">The region to update.</param>
        internal void RemoveRegion(CodeItemRegion region)
        {
            if (region == null || region.IsInvalidated || region.IsPseudoGroup || region.StartLine <= 0 || region.EndLine <= 0)
            {
                return;
            }

            new UndoTransactionHelper(_package, "CodeMaid Remove Region " + region.Name).Run(() =>
            {
                var end = region.EndPoint.CreateEditPoint();
                end.StartOfLine();
                end.Delete(end.LineLength);
                end.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
                end.Insert(Environment.NewLine);

                var start = region.StartPoint.CreateEditPoint();
                start.StartOfLine();
                start.Delete(start.LineLength);
                start.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
                start.Insert(Environment.NewLine);

                region.IsInvalidated = true;
            });
        }
Пример #4
0
        /// <summary>
        /// Recursively groups the children within the specified item based on their type.
        /// </summary>
        /// <param name="codeItem">The code item.</param>
        private static void RecursivelyGroupByType(ICodeItemParent codeItem)
        {
            // Skip any code item that is already a region or does not have children.
            if (codeItem.Kind == KindCodeItem.Region || !codeItem.Children.Any())
            {
                return;
            }

            // Capture the current children, then clear them out so they can be re-added.
            var children = codeItem.Children.ToArray();
            codeItem.Children.Clear();

            CodeItemRegion group = null;
            int groupOrder = -1;

            foreach (var child in children)
            {
                var memberTypeSetting = MemberTypeSettingHelper.LookupByKind(child.Kind);

                // Create a new group unless the right kind has already been defined.
                if (group == null || memberTypeSetting.Order != groupOrder)
                {
                    group = new CodeItemRegion { Name = memberTypeSetting.EffectiveName, IsPseudoGroup = true };
                    groupOrder = memberTypeSetting.Order;

                    codeItem.Children.Add(group);
                }

                // Add the child to the group and recurse.
                group.Children.Add(child);

                var childAsParent = child as ICodeItemParent;
                if (childAsParent != null)
                {
                    RecursivelyGroupByType(childAsParent);
                }
            }
        }
Пример #5
0
 /// <summary>
 /// Determines if the specified code item belongs in the specified region.
 /// </summary>
 /// <param name="codeItem">The code item.</param>
 /// <param name="region">The region.</param>
 /// <returns>True if the specified code item belongs in the specified region, otherwise false.</returns>
 private bool CodeItemBelongsInRegion(BaseCodeItem codeItem, CodeItemRegion region)
 {
     return codeItem != null && _regionComparerByName.Equals(region, ComposeRegionForCodeItem(codeItem));
 }
Пример #6
0
        /// <summary>
        /// Inserts a #region tag for the specified region preceding the specified start point.
        /// </summary>
        /// <param name="region">The region to start.</param>
        /// <param name="startPoint">The starting point.</param>
        /// <returns>The updated cursor.</returns>
        private EditPoint InsertRegionTag(CodeItemRegion region, EditPoint startPoint)
        {
            var cursor = startPoint.CreateEditPoint();

            // If the cursor is not preceeded only by whitespace, insert a new line.
            var firstNonWhitespaceIndex = cursor.GetLine().TakeWhile(char.IsWhiteSpace).Count();
            if (cursor.DisplayColumn > firstNonWhitespaceIndex + 1)
            {
                cursor.Insert(Environment.NewLine);
            }

            cursor.Insert(string.Format("#region {0}{1}", region.Name, Environment.NewLine));

            startPoint.SmartFormat(cursor);

            region.StartPoint = cursor.CreateEditPoint();
            region.StartPoint.LineUp();
            region.StartPoint.StartOfLine();

            var regionWrapper = new[] { region };
            _insertBlankLinePaddingLogic.InsertPaddingBeforeRegionTags(regionWrapper);
            _insertBlankLinePaddingLogic.InsertPaddingAfterRegionTags(regionWrapper);

            return cursor;
        }
 /// <summary>
 /// Determines if the specified region is a candidate for removal.
 /// </summary>
 /// <param name="region">The region.</param>
 /// <returns>True if the region can be removed, otherwise false.</returns>
 private static bool IsRemoveableRegion(CodeItemRegion region)
 {
     return !region.IsPseudoGroup && region.StartLine > 0 && region.EndLine > 0;
 }