Skip to content

Commit c9c67ec

Browse files
committed
Add force_multiline_arrays formatting flag and corresponding tests. With this flag set, all arrays will be printed one element per line.
1 parent bf869b0 commit c9c67ec

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

include/toml++/impl/formatter.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ TOML_IMPL_NAMESPACE_START
130130
return !!(config_.flags & format_flags::terse_key_value_pairs);
131131
}
132132

133+
TOML_PURE_INLINE_GETTER
134+
bool force_multiline_arrays() const noexcept
135+
{
136+
return !!(config_.flags & format_flags::force_multiline_arrays);
137+
}
138+
133139
TOML_EXPORTED_MEMBER_FUNCTION
134140
void attach(std::ostream& stream) noexcept;
135141

include/toml++/impl/forward_declarations.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ TOML_NAMESPACE_START // abi namespace
343343

344344
/// \brief Avoids the use of whitespace around key-value pairs.
345345
terse_key_value_pairs = (1ull << 12),
346+
347+
/// \brief Always print multiline arrays (one element per line).
348+
force_multiline_arrays = (1ull << 13),
346349
};
347350
TOML_MAKE_FLAGS(format_flags);
348351

include/toml++/impl/toml_formatter.inl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,11 @@ TOML_NAMESPACE_START
179179
}
180180

181181
const auto original_indent = indent();
182-
const auto multiline = TOML_ANON_NAMESPACE::toml_formatter_forces_multiline(
183-
arr,
184-
120u,
185-
indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent));
182+
const auto multiline = force_multiline_arrays()
183+
|| TOML_ANON_NAMESPACE::toml_formatter_forces_multiline(
184+
arr,
185+
120u,
186+
indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent));
186187

187188
print_unformatted("["sv);
188189

tests/manipulating_tables.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,94 @@ key8 = [
614614
])"sv;
615615
CHECK(to_string(input, toml_formatter::default_flags, format_flags::indentation)
616616
== expected_without_indentation);
617+
618+
// forcing multiline arrays: even short arrays become one-per-line (with array elements indented)
619+
constexpr auto expected_forced_multiline = R"(key1 = 'val1'
620+
key2 = [
621+
1,
622+
2,
623+
3,
624+
4,
625+
'5'
626+
]
627+
key3 = [
628+
'this is a really long array',
629+
'and should be split over multiple lines',
630+
'by the formatter',
631+
'unless i dun goofed',
632+
'i guess thats what tests are for'
633+
]
634+
635+
[sub1]
636+
key4 = 'val'
637+
638+
[sub2]
639+
key5 = 'val'
640+
641+
[sub2.sub3]
642+
key6 = 'val'
643+
key7 = [
644+
1,
645+
2,
646+
3,
647+
4,
648+
'5'
649+
]
650+
key8 = [
651+
'this is a really long array',
652+
'and should be split over multiple lines',
653+
'by the formatter',
654+
'unless i dun goofed',
655+
'i guess thats what tests are for'
656+
])"sv;
657+
658+
CHECK(to_string(input, toml_formatter::default_flags | format_flags::force_multiline_arrays)
659+
== expected_forced_multiline);
660+
661+
// forcing multiline arrays without indenting array elements
662+
constexpr auto expected_forced_without_indented_arrays = R"(key1 = 'val1'
663+
key2 = [
664+
1,
665+
2,
666+
3,
667+
4,
668+
'5'
669+
]
670+
key3 = [
671+
'this is a really long array',
672+
'and should be split over multiple lines',
673+
'by the formatter',
674+
'unless i dun goofed',
675+
'i guess thats what tests are for'
676+
]
677+
678+
[sub1]
679+
key4 = 'val'
680+
681+
[sub2]
682+
key5 = 'val'
683+
684+
[sub2.sub3]
685+
key6 = 'val'
686+
key7 = [
687+
1,
688+
2,
689+
3,
690+
4,
691+
'5'
692+
]
693+
key8 = [
694+
'this is a really long array',
695+
'and should be split over multiple lines',
696+
'by the formatter',
697+
'unless i dun goofed',
698+
'i guess thats what tests are for'
699+
])"sv;
700+
701+
CHECK(to_string(input,
702+
toml_formatter::default_flags | format_flags::force_multiline_arrays,
703+
format_flags::indent_array_elements)
704+
== expected_forced_without_indented_arrays);
617705
}
618706
}
619707

0 commit comments

Comments
 (0)