示例#1
0
        /// <summary>
        /// Do not place regions within elements.
        /// </summary>
        /// <param name="node">
        /// The node to process.
        /// </param>
        private void DoNotPlaceRegionsWithinElements(ITreeNode node)
        {
            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ITokenNode)
                {
                    ITokenNode tokenNode = currentNode as ITokenNode;

                    if (tokenNode.GetTokenType() == CSharpTokenType.PP_START_REGION)
                    {
                        IStartRegionNode startRegionNode = tokenNode.GetContainingElement <IStartRegionNode>(true);
                        if (startRegionNode != null)
                        {
                            if (startRegionNode.Parent is IBlock)
                            {
                                // we're in a block so remove the end and start region
                                IEndRegionNode endRegionNode = startRegionNode.EndRegion;

                                using (WriteLockCookie.Create(true))
                                {
                                    LowLevelModificationUtil.DeleteChild(endRegionNode);
                                    LowLevelModificationUtil.DeleteChild(startRegionNode);
                                }
                            }
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    this.DoNotPlaceRegionsWithinElements(currentNode.FirstChild);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Moves the IStartRegion specified inside the next open curly bracket and moves the corresponding end region inside too.
        /// </summary>
        /// <param name="startRegionNode">
        /// The node to move.
        /// </param>
        public static void MoveRegionInsideNextOpenCurlyBracket(IStartRegionNode startRegionNode)
        {
            using (WriteLockCookie.Create(true))
            {
                ITokenNode newLocationTokenNode = Utils.GetFirstNonWhitespaceTokenToRight(startRegionNode.Message);

                // if its a start region there is probably a corresponding end region
                // find it, and move it inside the block
                // find the position to delete from
                ITokenNode startOfTokensToDelete = Utils.GetFirstNewLineTokenToLeft(startRegionNode.NumberSign);
                ITokenNode endOfTokensToDelete   = Utils.GetFirstNewLineTokenToRight(startRegionNode.Message);
                ITokenNode startOfTokensToFormat = startOfTokensToDelete.GetPrevToken();

                IEndRegionNode   endRegionNode                  = startRegionNode.EndRegion;
                IStartRegionNode newStartRegionNode             = startRegionNode.CopyElement(null);
                ITokenNode       firstNonWhitespaceAfterBracket = Utils.GetFirstNonWhitespaceTokenToRight(newLocationTokenNode);

                LowLevelModificationUtil.AddChildBefore(firstNonWhitespaceAfterBracket, new[] { newStartRegionNode });

                newStartRegionNode.ToTreeNode().InsertNewLineAfter();

                LowLevelModificationUtil.DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete);
                IElement endOfTokensToFormat = (IElement)newStartRegionNode;

                if (endRegionNode != null)
                {
                    startOfTokensToDelete = Utils.GetFirstNewLineTokenToLeft(endRegionNode.NumberSign);
                    endOfTokensToDelete   = Utils.GetFirstNewLineTokenToRight(endRegionNode.NumberSign);

                    IEndRegionNode newEndRegionNode = endRegionNode.CopyElement(null);
                    ITokenNode     newLineToken     = Utils.GetFirstNonWhitespaceTokenToLeft(endRegionNode.NumberSign);
                    LowLevelModificationUtil.AddChildBefore(newLineToken, new[] { newEndRegionNode });

                    newEndRegionNode.InsertNewLineAfter();

                    LowLevelModificationUtil.DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete);
                    endOfTokensToFormat = newLineToken;
                }

                CSharpFormatterHelper.FormatterInstance.Format(startOfTokensToFormat, endOfTokensToFormat);
            }
        }
示例#3
0
        private void CodeMustNotContainEmptyRegions(ITreeNode node)
        {
            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ITokenNode)
                {
                    ITokenNode tokenNode = currentNode as ITokenNode;

                    if (tokenNode.GetTokenType() == CSharpTokenType.PP_START_REGION)
                    {
                        IStartRegionNode startRegionNode = tokenNode.GetContainingElement <IStartRegionNode>(true);

                        IEndRegionNode endRegion = startRegionNode.EndRegion;

                        if (endRegion != null)
                        {
                            ITokenNode previousTokenNode = Utils.GetFirstNonWhitespaceTokenToLeft(endRegion.NumberSign);

                            IStartRegionNode a = previousTokenNode.GetContainingElement <IStartRegionNode>(true);

                            if (a != null && a.Equals(startRegionNode))
                            {
                                using (WriteLockCookie.Create(true))
                                {
                                    ModificationUtil.DeleteChild(startRegionNode);
                                    ModificationUtil.DeleteChild(endRegion);
                                }
                            }
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    this.CodeMustNotContainEmptyRegions(currentNode.FirstChild);
                }
            }
        }