Improve table stringifier when line breaks enabled

This commit is contained in:
JohnnyMorganz 2022-05-17 20:14:54 +01:00
parent f2191b9e4d
commit 6ac53f5f39
2 changed files with 51 additions and 8 deletions

View file

@ -628,6 +628,7 @@ struct TypeVarStringifier
bool comma = false; bool comma = false;
if (ttv.indexer) if (ttv.indexer)
{ {
state.newline();
state.emit("["); state.emit("[");
stringify(ttv.indexer->indexType); stringify(ttv.indexer->indexType);
state.emit("]: "); state.emit("]: ");
@ -644,6 +645,10 @@ struct TypeVarStringifier
state.emit(","); state.emit(",");
state.newline(); state.newline();
} }
else
{
state.newline();
}
size_t length = state.result.name.length() - oldLength; size_t length = state.result.name.length() - oldLength;
@ -670,6 +675,10 @@ struct TypeVarStringifier
} }
state.dedent(); state.dedent();
if (comma)
state.newline();
else
state.emit(" ");
state.emit(closedbrace); state.emit(closedbrace);
state.unsee(&ttv); state.unsee(&ttv);

View file

@ -60,6 +60,40 @@ TEST_CASE_FIXTURE(Fixture, "named_table")
CHECK_EQ("TheTable", toString(&table)); CHECK_EQ("TheTable", toString(&table));
} }
TEST_CASE_FIXTURE(Fixture, "empty_table")
{
CheckResult result = check(R"(
local a: {}
)");
CHECK_EQ("{| |}", toString(requireType("a")));
// Should stay the same with useLineBreaks enabled
ToStringOptions opts;
opts.useLineBreaks = true;
CHECK_EQ("{| |}", toString(requireType("a"), opts));
}
TEST_CASE_FIXTURE(Fixture, "table_respects_use_line_break")
{
CheckResult result = check(R"(
local a: { prop: string, anotherProp: number, thirdProp: boolean }
)");
ToStringOptions opts;
opts.useLineBreaks = true;
opts.indent = true;
//clang-format off
CHECK_EQ("{|\n"
" anotherProp: number,\n"
" prop: string,\n"
" thirdProp: boolean\n"
"|}",
toString(requireType("a"), opts));
//clang-format on
}
TEST_CASE_FIXTURE(BuiltinsFixture, "exhaustive_toString_of_cyclic_table") TEST_CASE_FIXTURE(BuiltinsFixture, "exhaustive_toString_of_cyclic_table")
{ {
CheckResult result = check(R"( CheckResult result = check(R"(