Skip to content

Commit c58ac89

Browse files
committed
[llvm][mustache] Align standalone partial indentation with spec
The current implementation 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 implementation to prepend the indentation to the first line of the partial.
1 parent 8553bd2 commit c58ac89

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

llvm/lib/Support/Mustache.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,15 @@ void stripTokenAhead(SmallVectorImpl<Token> &Tokens, size_t Idx) {
282282
// For example:
283283
// The template string
284284
// " \t{{#section}}A{{/section}}"
285-
// would be considered as having no text ahead and would be render as
285+
// would be considered as having no text ahead and would be render as:
286286
// "A"
287-
// The exception for this is partial tag which requires us to
288-
// keep track of the indentation once it's rendered.
289287
void stripTokenBefore(SmallVectorImpl<Token> &Tokens, size_t Idx,
290288
Token &CurrentToken, Token::Type CurrentType) {
291289
Token &PrevToken = Tokens[Idx - 1];
292290
StringRef PrevTokenBody = PrevToken.TokenBody;
293291
StringRef Unindented = PrevTokenBody.rtrim(" \r\t\v");
294292
size_t Indentation = PrevTokenBody.size() - Unindented.size();
295-
if (CurrentType != Token::Type::Partial)
296-
PrevToken.TokenBody = Unindented.str();
293+
PrevToken.TokenBody = Unindented.str();
297294
CurrentToken.setIndentation(Indentation);
298295
}
299296

@@ -425,7 +422,8 @@ class AddIndentationStringStream : public raw_ostream {
425422
public:
426423
explicit AddIndentationStringStream(llvm::raw_ostream &WrappedStream,
427424
size_t Indentation)
428-
: Indentation(Indentation), WrappedStream(WrappedStream) {
425+
: Indentation(Indentation), WrappedStream(WrappedStream),
426+
NeedsIndent(true) {
429427
SetUnbuffered();
430428
}
431429

@@ -434,10 +432,15 @@ class AddIndentationStringStream : public raw_ostream {
434432
llvm::StringRef Data(Ptr, Size);
435433
SmallString<0> Indent;
436434
Indent.resize(Indentation, ' ');
435+
437436
for (char C : Data) {
437+
if (NeedsIndent && C != '\n') {
438+
WrappedStream << Indent;
439+
NeedsIndent = false;
440+
}
438441
WrappedStream << C;
439442
if (C == '\n')
440-
WrappedStream << Indent;
443+
NeedsIndent = true;
441444
}
442445
}
443446

@@ -446,6 +449,7 @@ class AddIndentationStringStream : public raw_ostream {
446449
private:
447450
size_t Indentation;
448451
llvm::raw_ostream &WrappedStream;
452+
bool NeedsIndent;
449453
};
450454

451455
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)