Skip to content

Commit 310cecb

Browse files
authored
Merge pull request #79 from 5lbBookOfGre/01_type_based_single_block
Support "disable_simple_one_line_block" for specific type of code block.
2 parents c214194 + 3d3bdb9 commit 310cecb

16 files changed

+388
-26
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ use_tab: false
7474
tab_width: 4
7575
continuation_indent_width: 4
7676
spaces_before_call: 1
77-
keep_simple_block_one_line: true
77+
keep_simple_control_block_one_line: true
78+
keep_simple_function_one_line: true
7879
align_args: true
7980
break_after_functioncall_lp: false
8081
break_before_functioncall_rp: false

docs/Style-Config.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,37 @@ local xxx, yyy =
7474
111, 222
7575
```
7676

77-
### keep_simple_block_one_line
77+
### keep_simple_control_block_one_line
7878

7979
type: bool, default: true
8080

81-
Allow format simple block to one line.
81+
Allow format simple control block(e.g. if, while, for, ...) to one line.
8282

8383
```lua
8484
-- keep_simple_block_one_line: true
85-
function x() print(1) end
8685
if cond then xx() end
8786

8887
-- keep_simple_block_one_line: false
89-
function x()
90-
print(1)
91-
end
9288
if cond then
9389
xx()
9490
end
9591
```
9692

93+
### keep_simple_function_one_line
94+
95+
type: bool, default: true
96+
97+
Allow format simple function to one line.
98+
99+
```lua
100+
-- keep_simple_block_one_line: true
101+
function x() print(1) end
102+
103+
-- keep_simple_block_one_line: false
104+
function x()
105+
print(1)
106+
end
107+
```
97108
### align_args
98109

99110
type: bool, default: true

src/Config.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Config::Config() {
1616
node_["continuation_indent_width"] = 4;
1717
node_["spaces_before_call"] = 1;
1818

19-
node_["keep_simple_block_one_line"] = true;
19+
node_["keep_simple_control_block_one_line"] = true;
20+
node_["keep_simple_function_one_line"] = true;
2021

2122
node_["align_args"] = true;
2223
node_["break_after_functioncall_lp"] = false;

src/FormatVisitor.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ antlrcpp::Any FormatVisitor::visitGotoStat(LuaParser::GotoStatContext* ctx) {
406406
antlrcpp::Any FormatVisitor::visitDoStat(LuaParser::DoStatContext* ctx) {
407407
LOG_FUNCTION_BEGIN("visitDoStat");
408408
cur_writer() << ctx->DO()->getText();
409-
visitBlockAndComment(ctx->DO(), ctx->block());
409+
visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK);
410410
cur_writer() << ctx->END()->getText();
411411
LOG_FUNCTION_END("visitDoStat");
412412
return nullptr;
@@ -420,7 +420,7 @@ antlrcpp::Any FormatVisitor::visitWhileStat(LuaParser::WhileStatContext* ctx) {
420420
visitExp(ctx->exp());
421421
cur_writer() << commentAfter(ctx->exp(), " ");
422422
cur_writer() << ctx->DO()->getText();
423-
visitBlockAndComment(ctx->DO(), ctx->block());
423+
visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK);
424424
cur_writer() << ctx->END()->getText();
425425
LOG_FUNCTION_END("visitWhileStat");
426426
return nullptr;
@@ -430,7 +430,7 @@ antlrcpp::Any FormatVisitor::visitWhileStat(LuaParser::WhileStatContext* ctx) {
430430
antlrcpp::Any FormatVisitor::visitRepeatStat(LuaParser::RepeatStatContext* ctx) {
431431
LOG_FUNCTION_BEGIN("visitRepeatStat");
432432
cur_writer() << ctx->REPEAT()->getText();
433-
visitBlockAndComment(ctx->REPEAT(), ctx->block());
433+
visitBlockAndComment(ctx->REPEAT(), ctx->block(), CONTROL_BLOCK);
434434
cur_writer() << ctx->UNTIL()->getText();
435435
cur_writer() << commentAfter(ctx->UNTIL(), " ");
436436
visitExp(ctx->exp());
@@ -452,7 +452,7 @@ antlrcpp::Any FormatVisitor::visitIfStat(LuaParser::IfStatContext* ctx) {
452452
cur_writer() << commentAfter(ctx->exp().front(), " ");
453453
cur_writer() << ctx->THEN().front()->getText();
454454
if (ctx->ELSEIF().size() == 0 && ctx->ELSE() == NULL) {
455-
if (needKeepBlockOneLine(ctx->THEN().front(), ctx->block().front())) {
455+
if (needKeepBlockOneLine(ctx->THEN().front(), ctx->block().front(), CONTROL_BLOCK)) {
456456
cur_writer() << commentAfter(ctx->THEN().front(), " ");
457457
bool temp = chop_down_block_;
458458
chop_down_block_ = false;
@@ -517,7 +517,7 @@ antlrcpp::Any FormatVisitor::visitForStat(LuaParser::ForStatContext* ctx) {
517517
cur_writer() << commentAfter(ctx->exp()[1], " ");
518518
}
519519
cur_writer() << ctx->DO()->getText();
520-
visitBlockAndComment(ctx->DO(), ctx->block());
520+
visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK);
521521
cur_writer() << ctx->END()->getText();
522522
LOG_FUNCTION_END("visitForStat");
523523
return nullptr;
@@ -535,7 +535,7 @@ antlrcpp::Any FormatVisitor::visitForInStat(LuaParser::ForInStatContext* ctx) {
535535
visitExplist(ctx->explist());
536536
cur_writer() << commentAfter(ctx->explist(), " ");
537537
cur_writer() << ctx->DO()->getText();
538-
visitBlockAndComment(ctx->DO(), ctx->block());
538+
visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK);
539539
cur_writer() << ctx->END()->getText();
540540
LOG_FUNCTION_END("visitForInStat");
541541
return nullptr;
@@ -1413,7 +1413,7 @@ antlrcpp::Any FormatVisitor::visitFuncbody(LuaParser::FuncbodyContext* ctx) {
14131413
cur_writer() << commentAfter(ctx->LP(), "");
14141414
}
14151415
cur_writer() << ctx->RP()->getText();
1416-
visitBlockAndComment(ctx->RP(), ctx->block());
1416+
visitBlockAndComment(ctx->RP(), ctx->block(), FUNCTION_BLOCK);
14171417
cur_writer() << ctx->END()->getText();
14181418
LOG_FUNCTION_END("visitFuncbody");
14191419
return nullptr;
@@ -1644,10 +1644,13 @@ antlrcpp::Any FormatVisitor::visitTerminal(tree::TerminalNode* node) {
16441644
return nullptr;
16451645
}
16461646

1647-
bool FormatVisitor::needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx) {
1648-
if (!config_.get<bool>("keep_simple_block_one_line")) {
1647+
bool FormatVisitor::needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType) {
1648+
if (blockType == CONTROL_BLOCK && !config_.get<bool>("keep_simple_control_block_one_line")){
1649+
return false;
1650+
} else if(blockType == FUNCTION_BLOCK && !config_.get<bool>("keep_simple_function_one_line")){
16491651
return false;
16501652
}
1653+
16511654
int stats = 0;
16521655
for (auto& s : ctx->stat()) {
16531656
if (s->SEMI() == NULL) {
@@ -1687,9 +1690,9 @@ bool FormatVisitor::isBlockEmpty(LuaParser::BlockContext* ctx) {
16871690
return ctx->stat().size() == 0 && ctx->retstat() == NULL;
16881691
}
16891692

1690-
void FormatVisitor::visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx) {
1693+
void FormatVisitor::visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType) {
16911694
LOG_FUNCTION_BEGIN("visitBlockAndComment");
1692-
bool oneline = needKeepBlockOneLine(previousNode, ctx);
1695+
bool oneline = needKeepBlockOneLine(previousNode, ctx, blockType);
16931696
if (oneline) {
16941697
cur_writer() << commentAfter(previousNode, " ");
16951698
bool temp = chop_down_block_;

src/FormatVisitor.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using namespace std;
99
using namespace antlr4;
1010

11+
12+
enum BlockType { CONTROL_BLOCK, FUNCTION_BLOCK };
1113
enum NewLineIndent { NONE_INDENT, INC_INDENT, DEC_INDENT, INC_CONTINUATION_INDENT, DEC_CONTINUATION_INDENT };
1214

1315
class FormatVisitor : public LuaBaseVisitor {
@@ -21,7 +23,7 @@ class FormatVisitor : public LuaBaseVisitor {
2123
antlrcpp::Any visitVarDecl(LuaParser::VarDeclContext* context) override;
2224
antlrcpp::Any visitGotoStat(LuaParser::GotoStatContext* context) override;
2325
antlrcpp::Any visitDoStat(LuaParser::DoStatContext* context) override;
24-
antlrcpp::Any visitWhileStat(LuaParser::WhileStatContext* context) override;
26+
antlrcpp::Any visitWhileStat(LuaParser::WhileStatContext* context) override;
2527
antlrcpp::Any visitRepeatStat(LuaParser::RepeatStatContext* context) override;
2628
antlrcpp::Any visitIfStat(LuaParser::IfStatContext* context) override;
2729
antlrcpp::Any visitForStat(LuaParser::ForStatContext* context) override;
@@ -80,9 +82,9 @@ class FormatVisitor : public LuaBaseVisitor {
8082

8183
string formatLineComment(Token* token);
8284

83-
bool needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx);
85+
bool needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType);
8486
bool isBlockEmpty(LuaParser::BlockContext* ctx);
85-
void visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx);
87+
void visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType);
8688
void visitNextNameAndArgs(LuaParser::VarSuffixContext* ctx);
8789

8890
void pushWriter();

test/test_config.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ TEST_CASE("extra_sep_at_table_end", "config") {
5858
REQUIRE("x = {\n 1, -- line break\n 2, 3\n}\n" == lua_format("x = {1,-- line break\n2;3}", config));
5959
}
6060

61-
TEST_CASE("keep_simple_block_one_line", "config") {
61+
TEST_CASE("keep_simple_function_one_line", "config") {
6262
Config config;
6363
config.set("indent_width", 2);
6464

6565
REQUIRE("function x() print(1) end\n" == lua_format("function x() print(1) end", config));
6666

67-
config.set("keep_simple_block_one_line", false);
67+
config.set("keep_simple_function_one_line", false);
6868
REQUIRE("function x()\n print(1)\nend\n" == lua_format("function x() print(1) end", config));
6969
}
7070

test/test_format_file.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ 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+
82+
TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/default.lua");
83+
TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.lua");
84+
TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.lua");
85+

test/testdata/issues/issue-36.config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ column_limit: 120
22
indent_width: 4
33
continuation_indent_width: 4
44
use_tab: false
5-
keep_simple_block_one_line: false
5+
keep_simple_control_block_one_line: false
6+
keep_simple_function_one_line: false
67
align_args: true
78
break_after_functioncall_lp: true
89
break_before_functioncall_rp: true
@@ -16,4 +17,4 @@ break_before_table_rb: true
1617
chop_down_kv_table: true
1718
table_sep: ','
1819
extra_sep_at_table_end: false
19-
break_after_operator: true
20+
break_after_operator: true
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function x() print("hello") end
2+
3+
function x() print("hello") end
4+
5+
local function x() print("hello") end
6+
7+
local function x() print("hello") end
8+
9+
x = function() print("hello") end
10+
11+
x = function() print("hello") end
12+
13+
if true then print("hello") end
14+
15+
if true then
16+
print("hello")
17+
elseif true then
18+
print("hello")
19+
end
20+
21+
if true then print("hello") end
22+
23+
while true do print("hello") end
24+
25+
while true do print("hello") end
26+
27+
repeat print("hello") until true
28+
29+
repeat print("hello") until true
30+
31+
do print("hello") end
32+
33+
do print("hello") end
34+
35+
for _ = 1, 10, 1 do print("hello") end
36+
37+
for _ = 1, 10, 1 do print("hello") end
38+
39+
for _ in pairs({}) do print("hello") end
40+
41+
for _ in pairs({}) do print("hello") end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function x() print("hello") end
2+
3+
function x() print("hello") end
4+
5+
local function x() print("hello") end
6+
7+
local function x() print("hello") end
8+
9+
x = function() print("hello") end
10+
11+
x = function() print("hello") end
12+
13+
if true then
14+
print("hello")
15+
end
16+
17+
if true then
18+
print("hello")
19+
elseif true then
20+
print("hello")
21+
end
22+
23+
if true then
24+
print("hello")
25+
end
26+
27+
while true do
28+
print("hello")
29+
end
30+
31+
while true do
32+
print("hello")
33+
end
34+
35+
repeat
36+
print("hello")
37+
until true
38+
39+
repeat
40+
print("hello")
41+
until true
42+
43+
do
44+
print("hello")
45+
end
46+
47+
do
48+
print("hello")
49+
end
50+
51+
for _ = 1, 10, 1 do
52+
print("hello")
53+
end
54+
55+
for _ = 1, 10, 1 do
56+
print("hello")
57+
end
58+
59+
for _ in pairs({}) do
60+
print("hello")
61+
end
62+
63+
for _ in pairs({}) do
64+
print("hello")
65+
end

0 commit comments

Comments
 (0)