@@ -166,6 +166,10 @@ class ASTNode {
166166 void renderSectionLambdas (const llvm::json::Value &Contexts,
167167 llvm::raw_ostream &OS, SectionLambda &L);
168168
169+ void indentTextNode (std::string &body, size_t Indentation, bool lastChild);
170+
171+ void indentNodes (ASTNode *Node, bool isPartial);
172+
169173 void renderPartial (const llvm::json::Value &Contexts, llvm::raw_ostream &OS,
170174 ASTNode *Partial);
171175
@@ -681,10 +685,57 @@ void ASTNode::renderChild(const json::Value &Contexts, llvm::raw_ostream &OS) {
681685 Child->render (Contexts, OS);
682686}
683687
688+ void ASTNode::indentTextNode (std::string &body, size_t Indentation,
689+ bool lastChild) {
690+ std::string spaces (Indentation, ' ' );
691+ size_t pos = 0 ;
692+
693+ if (lastChild)
694+ body.erase (body.find_last_not_of (" \t\r\f\v " ) + 1 ); // .rtrim??
695+
696+ while ((pos = body.find (' \n ' , pos)) != std::string::npos) {
697+ if ((!lastChild) || (pos != body.size () - 1 )) {
698+ body.insert (pos + 1 , spaces);
699+ pos += 1 + Indentation;
700+ } else {
701+ break ;
702+ }
703+ }
704+ }
705+
706+ void ASTNode::indentNodes (ASTNode *Node, bool isPartial) {
707+ size_t size = Node->Children .size ();
708+
709+ for (size_t i = 0 ; i < size; ++i) {
710+ ASTNode *child = Node->Children [i].get ();
711+ switch (child->Ty ) {
712+ case ASTNode::Text: {
713+ indentTextNode (child->Body , Indentation, ((i == size - 1 ) && isPartial));
714+ break ;
715+ }
716+ case ASTNode::Section: {
717+ indentNodes (child, false );
718+ break ;
719+ }
720+ case ASTNode::Partial: {
721+ indentNodes (child, true );
722+ }
723+ case ASTNode::Root:
724+ case ASTNode::Variable:
725+ case ASTNode::UnescapeVariable:
726+ case ASTNode::InvertSection:
727+ break ;
728+ default :
729+ llvm::outs () << " Invalid ASTNode type\n " ;
730+ break ;
731+ }
732+ }
733+ }
734+
684735void ASTNode::renderPartial (const json::Value &Contexts, llvm::raw_ostream &OS,
685736 ASTNode *Partial) {
686- AddIndentationStringStream IS (OS, Indentation );
687- Partial->render (Contexts, IS );
737+ indentNodes (Partial, true );
738+ Partial->render (Contexts, OS );
688739}
689740
690741void ASTNode::renderLambdas (const json::Value &Contexts, llvm::raw_ostream &OS,
0 commit comments