Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
63 changes: 63 additions & 0 deletions test/integration/emitter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1822,5 +1822,68 @@ 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");
}

TEST_F(EmitterTest, CommentInsideDoubleNestedMapIsIndented) {
out << YAML::BeginMap << YAML::Key << "map1" << YAML::Value << YAML::BeginMap
<< YAML::Key << "map2" << YAML::Value << YAML::BeginMap
<< YAML::Comment("nested comment") << YAML::Key << "foo" << YAML::Value
<< "bar" << YAML::EndMap << YAML::EndMap << YAML::EndMap;

ExpectEmit(
"map1:\n"
" map2:\n"
" # nested comment\n"
" foo: bar");
}

TEST_F(EmitterTest, CommentAtEndOfDoubleNestedMapIsIndented) {
out << YAML::BeginMap << YAML::Key << "map1" << YAML::Value << YAML::BeginMap
<< YAML::Key << "map2" << YAML::Value << YAML::BeginMap << YAML::Key
<< "foo" << YAML::Value << "bar" << YAML::Newline
<< YAML::Comment("nested comment at the end") << YAML::EndMap
<< YAML::EndMap << YAML::EndMap;

ExpectEmit(
"map1:\n"
" map2:\n"
" foo: bar\n"
" # nested comment at the end");
}

TEST_F(EmitterTest, CommentAfterNestedMapUsesParentIndentation) {
out << YAML::BeginMap << YAML::Key << "map1" << YAML::Value << YAML::BeginMap
<< YAML::Key << "map2" << YAML::Value << YAML::BeginMap << YAML::Key
<< "foo" << YAML::Value << "bar" << YAML::EndMap << YAML::Newline
<< YAML::Comment("nested comment outside of map2") << YAML::EndMap
<< YAML::EndMap;

ExpectEmit(
"map1:\n"
" map2:\n"
" foo: bar\n"
" # nested comment outside of map2");
}

TEST_F(EmitterTest, CommentInsideFlowMapIsUnaffected) {
out << YAML::BeginMap << YAML::Key << "some_map" << YAML::Value << YAML::Flow
<< YAML::BeginMap << YAML::Key << "foo" << YAML::Value << "bar"
<< YAML::Comment("comment") << YAML::Key << "key2" << YAML::Value
<< "value2" << YAML::EndMap << YAML::EndMap;

ExpectEmit(
"some_map: {foo: bar, # comment\n"
"key2: value2}");
}

} // namespace
} // namespace YAML