void Align(AstNode lPar, AstNode alignNode, bool space)
        {
            int extraSpaces    = 0;
            var useExtraSpaces = lPar.StartLocation.Line == alignNode.StartLocation.Line;

            if (useExtraSpaces)
            {
                extraSpaces            = Math.Max(0, lPar.StartLocation.Column + (space ? 1 : 0) - curIndent.IndentString.Length);
                curIndent.ExtraSpaces += extraSpaces;
                ForceSpacesAfter(lPar, space);
            }
            else
            {
                curIndent.Push(IndentType.Continuation);
                FixIndentation(alignNode);
            }
            alignNode.AcceptVisitor(this);

            if (useExtraSpaces)
            {
                curIndent.ExtraSpaces -= extraSpaces;
            }
            else
            {
                curIndent.Pop();
            }
        }
 public void Apply(AstNode rootNode)
 {
     if (rootNode == null)
     {
         throw new ArgumentNullException("rootNode");
     }
     rootNode.AcceptVisitor(new GenerateCodeVisitior(this));
 }
Пример #3
0
        /// <summary>
        /// Applies the <paramref name="visitor"/> to all nodes in this collection.
        /// </summary>
        public void AcceptVisitor(IAstVisitor visitor)
        {
            uint    roleIndex = role.Index;
            AstNode next;

            for (AstNode cur = node.FirstChild; cur != null; cur = next)
            {
                Debug.Assert(cur.Parent == node);
                // Remember next before yielding cur.
                // This allows removing/replacing nodes while iterating through the list.
                next = cur.NextSibling;
                if (cur.RoleIndex == roleIndex)
                {
                    cur.AcceptVisitor(visitor);
                }
            }
        }
        void FixEmbeddedStatment(BraceStyle braceStyle, AlTokenNode token, bool allowInLine, AstNode node, bool statementAlreadyIndented = false)
        {
            if (node == null)
            {
                return;
            }
            bool isBlock = node is BlockStatement;

            FormattingChanges.TextReplaceAction beginBraceAction = null;
            FormattingChanges.TextReplaceAction endBraceAction   = null;
            BlockStatement closeBlockToBeFixed = null;

            if (isBlock)
            {
                BlockStatement block = node as BlockStatement;
                if (allowInLine && block.StartLocation.Line == block.EndLocation.Line && block.Statements.Count() <= 1)
                {
                    if (block.Statements.Count() == 1)
                    {
                        nextStatementIndent = " ";
                    }
                }
                else
                {
                    if (!statementAlreadyIndented)
                    {
                        FixOpenBrace(braceStyle, block.LBraceToken);
                    }
                    closeBlockToBeFixed = block;
                }

                if (braceStyle == BraceStyle.NextLineShifted2)
                {
                    curIndent.Push(IndentType.Block);
                }
            }
            else
            {
                if (allowInLine && token.StartLocation.Line == node.EndLocation.Line)
                {
                    nextStatementIndent = " ";
                }
            }
            bool pushed = false;

            if (policy.IndentBlocks && !(
                    policy.AlignEmbeddedStatements && node is IfElseStatement && node.Parent is IfElseStatement ||
                    policy.AlignEmbeddedStatements && node is UsingStatement && node.Parent is UsingStatement ||
                    policy.AlignEmbeddedStatements && node is LockStatement && node.Parent is LockStatement))
            {
                curIndent.Push(IndentType.Block);
                pushed = true;
            }
            if (isBlock)
            {
                VisitBlockWithoutFixingBraces((BlockStatement)node, false);
            }
            else
            {
                if (!statementAlreadyIndented)
                {
                    PlaceOnNewLine(policy.EmbeddedStatementPlacement, node);
                    nextStatementIndent = null;
                }
                node.AcceptVisitor(this);
            }
            nextStatementIndent = null;
            if (pushed)
            {
                curIndent.Pop();
            }
            if (beginBraceAction != null && endBraceAction != null)
            {
                beginBraceAction.DependsOn = endBraceAction;
                endBraceAction.DependsOn   = beginBraceAction;
            }

            if (isBlock && braceStyle == BraceStyle.NextLineShifted2)
            {
                curIndent.Pop();
            }
            if (closeBlockToBeFixed != null)
            {
                FixClosingBrace(braceStyle, closeBlockToBeFixed.RBraceToken);
            }
        }