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)); } }
private void ChangeDocumentation(string text) { text = text.Trim('\r', '\n'); if (!string.IsNullOrEmpty(text)) { var newNode = GetElementFactory().CreateDocCommentBlock(text); if (IsCreated) { LowLevelModificationUtil.ReplaceChildRange(Node, Node, newNode); } else { LowLevelModificationUtil.AddChildBefore(AnalyzeUnit.Node.FirstChild, newNode); } newNode.FormatNode(); Node = newNode; } else if (Node != null) { LowLevelModificationUtil.DeleteChild(Node); Node = null; } Update(); }
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)); } }
protected virtual void DoPutNewIndents([NotNull] IFormalParameterList parameters) { LowLevelModificationUtil.AddChildBefore(parameters, CreateLineBreakToken()); foreach (var paramDecl in parameters.ParameterDeclarationsEnumerable.Skip(1)) { LowLevelModificationUtil.AddChildBefore(paramDecl, CreateLineBreakToken()); } }
/// <summary> /// Inserts a newline in front of the Node provided. /// </summary> /// <param name="currentNode"> /// The node to insert in front of. /// </param> /// <returns> /// The inserted ITreeNode. /// </returns> public static ITreeNode InsertNewLineBefore(this ITreeNode currentNode) { LeafElementBase leafElement = GetLeafElement(); using (WriteLockCookie.Create(true)) { LowLevelModificationUtil.AddChildBefore(currentNode, new[] { leafElement }); } return(leafElement); }
/// <summary> /// Commas must be spaced correctly. /// </summary> /// <param name="node"> /// The node to use. /// </param> public void EqualsMustBeSpacedCorrectly(ITreeNode node) { List <TokenNodeType> tokensThatCanBeLeftSideOfEquals = new List <TokenNodeType> { CSharpTokenType.WHITE_SPACE, CSharpTokenType.NE, CSharpTokenType.LT, CSharpTokenType.GT }; const string WhiteSpace = " "; for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.EQ) { ITokenNode nextToken = tokenNode.GetNextToken(); ITokenNode previousToken = tokenNode.GetPrevToken(); if (!nextToken.IsWhitespace()) { using (WriteLockCookie.Create(true)) { // insert a space LeafElementBase leafElement = TreeElementFactory.CreateLeafElement( CSharpTokenType.WHITE_SPACE, new JetBrains.Text.StringBuffer(WhiteSpace), 0, WhiteSpace.Length); LowLevelModificationUtil.AddChildBefore(nextToken, new ITreeNode[] { leafElement }); } } if (!tokensThatCanBeLeftSideOfEquals.Contains(previousToken.GetTokenType())) { using (WriteLockCookie.Create(true)) { // insert a space LeafElementBase leafElement = TreeElementFactory.CreateLeafElement( CSharpTokenType.WHITE_SPACE, new JetBrains.Text.StringBuffer(WhiteSpace), 0, WhiteSpace.Length); LowLevelModificationUtil.AddChildBefore(tokenNode, new ITreeNode[] { leafElement }); } } } } if (currentNode.FirstChild != null) { this.EqualsMustBeSpacedCorrectly(currentNode.FirstChild); } } }
public void AdjustLeadingAndTrailingWhitespaces(CppCodeFormatter cppCodeFormatter, CppFile cppFile) { var cgProgram = (cppFile.Parent as IInjectedFileHolder)?.OriginalNode.PrevSibling; var s = ShaderLabCppFormatterExtension.GetIndentInCgProgram(cgProgram); cppCodeFormatter.RemoveLeadingSpacesInFile(cppFile); cppCodeFormatter.RemoveTrailingSpaces(cppFile); var lineEnding = cppFile.DetectLineEnding(cppFile.GetPsiServices()); LowLevelModificationUtil.AddChildBefore(cppFile.firstChild, cppCodeFormatter.CreateNewLine(lineEnding), cppCodeFormatter.CreateSpace(s, null)); LowLevelModificationUtil.AddChildAfter(cppFile.lastChild, cppCodeFormatter.CreateNewLine(lineEnding), cppCodeFormatter.CreateSpace(s, null)); }
/// <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); } }
protected override ITreeNode Move(Direction direction) { using (PsiTransactionCookie.CreateAutoCommitCookieWithCachesUpdate(myRuleDeclaration.GetPsiServices(), "Rearrange code")) { if (direction == Direction.Up) { var sibling = myRuleDeclaration.PrevSibling; while (sibling is IWhitespaceNode) { sibling = sibling.PrevSibling; } var ruleDeclaration = sibling as IRuleDeclaration; if (ruleDeclaration != null) { using (WriteLockCookie.Create()) { LowLevelModificationUtil.AddChildBefore(ruleDeclaration, myRuleDeclaration); LowLevelModificationUtil.AddChildBefore(ruleDeclaration, new NewLine("\r\n")); } } } if (direction == Direction.Down) { var sibling = myRuleDeclaration.NextSibling; while (sibling is IWhitespaceNode) { sibling = sibling.NextSibling; } var ruleDeclaration = sibling as IRuleDeclaration; if (ruleDeclaration != null) { using (WriteLockCookie.Create()) { LowLevelModificationUtil.AddChildAfter(ruleDeclaration, myRuleDeclaration); LowLevelModificationUtil.AddChildAfter(ruleDeclaration, new NewLine("\r\n")); } } } } return(myRuleDeclaration); }
/// <summary> /// Commas must be spaced correctly. /// </summary> /// <param name="node"> /// The node to use. /// </param> public static void CommasMustBeSpacedCorrectly(ITreeNode node) { List <TokenNodeType> tokensThatCanBeRightSideOfComma = new List <TokenNodeType> { CSharpTokenType.NEW_LINE, CSharpTokenType.WHITE_SPACE, CSharpTokenType.RBRACKET, CSharpTokenType.GT, CSharpTokenType.COMMA, CSharpTokenType.RPARENTH }; const string WhiteSpace = " "; for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.COMMA) { ITokenNode nextToken = tokenNode.GetNextToken(); if (!tokensThatCanBeRightSideOfComma.Contains(nextToken.GetTokenType())) { using (WriteLockCookie.Create(true)) { // insert a space LeafElementBase leafElement = TreeElementFactory.CreateLeafElement( CSharpTokenType.WHITE_SPACE, new JetBrains.Text.StringBuffer(WhiteSpace), 0, WhiteSpace.Length); LowLevelModificationUtil.AddChildBefore(nextToken, new ITreeNode[] { leafElement }); } } } } if (currentNode.FirstChild != null) { CommasMustBeSpacedCorrectly(currentNode.FirstChild); } } }
private static void SwapFileHeaderNode(ICSharpFile file, string newHeader) { ITreeRange existingHeaderRange = Utils.GetFileHeaderTreeRange(file); using (WriteLockCookie.Create(file.IsPhysical())) { ICommentNode newCommentNode; if (existingHeaderRange.IsEmpty) { // existing header missing so add on a new line for our new header newHeader += Environment.NewLine; IWhitespaceNode node = file.FirstChild as IWhitespaceNode; bool insertNewLine = true; while (node != null) { if (node.IsNewLine) { insertNewLine = false; break; } node = node.NextSibling as IWhitespaceNode; } if (insertNewLine) { newHeader += Environment.NewLine; } newCommentNode = (ICommentNode) CSharpTokenType.END_OF_LINE_COMMENT.Create(new JB::JetBrains.Text.StringBuffer(newHeader), new TreeOffset(0), new TreeOffset(newHeader.Length)); LowLevelModificationUtil.AddChildBefore(file.FirstChild, new ITreeNode[] { newCommentNode }); } else { ITokenNode lastToken = (ITokenNode)existingHeaderRange.Last; ITokenNode nextToken = lastToken.GetNextToken(); if (nextToken != null) { ITokenNode nextNextToken = nextToken.GetNextToken(); if (nextNextToken != null) { ITokenNode nextNextNextToken = nextNextToken.GetNextToken(); if (!nextToken.IsNewLine() || !nextNextToken.IsNewLine()) { newHeader += Environment.NewLine; } if (nextNextNextToken.GetTokenType() == CSharpTokenType.PP_SHARP && nextToken.IsNewLine() && nextNextToken.IsNewLine()) { newHeader += Environment.NewLine; } newCommentNode = (ICommentNode) CSharpTokenType.END_OF_LINE_COMMENT.Create( new JB::JetBrains.Text.StringBuffer(newHeader), new TreeOffset(0), new TreeOffset(newHeader.Length)); LowLevelModificationUtil.ReplaceChildRange(existingHeaderRange.First, existingHeaderRange.Last, new ITreeNode[] { newCommentNode }); } } } } }