diff --git a/src/emitter.cpp b/src/emitter.cpp index 5168cba89..4afb20ef4 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -947,8 +947,12 @@ Emitter& Emitter::Write(const _Comment& comment) { PrepareNode(EmitterNodeType::NoType); - if (m_stream.col() > 0) + if (m_stream.col() == 0 && + m_pState->CurGroupNodeType() == EmitterNodeType::BlockMap) { + m_stream << IndentTo(m_pState->CurIndent()); + } else if (m_stream.col() > 0) { m_stream << Indentation(m_pState->GetPreCommentIndent()); + } Utils::WriteComment(m_stream, comment.content.data(), comment.content.size(), m_pState->GetPostCommentIndent()); diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index f1472d6f3..b13e09277 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -1822,5 +1822,16 @@ TEST_F(EmitterTest, ShowTrailingZero) { - .nan)"); } +TEST_F(EmitterTest, CommentInsideMapValueIsIndented) { + out << YAML::BeginMap << YAML::Key << "foo" << YAML::BeginMap + << YAML::Comment("Comment") << YAML::Key << "bar" << YAML::Value << true + << YAML::EndMap << YAML::EndMap; + + ExpectEmit( + "foo:\n" + " # Comment\n" + " bar: true"); +} + } // namespace } // namespace YAML