private void PushDocument()

in src/Bicep.Core/PrettyPrint/DocumentBuildVisitor.cs [861:942]


        private void PushDocument(ILinkedDocument document)
        {
            if (this.documentStack.Count == 0)
            {
                this.documentStack.Push(document);

                return;
            }

            ILinkedDocument top = this.documentStack.Peek();

            if (document == Line)
            {
                if (top == NoLine || top == SingleLine || top == DoubleLine)
                {
                    // No newlines are allowed after a NoLine / SingleLine / DoubleLine.
                    return;
                }

                if (top == Line)
                {
                    // Two Lines form a DoubleLine that prevents more newlines from being added.
                    this.documentStack.Pop();
                    this.documentStack.Push(DoubleLine);
                }
                else
                {
                    this.documentStack.Push(Line);
                }
            }
            else if (document == NoLine)
            {
                if (top == Line || top == DoubleLine)
                {
                    // No newlines are allowed before a NoLine.
                    this.documentStack.Pop();
                }

                this.documentStack.Push(document);
            }
            else if (document == SingleLine)
            {
                if (top == SingleLine)
                {
                    // When two SingleLines meet, the current block is empty. Remove all newlines inside the block.
                    this.documentStack.Pop();
                    return;
                }

                if (top == Line || top == DoubleLine)
                {
                    // No newlines are allowed before a SingleLine.
                    this.documentStack.Pop();
                }

                this.documentStack.Push(document);
            }
            else if (this.visitingComment && this.visitingLeadingTrivia)
            {
                if (this.LeadingDirectiveOrComments is null)
                {
                    this.LeadingDirectiveOrComments = document;
                }
                else
                {
                    this.LeadingDirectiveOrComments = Concat(this.LeadingDirectiveOrComments, Space, document);
                }
            }
            else if (visitingComment)
            {
                // Add a space before the comment if it's not at the beginning of the file or after a newline.
                ILinkedDocument gap = top != NoLine && top != Line && top != SingleLine && top != DoubleLine ? Space : Nil;

                // Combine the comment and the document at the top of the stack. This is the key to simplify VisitToken.

                this.documentStack.Push(Concat(this.documentStack.Pop(), gap, document));
            }
            else
            {
                this.documentStack.Push(document);
            }
        }