/// <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) { IStartRegion startRegionNode = tokenNode.GetContainingNode <IStartRegion>(true); if (startRegionNode != null) { if (startRegionNode.Parent is IBlock) { // we're in a block so remove the end and start region IEndRegion endRegionNode = startRegionNode.EndRegion; using (WriteLockCookie.Create(true)) { LowLevelModificationUtil.DeleteChild(endRegionNode); LowLevelModificationUtil.DeleteChild(startRegionNode); } } } } } if (currentNode.FirstChild != null) { this.DoNotPlaceRegionsWithinElements(currentNode.FirstChild); } } }
/// <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(IStartRegion 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(); IEndRegion endRegionNode = startRegionNode.EndRegion; IStartRegion newStartRegion = startRegionNode.Copy(null); ITokenNode firstNonWhitespaceAfterBracket = Utils.GetFirstNonWhitespaceTokenToRight(newLocationTokenNode); LowLevelModificationUtil.AddChildBefore(firstNonWhitespaceAfterBracket, new[] { newStartRegion }); newStartRegion.InsertNewLineAfter(); LowLevelModificationUtil.DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete); IStartRegion endOfTokensToFormat = newStartRegion; if (endRegionNode != null) { startOfTokensToDelete = Utils.GetFirstNewLineTokenToLeft(endRegionNode.NumberSign); endOfTokensToDelete = Utils.GetFirstNewLineTokenToRight(endRegionNode.NumberSign); IEndRegion newEndRegionNode = endRegionNode.Copy(null); ITokenNode newLineToken = Utils.GetFirstNonWhitespaceTokenToLeft(endRegionNode.NumberSign); LowLevelModificationUtil.AddChildBefore(newLineToken, new[] { newEndRegionNode }); newEndRegionNode.InsertNewLineAfter(); LowLevelModificationUtil.DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete); endOfTokensToFormat = (IStartRegion)newLineToken; } ////CSharpFormatterHelper.FormatterInstance.Format(startOfTokensToFormat, endOfTokensToFormat); ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)CSharpLanguage.Instance.LanguageService().CodeFormatter; codeFormatter.Format(startOfTokensToFormat, endOfTokensToFormat); } }
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) { IStartRegion startRegionNode = tokenNode.GetContainingNode <IStartRegion>(true); IEndRegion endRegion = startRegionNode.EndRegion; if (endRegion != null) { ITokenNode previousTokenNode = Utils.GetFirstNonWhitespaceTokenToLeft(endRegion.NumberSign); IStartRegion a = previousTokenNode.GetContainingNode <IStartRegion>(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); } } }