Skip to content

Commit c4b1cba

Browse files
committed
[llvm][mustache] Align standalone partial indentation with spec
The current implementaion did not correctly handle indentation for standalone partial tags. It was only applied to lines following a newline, instead of the first line of a partial's content. This was fixed by updating the AddIndentation implementaion to prepend the indentation to the first line of the partial.
1 parent 8553bd2 commit c4b1cba

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

llvm/lib/Support/Mustache.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ void stripTokenBefore(SmallVectorImpl<Token> &Tokens, size_t Idx,
292292
StringRef PrevTokenBody = PrevToken.TokenBody;
293293
StringRef Unindented = PrevTokenBody.rtrim(" \r\t\v");
294294
size_t Indentation = PrevTokenBody.size() - Unindented.size();
295-
if (CurrentType != Token::Type::Partial)
296-
PrevToken.TokenBody = Unindented.str();
295+
PrevToken.TokenBody = Unindented.str();
297296
CurrentToken.setIndentation(Indentation);
298297
}
299298

@@ -425,7 +424,8 @@ class AddIndentationStringStream : public raw_ostream {
425424
public:
426425
explicit AddIndentationStringStream(llvm::raw_ostream &WrappedStream,
427426
size_t Indentation)
428-
: Indentation(Indentation), WrappedStream(WrappedStream) {
427+
: Indentation(Indentation), WrappedStream(WrappedStream),
428+
NeedsIndent(true) {
429429
SetUnbuffered();
430430
}
431431

@@ -434,10 +434,15 @@ class AddIndentationStringStream : public raw_ostream {
434434
llvm::StringRef Data(Ptr, Size);
435435
SmallString<0> Indent;
436436
Indent.resize(Indentation, ' ');
437+
437438
for (char C : Data) {
439+
if (NeedsIndent && C != '\n') {
440+
WrappedStream << Indent;
441+
NeedsIndent = false;
442+
}
438443
WrappedStream << C;
439444
if (C == '\n')
440-
WrappedStream << Indent;
445+
NeedsIndent = true;
441446
}
442447
}
443448

@@ -446,6 +451,7 @@ class AddIndentationStringStream : public raw_ostream {
446451
private:
447452
size_t Indentation;
448453
llvm::raw_ostream &WrappedStream;
454+
bool NeedsIndent;
449455
};
450456

451457
class Parser {

llvm/unittests/Support/MustacheTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ TEST(MustachePartials, StandaloneIndentation) {
998998
std::string Out;
999999
raw_string_ostream OS(Out);
10001000
T.render(D, OS);
1001-
EXPECT_NE("\\\n |\n <\n ->\n |\n/\n", Out);
1001+
EXPECT_EQ("\\\n |\n <\n ->\n |\n/\n", Out);
10021002
}
10031003

10041004
TEST(MustacheLambdas, BasicInterpolation) {

0 commit comments

Comments
 (0)