/// <summary> /// Comments must be preceded by blank line. /// </summary> /// <param name="node"> /// The node. /// </param> private void CommentsMustBePreceededByBlankLine(ITreeNode node) { ITreeNode siblingMinus2; ITreeNode siblingMinus1; ITreeNode siblingMinus3; for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ICommentNode && !(currentNode is IDocCommentNode)) { if (Utils.IsFirstNodeOnLine(currentNode) && !(currentNode.Parent is ICSharpFile)) { siblingMinus1 = currentNode.PrevSibling; if (siblingMinus1 != null) { siblingMinus2 = siblingMinus1.PrevSibling; if (siblingMinus2 != null) { siblingMinus3 = siblingMinus2.PrevSibling; ITokenNode siblingMinus3Token = siblingMinus3 as ITokenNode; IWhitespaceNode siblingMinus2WhitespaceNode = siblingMinus2 as IWhitespaceNode; IWhitespaceNode siblingMinus3WhitespaceNode = siblingMinus3 as IWhitespaceNode; ICommentNode siblingMinus3CommentNode = siblingMinus3 as ICommentNode; if (siblingMinus3CommentNode != null) { // if the previous sibling is a comment then it doesn't need a new line. continue; } if (siblingMinus3Token != null && siblingMinus3Token.GetTokenType() == TokenType.LBRACE) { // if we're the start of a code block then don't insert a new line. continue; } if (siblingMinus2WhitespaceNode == null || siblingMinus3WhitespaceNode == null || !siblingMinus2WhitespaceNode.IsNewLine || !siblingMinus3WhitespaceNode.IsNewLine) { currentNode.InsertNewLineBefore(); ////CSharpFormatterHelper.FormatterInstance.Format(currentNode.Parent); ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)CSharpLanguage.Instance.LanguageService().CodeFormatter; codeFormatter.Format(currentNode.Parent); } } } } } if (currentNode.FirstChild != null) { this.CommentsMustBePreceededByBlankLine(currentNode.FirstChild); } } }
/// <summary> /// Remove line if previous tokens are new lines. /// </summary> /// <param name="tokenNode"> /// The token node. /// </param> private static void RemoveLineIfPreviousTokensAreNewLines(ITokenNode tokenNode) { // first prev token will be whitespace padding out the line ITokenNode prevToken1 = tokenNode.GetPrevToken(); ITokenNode prevToken2 = prevToken1.GetPrevToken(); ITokenNode prevToken3 = prevToken2.GetPrevToken(); IWhitespaceNode prevToken2WhiteSpaceNode = prevToken2 as IWhitespaceNode; IWhitespaceNode prevToken3WhiteSpaceNode = prevToken3 as IWhitespaceNode; if (prevToken2WhiteSpaceNode != null && prevToken2WhiteSpaceNode.IsNewLine && prevToken3WhiteSpaceNode != null && prevToken3WhiteSpaceNode.IsNewLine) { Utils.RemoveNewLineBefore(tokenNode); } }
private void restartLexer(ITreeNode child, uint state) { _myCurrentNode = null; _myCurrentCommentNode = null; while (child != null) { _myCurrentNode = child; _myCurrentCommentNode = child as IDocCommentNode; if (_myCurrentCommentNode != null) { //LeafElementBase leaf = (LeafElementBase)_myCurrentCommentNode; _myLexer = new XmlLexerGenerated(_myCurrentCommentNode.GetTextAsBuffer(), XmlTokenType); //_myLexer.Start(leaf.GetTreeStartOffset().Offset + 3, leaf.GetTreeStartOffset().Offset + leaf.GetTextLength(), state); _myLexer.Start(_commentStartLength, _myCurrentCommentNode.GetTextLength(), state); if (_myLexer.TokenType == null) { restartLexer(_myCurrentCommentNode.NextSibling, state); } return; } IWhitespaceNode whitespaceNode = child as IWhitespaceNode; if (whitespaceNode != null && whitespaceNode.IsNewLine) { _myLexer = new XmlLexerGenerated(new StringBuffer("\n"), XmlTokenType); //_myLexer.Start(leaf.GetTreeStartOffset().Offset + 3, leaf.GetTreeStartOffset().Offset + leaf.GetTextLength(), state); _myLexer.Start(0, 1, state); return; } child = child.NextSibling; } _myLexer = null; }
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 }); } } } } }
/// <summary> /// Determines if the <see cref="IElement"/> is a whitespace. /// </summary> /// <param name="element"> /// The <see cref="ITokenNode"/> value to check. /// </param> /// <returns> /// <c>true</c>if <paramref name="element"/> is a whitespace; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="element"/>is <c>null</c>. /// </exception> public static bool IsWhitespace(this ITreeNode element) { IWhitespaceNode whitespaceNode = element as IWhitespaceNode; return(whitespaceNode != null); }
/// <summary> /// Determines if the <see cref="IElement"/> is a whitespace new line. /// </summary> /// <param name="element"> /// The <see cref="ITokenNode"/> value to check. /// </param> /// <returns> /// <c>true</c>if <paramref name="element"/> is a New Line; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="element"/>is <c>null</c>. /// </exception> public static bool IsNewLine(this ITreeNode element) { IWhitespaceNode whitespaceNode = element as IWhitespaceNode; return(whitespaceNode != null && whitespaceNode.IsNewLine); }
/// <summary> /// Comments must be preceded by blank line. /// </summary> /// <param name="node"> /// The node. /// </param> private static void CommentsMustBePreceededByBlankLine(ITreeNode node) { for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ICommentNode && !(currentNode is IDocCommentNode)) { if (Utils.IsFirstNodeOnLine(currentNode) && !(currentNode.Parent is ICSharpFile)) { ITreeNode siblingMinus1 = currentNode.PrevSibling; if (siblingMinus1 != null) { ITreeNode siblingMinus2 = siblingMinus1.PrevSibling; if (siblingMinus2 != null) { ITreeNode siblingMinus3 = siblingMinus2.PrevSibling; ITokenNode siblingMinus3Token = siblingMinus3 as ITokenNode; IWhitespaceNode siblingMinus2WhitespaceNode = siblingMinus2 as IWhitespaceNode; IWhitespaceNode siblingMinus3WhitespaceNode = siblingMinus3 as IWhitespaceNode; ICommentNode siblingMinus3CommentNode = siblingMinus3 as ICommentNode; if (siblingMinus3CommentNode != null) { // if the previous sibling is a comment then it doesn't need a new line. continue; } if (siblingMinus3 is ISwitchLabelStatement) { //�if�we're�the�start�of�a�switch�block�then�don't�insert�a�new�line. continue; } if (siblingMinus3Token != null && siblingMinus3Token.GetTokenType() == CSharpTokenType.LBRACE) { // if we're the start of a code block then don't insert a new line. continue; } if (siblingMinus2WhitespaceNode == null || siblingMinus3WhitespaceNode == null || !siblingMinus2WhitespaceNode.IsNewLine || !(siblingMinus3WhitespaceNode.IsNewLine || siblingMinus3WhitespaceNode.IsWhitespace())) { currentNode.InsertNewLineBefore(); ////CSharpFormatterHelper.FormatterInstance.Format(currentNode.Parent); LanguageService languageService = CSharpLanguage.Instance.LanguageService(); if (languageService != null) { ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)languageService.CodeFormatter; if (codeFormatter != null) { codeFormatter.Format(currentNode.Parent); } } } } } } } if (currentNode.FirstChild != null) { CommentsMustBePreceededByBlankLine(currentNode.FirstChild); } } }