public void MakeIndent(ITreeNode indentNode, string indent) { var lastSpace = AsWhitespaceNode(indentNode.PrevSibling); if (lastSpace != null && lastSpace.GetTokenType() != NewLineType) { var firstSpace = LeftWhitespaces(lastSpace).TakeWhile(ws => ws.GetTokenType() != NewLineType).LastOrDefault() ?? lastSpace; if (firstSpace != lastSpace || lastSpace.GetText() != indent) { if (indent.IsEmpty()) { LowLevelModificationUtil.DeleteChildRange(firstSpace, lastSpace); } else { LowLevelModificationUtil.ReplaceChildRange(firstSpace, lastSpace, CreateSpace(indent)); } } } else if (!indent.IsEmpty()) { LowLevelModificationUtil.AddChildBefore(indentNode, CreateSpace(indent)); } }
public override void Regenerate(IndependentInjectedNodeContext nodeContext) { var htmlAttributeValue = nodeContext.OriginalContextNode as IHtmlAttributeValue; if (htmlAttributeValue == null) { Logger.Fail("Original node is not IHtmlAttributeValue"); return; } if (htmlAttributeValue.LeadingQuote == null) { Logger.Fail("No leading quote"); return; } var buffer = nodeContext.GeneratedNode.GetTextAsBuffer(); var token = htmlAttributeValue.LeadingQuote.TokenTypes.ATTRIBUTE_VALUE.Create(buffer, TreeOffset.Zero, new TreeOffset(buffer.Length)) as IHtmlToken; var list = htmlAttributeValue.ValueElements.ToArray(); if (list.Any()) { LowLevelModificationUtil.DeleteChildRange(list.First(), list.Last()); } LowLevelModificationUtil.AddChildAfter(htmlAttributeValue.LeadingQuote, token); }
public static void MakeIndent(this ITreeNode indentNode, string indent) { var lastSpace = indentNode.PrevSibling as IWhitespaceNode; if (lastSpace != null && lastSpace.GetTokenType() != PsiTokenType.NEW_LINE) { ITreeNode firstSpace = lastSpace.LeftWhitespaces().TakeWhile(ws => ws != PsiTokenType.NEW_LINE).LastOrDefault() ?? lastSpace; while (firstSpace.GetTokenType() != PsiTokenType.NEW_LINE) { firstSpace = firstSpace.GetNextToken(); } firstSpace = firstSpace.GetNextToken(); if (firstSpace != lastSpace || lastSpace.GetText() != indent) { if (indent.IsEmpty()) { LowLevelModificationUtil.DeleteChildRange(firstSpace, lastSpace); } else { LowLevelModificationUtil.ReplaceChildRange(firstSpace, lastSpace, CreateSpace(indent)); } } } else if (!indent.IsEmpty()) { LowLevelModificationUtil.AddChildBefore(indentNode, CreateSpace(indent)); } }
/// <summary> /// Remove empty comments. /// </summary> /// <param name="node"> /// The node to process. /// </param> public static void RemoveEmptyComments(ITreeNode node) { // we don't remove empty lines from Element Doc Comments in here // the DeclarationHeader types take care of that. for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ICommentNode commentNode = currentNode as ICommentNode; if (commentNode != null && !(commentNode is IDocCommentNode)) { if (commentNode.CommentText.Trim() == string.Empty) { ITokenNode leftToken = Utils.GetFirstNewLineTokenToLeft((ITokenNode)currentNode); ITokenNode rightToken = Utils.GetFirstNewLineTokenToRight((ITokenNode)currentNode); if (leftToken == null) { leftToken = (ITokenNode)currentNode; } else { leftToken = leftToken.GetNextToken(); } if (rightToken == null) { rightToken = (ITokenNode)currentNode; } else { currentNode = rightToken.GetNextToken(); } using (WriteLockCookie.Create(true)) { LowLevelModificationUtil.DeleteChildRange(leftToken, rightToken); } } } } if (currentNode != null && currentNode.FirstChild != null) { RemoveEmptyComments(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); } }