diff options
author | Paul Kirth <paulkirth@google.com> | 2025-08-29 23:26:29 -0700 |
---|---|---|
committer | Paul Kirth <paulkirth@google.com> | 2025-09-25 18:52:03 -0700 |
commit | c4b1cbae2b2fda887b46bce0b561d2f3b497228d (patch) | |
tree | 7d0f6c3a89e3b889e99e937f54357070ec13eeff | |
parent | 8553bd2b29ad2b17a9a884f14da6c43b606ec776 (diff) | |
download | llvm-users/ilovepi/mustache-indent-impl.zip llvm-users/ilovepi/mustache-indent-impl.tar.gz llvm-users/ilovepi/mustache-indent-impl.tar.bz2 |
[llvm][mustache] Align standalone partial indentation with specusers/ilovepi/mustache-indent-impl
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.
-rw-r--r-- | llvm/lib/Support/Mustache.cpp | 14 | ||||
-rw-r--r-- | llvm/unittests/Support/MustacheTest.cpp | 2 |
2 files changed, 11 insertions, 5 deletions
diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp index be9cbfd..9c71d6a 100644 --- a/llvm/lib/Support/Mustache.cpp +++ b/llvm/lib/Support/Mustache.cpp @@ -292,8 +292,7 @@ void stripTokenBefore(SmallVectorImpl<Token> &Tokens, size_t Idx, StringRef PrevTokenBody = PrevToken.TokenBody; StringRef Unindented = PrevTokenBody.rtrim(" \r\t\v"); size_t Indentation = PrevTokenBody.size() - Unindented.size(); - if (CurrentType != Token::Type::Partial) - PrevToken.TokenBody = Unindented.str(); + PrevToken.TokenBody = Unindented.str(); CurrentToken.setIndentation(Indentation); } @@ -425,7 +424,8 @@ class AddIndentationStringStream : public raw_ostream { public: explicit AddIndentationStringStream(llvm::raw_ostream &WrappedStream, size_t Indentation) - : Indentation(Indentation), WrappedStream(WrappedStream) { + : Indentation(Indentation), WrappedStream(WrappedStream), + NeedsIndent(true) { SetUnbuffered(); } @@ -434,10 +434,15 @@ protected: llvm::StringRef Data(Ptr, Size); SmallString<0> Indent; Indent.resize(Indentation, ' '); + for (char C : Data) { + if (NeedsIndent && C != '\n') { + WrappedStream << Indent; + NeedsIndent = false; + } WrappedStream << C; if (C == '\n') - WrappedStream << Indent; + NeedsIndent = true; } } @@ -446,6 +451,7 @@ protected: private: size_t Indentation; llvm::raw_ostream &WrappedStream; + bool NeedsIndent; }; class Parser { diff --git a/llvm/unittests/Support/MustacheTest.cpp b/llvm/unittests/Support/MustacheTest.cpp index 02eaed4..3635463 100644 --- a/llvm/unittests/Support/MustacheTest.cpp +++ b/llvm/unittests/Support/MustacheTest.cpp @@ -998,7 +998,7 @@ TEST(MustachePartials, StandaloneIndentation) { std::string Out; raw_string_ostream OS(Out); T.render(D, OS); - EXPECT_NE("\\\n |\n <\n ->\n |\n/\n", Out); + EXPECT_EQ("\\\n |\n <\n ->\n |\n/\n", Out); } TEST(MustacheLambdas, BasicInterpolation) { |