Skip to content

Commit 897275f

Browse files
authored
Merge pull request #106 from karanankit01/cov-test
fix issue#98 : adding column_table_limit feature
2 parents 0f5e567 + d503d48 commit 897275f

File tree

12 files changed

+143
-2
lines changed

12 files changed

+143
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ break_before_table_rb: true
9090
chop_down_table: false
9191
chop_down_kv_table: true
9292
table_sep: ","
93+
column_table_limit: column_limit
9394
extra_sep_at_table_end: false
9495
break_after_operator: true
9596
double_quote_to_single_quote: false

docs/Style-Config.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,48 @@ x = {
325325
k3 = v3, k4 = v4
326326
}
327327
```
328+
### column_table_limit
329+
330+
type: int, default: column_limit
331+
332+
The column limit of each line of a table. Default value the same as column_limit value.
333+
334+
```lua
335+
--column_table_limit: column_limit
336+
test = {
337+
image = "test",
338+
list = {
339+
{
340+
ref = "testRef",
341+
tags = {"tagTest"},
342+
time = 10,
343+
344+
materials = {{materialId = 123, count = 10}}
345+
}
346+
}
347+
}
348+
349+
--column_table_limit: 20
350+
test = {
351+
image = "test",
352+
list = {
353+
{
354+
ref = "testRef",
355+
tags = {
356+
"tagTest"
357+
},
358+
time = 10,
359+
360+
materials = {
361+
{
362+
materialId = 123,
363+
count = 10
364+
}
365+
}
366+
}
367+
}
368+
}
369+
```
328370

329371
### table_sep
330372

src/Config.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Config::Config() {
3535
node_["chop_down_kv_table"] = true;
3636
node_["table_sep"] = ",";
3737
node_["extra_sep_at_table_end"] = false;
38+
node_["column_table_limit"] = node_["column_limit"];
3839

3940
node_["break_after_operator"] = true;
4041

@@ -59,11 +60,20 @@ void Config::readFromFile(const string &file) {
5960
YAML::Node n = YAML::LoadFile(file);
6061

6162
// Keys are always strings
63+
bool given = false;
64+
string CTL = "column_table_limit";
65+
string CL = "column_limit";
6266
for (auto kv : n) {
6367
auto key = kv.first.as<string>();
6468
if (node_[key]) {
6569
node_[key] = kv.second;
6670
}
71+
if (key == CTL) {
72+
given = true;
73+
}
74+
}
75+
if (!given) {
76+
node_[CTL] = node_[CL];
6777
}
6878
}
6979

src/FormatVisitor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ antlrcpp::Any FormatVisitor::visitTableconstructor(LuaParser::TableconstructorCo
14821482
int length = cur_writer().firstLineColumn();
14831483
int lines = cur_writer().lines();
14841484
popWriter();
1485-
beyondLimit = cur_columns() + length > config_.get<int>("column_limit") || lines > 1;
1485+
beyondLimit = cur_columns() + length > config_.get<int>("column_table_limit") || lines > 1;
14861486
}
14871487
bool breakAfterLb = false;
14881488
if (beyondLimit) {
@@ -1576,7 +1576,7 @@ antlrcpp::Any FormatVisitor::visitFieldlist(LuaParser::FieldlistContext* ctx) {
15761576
int length = cur_writer().firstLineColumn();
15771577
popWriter();
15781578
if (i != n - 1 || config_.get<bool>("extra_sep_at_table_end")) length++; // calc a ',' if exp >1
1579-
beyondLimit = cur_columns() + length > config_.get<int>("column_limit");
1579+
beyondLimit = cur_columns() + length > config_.get<int>("column_table_limit");
15801580
if (beyondLimit) {
15811581
if (config_.get<bool>("align_table_field")) {
15821582
cur_writer() << commentAfterNewLine(ctx->fieldsep()[i - 1], NONE_INDENT);

test/test_config.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ TEST_CASE("table", "config") {
159159
config.set("break_after_table_lb", false);
160160
config.set("break_before_table_rb", false);
161161
REQUIRE("x = {1, 2, 3,\n 4, 5, 6, 7}\n" == lua_format("x = {1,2,3,4,5,6,7}", config));
162+
163+
config.set("column_limit", 250);
164+
config.set("indent_width", 2);
165+
config.set("tab_width", 2);
166+
config.set("continuation_indent_width", 2);
167+
config.set("chop_down_table", true);
168+
config.set("chop_down_kv_table", true);
169+
config.set("column_table_limit", 80);
170+
171+
REQUIRE("test = {\n image = {1, 2, 3},\n list = {\n {\n ref = {4, 5, 9, 8, 2},\n tags = {1, 2, 8, 6},\n time = 10,\n\n materials = {{materialId = 123, count = 10}}\n }\n }\n}\n" ==
172+
lua_format("test = {\n image = {1,2,3},\n list = {\n {\n ref = {4,5,9,8,2},\n tags = { 1, 2, 8, 6 },\n time = 10,\n\n materials = {\n {\n materialId = 123,\n count = 10\n }\n },\n },\n },\n}", config));
162173
}
163174

164175
TEST_CASE("break_after_operator", "config") {

test/test_format_file.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-62_1.lua");
7878
TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-62_2.lua");
7979
TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-62_3.lua");
8080
TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-70.lua");
81+
TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-98.lua");
82+
TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-98_1.lua");
8183
TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-80.lua");
8284
TEST_FILE(PROJECT_PATH "/test/testdata/issues/PR-100.lua");
8385

86+
8487
TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/default.lua");
8588
TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.lua");
8689
TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.lua");

test/testdata/issues/_issue-98.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
test = {
2+
image = "test",
3+
list = {
4+
{
5+
ref = "testRef",
6+
tags = {"tagTest"},
7+
time = 10,
8+
9+
materials = {{materialId = 123, count = 10}}
10+
}
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
test = {
2+
image = "test",
3+
list = {
4+
{
5+
ref = "testRef",
6+
tags = {"tagTest"},
7+
time = 10,
8+
9+
materials = {
10+
{materialId = 123, count = 10}
11+
}
12+
}
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
column_limit: 250
2+
indent_width: 2
3+
tab_width: 2
4+
continuation_indent_width: 2
5+
chop_down_table: true
6+
chop_down_kv_table: true
7+
column_table_limit: 80

test/testdata/issues/issue-98.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
test = {
2+
image = "test",
3+
list = {
4+
{
5+
ref = "testRef",
6+
tags = { "tagTest" },
7+
time = 10,
8+
9+
materials = {
10+
{
11+
materialId = 123,
12+
count = 10
13+
}
14+
},
15+
},
16+
},
17+
}

0 commit comments

Comments
 (0)