Skip to content

Commit e7aaccc

Browse files
authored
Formatter flag to force multi-line arrays (#282)
1 parent bf869b0 commit e7aaccc

File tree

6 files changed

+115
-8
lines changed

6 files changed

+115
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
312312
- **[@tyler92](https://github.com/tyler92)** - Fixed stack overflow that occurred during fuzzing tests
313313
- **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions
314314
- **[@ximion](https://github.com/ximion)** - Added support for installation with meson
315+
- **[@rafal-c](https://github.com/rafal-c)** - Added a formatting flag
315316
<br>
316317

317318
## Contact

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

toml.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ TOML_NAMESPACE_START // abi namespace
16741674
indentation = indent_sub_tables | indent_array_elements,
16751675
relaxed_float_precision = (1ull << 11),
16761676
terse_key_value_pairs = (1ull << 12),
1677+
force_multiline_arrays = (1ull << 13),
16771678
};
16781679
TOML_MAKE_FLAGS(format_flags);
16791680

@@ -9890,6 +9891,12 @@ TOML_IMPL_NAMESPACE_START
98909891
return !!(config_.flags & format_flags::terse_key_value_pairs);
98919892
}
98929893

9894+
TOML_PURE_INLINE_GETTER
9895+
bool force_multiline_arrays() const noexcept
9896+
{
9897+
return !!(config_.flags & format_flags::force_multiline_arrays);
9898+
}
9899+
98939900
TOML_EXPORTED_MEMBER_FUNCTION
98949901
void attach(std::ostream& stream) noexcept;
98959902

@@ -17195,10 +17202,11 @@ TOML_NAMESPACE_START
1719517202
}
1719617203

1719717204
const auto original_indent = indent();
17198-
const auto multiline = TOML_ANON_NAMESPACE::toml_formatter_forces_multiline(
17199-
arr,
17200-
120u,
17201-
indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent));
17205+
const auto multiline = force_multiline_arrays()
17206+
|| TOML_ANON_NAMESPACE::toml_formatter_forces_multiline(
17207+
arr,
17208+
120u,
17209+
indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent));
1720217210

1720317211
print_unformatted("["sv);
1720417212

0 commit comments

Comments
 (0)