Skip to content

Commit 7b96dfb

Browse files
authored
[llvm][mustache] Align standalone partial indentation with spec (#159185)
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 68c3b53 commit 7b96dfb

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

@@ -439,7 +436,8 @@ class AddIndentationStringStream : public raw_ostream {
439436
public:
440437
explicit AddIndentationStringStream(llvm::raw_ostream &WrappedStream,
441438
size_t Indentation)
442-
: Indentation(Indentation), WrappedStream(WrappedStream) {
439+
: Indentation(Indentation), WrappedStream(WrappedStream),
440+
NeedsIndent(true) {
443441
SetUnbuffered();
444442
}
445443

@@ -448,10 +446,15 @@ class AddIndentationStringStream : public raw_ostream {
448446
llvm::StringRef Data(Ptr, Size);
449447
SmallString<0> Indent;
450448
Indent.resize(Indentation, ' ');
449+
451450
for (char C : Data) {
451+
if (NeedsIndent && C != '\n') {
452+
WrappedStream << Indent;
453+
NeedsIndent = false;
454+
}
452455
WrappedStream << C;
453456
if (C == '\n')
454-
WrappedStream << Indent;
457+
NeedsIndent = true;
455458
}
456459
}
457460

@@ -460,6 +463,7 @@ class AddIndentationStringStream : public raw_ostream {
460463
private:
461464
size_t Indentation;
462465
llvm::raw_ostream &WrappedStream;
466+
bool NeedsIndent;
463467
};
464468

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