Skip to content

Commit 903069c

Browse files
Brock Densonmdenson
authored andcommitted
[clang-doc] fix mustache template whitespace
1 parent e317c7e commit 903069c

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

llvm/lib/Support/Mustache.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
684735
void 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

690741
void ASTNode::renderLambdas(const json::Value &Contexts, llvm::raw_ostream &OS,

0 commit comments

Comments
 (0)