diff --git a/README.md b/README.md index d1f956cb..1e8e2f7e 100644 --- a/README.md +++ b/README.md @@ -312,6 +312,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[ - **[@tyler92](https://github.com/tyler92)** - Fixed stack overflow that occurred during fuzzing tests - **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions - **[@ximion](https://github.com/ximion)** - Added support for installation with meson +- **[@rafal-c](https://github.com/rafal-c)** - Added a formatting flag
## Contact diff --git a/include/toml++/impl/formatter.hpp b/include/toml++/impl/formatter.hpp index 0c97833f..65005b12 100644 --- a/include/toml++/impl/formatter.hpp +++ b/include/toml++/impl/formatter.hpp @@ -130,6 +130,12 @@ TOML_IMPL_NAMESPACE_START return !!(config_.flags & format_flags::terse_key_value_pairs); } + TOML_PURE_INLINE_GETTER + bool force_multiline_arrays() const noexcept + { + return !!(config_.flags & format_flags::force_multiline_arrays); + } + TOML_EXPORTED_MEMBER_FUNCTION void attach(std::ostream& stream) noexcept; diff --git a/include/toml++/impl/forward_declarations.hpp b/include/toml++/impl/forward_declarations.hpp index 386a9e06..f4dfe4ff 100644 --- a/include/toml++/impl/forward_declarations.hpp +++ b/include/toml++/impl/forward_declarations.hpp @@ -343,6 +343,9 @@ TOML_NAMESPACE_START // abi namespace /// \brief Avoids the use of whitespace around key-value pairs. terse_key_value_pairs = (1ull << 12), + + /// \brief Always print multiline arrays (one element per line). + force_multiline_arrays = (1ull << 13), }; TOML_MAKE_FLAGS(format_flags); diff --git a/include/toml++/impl/toml_formatter.inl b/include/toml++/impl/toml_formatter.inl index 8b4f633d..bc830324 100644 --- a/include/toml++/impl/toml_formatter.inl +++ b/include/toml++/impl/toml_formatter.inl @@ -179,10 +179,11 @@ TOML_NAMESPACE_START } const auto original_indent = indent(); - const auto multiline = TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( - arr, - 120u, - indent_columns() * static_cast(original_indent < 0 ? 0 : original_indent)); + const auto multiline = force_multiline_arrays() + || TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( + arr, + 120u, + indent_columns() * static_cast(original_indent < 0 ? 0 : original_indent)); print_unformatted("["sv); diff --git a/tests/manipulating_tables.cpp b/tests/manipulating_tables.cpp index afed6045..56f7b3bf 100644 --- a/tests/manipulating_tables.cpp +++ b/tests/manipulating_tables.cpp @@ -614,6 +614,94 @@ key8 = [ ])"sv; CHECK(to_string(input, toml_formatter::default_flags, format_flags::indentation) == expected_without_indentation); + + // forcing multiline arrays: even short arrays become one-per-line (with array elements indented) + constexpr auto expected_forced_multiline = R"(key1 = 'val1' +key2 = [ + 1, + 2, + 3, + 4, + '5' +] +key3 = [ + 'this is a really long array', + 'and should be split over multiple lines', + 'by the formatter', + 'unless i dun goofed', + 'i guess thats what tests are for' +] + +[sub1] +key4 = 'val' + +[sub2] +key5 = 'val' + + [sub2.sub3] + key6 = 'val' + key7 = [ + 1, + 2, + 3, + 4, + '5' + ] + key8 = [ + 'this is a really long array', + 'and should be split over multiple lines', + 'by the formatter', + 'unless i dun goofed', + 'i guess thats what tests are for' + ])"sv; + + CHECK(to_string(input, toml_formatter::default_flags | format_flags::force_multiline_arrays) + == expected_forced_multiline); + + // forcing multiline arrays without indenting array elements + constexpr auto expected_forced_without_indented_arrays = R"(key1 = 'val1' +key2 = [ +1, +2, +3, +4, +'5' +] +key3 = [ +'this is a really long array', +'and should be split over multiple lines', +'by the formatter', +'unless i dun goofed', +'i guess thats what tests are for' +] + +[sub1] +key4 = 'val' + +[sub2] +key5 = 'val' + + [sub2.sub3] + key6 = 'val' + key7 = [ + 1, + 2, + 3, + 4, + '5' + ] + key8 = [ + 'this is a really long array', + 'and should be split over multiple lines', + 'by the formatter', + 'unless i dun goofed', + 'i guess thats what tests are for' + ])"sv; + + CHECK(to_string(input, + toml_formatter::default_flags | format_flags::force_multiline_arrays, + format_flags::indent_array_elements) + == expected_forced_without_indented_arrays); } } diff --git a/toml.hpp b/toml.hpp index 8fe1a0d5..c01a208f 100644 --- a/toml.hpp +++ b/toml.hpp @@ -1674,6 +1674,7 @@ TOML_NAMESPACE_START // abi namespace indentation = indent_sub_tables | indent_array_elements, relaxed_float_precision = (1ull << 11), terse_key_value_pairs = (1ull << 12), + force_multiline_arrays = (1ull << 13), }; TOML_MAKE_FLAGS(format_flags); @@ -9890,6 +9891,12 @@ TOML_IMPL_NAMESPACE_START return !!(config_.flags & format_flags::terse_key_value_pairs); } + TOML_PURE_INLINE_GETTER + bool force_multiline_arrays() const noexcept + { + return !!(config_.flags & format_flags::force_multiline_arrays); + } + TOML_EXPORTED_MEMBER_FUNCTION void attach(std::ostream& stream) noexcept; @@ -17195,10 +17202,11 @@ TOML_NAMESPACE_START } const auto original_indent = indent(); - const auto multiline = TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( - arr, - 120u, - indent_columns() * static_cast(original_indent < 0 ? 0 : original_indent)); + const auto multiline = force_multiline_arrays() + || TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( + arr, + 120u, + indent_columns() * static_cast(original_indent < 0 ? 0 : original_indent)); print_unformatted("["sv);