Example #1
0
        /// <summary>
        /// Curly brackets for multi line statements must not share line.
        /// </summary>
        /// <param name="node">
        /// The node.
        /// </param>
        public void CurlyBracketsForMultiLineStatementsMustNotShareLine(ITreeNode node)
        {
            int offsetColumn = 0;

            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ITokenNode)
                {
                    ITokenNode tokenNode = currentNode as ITokenNode;
                    if (tokenNode.GetTokenType() == CSharpTokenType.LBRACE)
                    {
                        // check to see if this LBRACE { is on a line with another non whitespace token
                        if (tokenNode.Parent is ICreationExpressionInitializer)
                        {
                            ICreationExpressionInitializer creationExpressionInitializerNode = tokenNode.Parent as ICreationExpressionInitializer;
                            if (creationExpressionInitializerNode != null)
                            {
                                ITokenNode          leftBrace              = creationExpressionInitializerNode.LBrace;
                                ITokenNode          rightBrace             = creationExpressionInitializerNode.RBrace;
                                ICreationExpression creationExpressionNode = tokenNode.GetContainingNode <ICreationExpression>(true);

                                if (creationExpressionNode != null)
                                {
                                    ITreeNode first = creationExpressionNode.FirstChild;
                                    ITreeNode last  = creationExpressionNode.LastChild;

                                    if (Utils.HasLineBreakBetween(first, last))
                                    {
                                        if (Utils.TokenHasNonWhitespaceTokenToRightOnSameLine(leftBrace))
                                        {
                                            // We'll be 4-1 honest.
                                            offsetColumn = Utils.GetOffsetToStartOfLine(leftBrace);
                                            ITreeNode newLine = leftBrace.InsertNewLineAfter();

                                            Utils.InsertWhitespaceAfter(newLine, offsetColumn + 3);
                                        }

                                        if (rightBrace != null)
                                        {
                                            // check to see if this RBRACE { is on a line with another non whitespace token
                                            if (Utils.TokenHasNonWhitespaceTokenToLeftOnSameLine(rightBrace))
                                            {
                                                ITreeNode newLine = rightBrace.InsertNewLineBefore();
                                                Utils.InsertWhitespaceAfter(newLine, offsetColumn);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    this.CurlyBracketsForMultiLineStatementsMustNotShareLine(currentNode.FirstChild);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Moves the comment token specified after the next available non whitespace char (normally an open curly bracket).
        /// </summary>
        /// <param name="commentTokenNode">
        /// The comment token to move.
        /// </param>
        public static void MoveCommentInsideNextOpenCurlyBracket(ITokenNode commentTokenNode)
        {
            using (WriteLockCookie.Create(true))
            {
                // move comment inside block curly bracket here
                // we copy it, then insert it and then delete the copied one
                ITokenNode startOfTokensToDelete = Utils.GetFirstNonWhitespaceTokenToLeft(commentTokenNode).GetNextToken();
                ITokenNode endOfTokensToDelete   = Utils.GetFirstNewLineTokenToRight(commentTokenNode);
                ITokenNode startOfTokensToFormat = startOfTokensToDelete.GetPrevToken();

                ITokenNode openCurlyBracketTokenNode = Utils.GetFirstNonWhitespaceTokenToRight(commentTokenNode);
                ITokenNode newCommentTokenNode       = commentTokenNode.Copy(null);
                ITokenNode tokenNodeToInsertAfter    = Utils.GetFirstNewLineTokenToRight(openCurlyBracketTokenNode);
                LowLevelModificationUtil.AddChildAfter(tokenNodeToInsertAfter, new[] { newCommentTokenNode });
                LowLevelModificationUtil.AddChildAfter(newCommentTokenNode, newCommentTokenNode.InsertNewLineAfter());

                DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete);
                ITokenNode endOfTokensToFormat = newCommentTokenNode;

                CSharpFormatterHelper.FormatterInstance.Format(startOfTokensToFormat, endOfTokensToFormat);
            }
        }