From 8069ce95da92474ce66911451def0a7b555615e2 Mon Sep 17 00:00:00 2001 From: alexprabhat99 Date: Mon, 30 Mar 2026 15:54:02 +0530 Subject: [PATCH 1/2] apply indentation before emitting comments on new lines --- src/emitter.cpp | 4 +++- test/integration/emitter_test.cpp | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/emitter.cpp b/src/emitter.cpp index 5168cba89..f24aee6bc 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -947,7 +947,9 @@ Emitter& Emitter::Write(const _Comment& comment) { PrepareNode(EmitterNodeType::NoType); - if (m_stream.col() > 0) + if (m_stream.col() == 0) + m_stream << IndentTo(m_pState->CurIndent()); + else 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..8a3f3a3df 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 From 5fbe49adf5f8ba29f446818966415a492ea96486 Mon Sep 17 00:00:00 2001 From: alexprabhat99 Date: Mon, 30 Mar 2026 17:39:40 +0530 Subject: [PATCH 2/2] fix for failing tests and formatting --- src/emitter.cpp | 6 ++++-- test/integration/emitter_test.cpp | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/emitter.cpp b/src/emitter.cpp index f24aee6bc..4afb20ef4 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -947,10 +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 + } 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 8a3f3a3df..b13e09277 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -1823,14 +1823,14 @@ TEST_F(EmitterTest, ShowTrailingZero) { } TEST_F(EmitterTest, CommentInsideMapValueIsIndented) { - out << YAML::BeginMap << YAML::Key << "foo" - << YAML::BeginMap << YAML::Comment("Comment") - << YAML::Key << "bar" << YAML::Value << true + 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"); + ExpectEmit( + "foo:\n" + " # Comment\n" + " bar: true"); } } // namespace