diff --git a/README.md b/README.md index e2a9298..f703ca8 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,8 @@ use_tab: false tab_width: 4 continuation_indent_width: 4 spaces_before_call: 1 -keep_simple_block_one_line: true +keep_simple_control_block_one_line: true +keep_simple_function_one_line: true align_args: true break_after_functioncall_lp: false break_before_functioncall_rp: false diff --git a/docs/Style-Config.md b/docs/Style-Config.md index 2f2d3e8..cbb7338 100644 --- a/docs/Style-Config.md +++ b/docs/Style-Config.md @@ -74,26 +74,37 @@ local xxx, yyy = 111, 222 ``` -### keep_simple_block_one_line +### keep_simple_control_block_one_line type: bool, default: true -Allow format simple block to one line. +Allow format simple control block(e.g. if, while, for, ...) to one line. ```lua -- keep_simple_block_one_line: true -function x() print(1) end if cond then xx() end -- keep_simple_block_one_line: false -function x() - print(1) -end if cond then xx() end ``` +### keep_simple_function_one_line + +type: bool, default: true + +Allow format simple function to one line. + +```lua +-- keep_simple_block_one_line: true +function x() print(1) end + +-- keep_simple_block_one_line: false +function x() + print(1) +end +``` ### align_args type: bool, default: true diff --git a/src/Config.cpp b/src/Config.cpp index c6f7eb9..4d9127f 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -16,7 +16,8 @@ Config::Config() { node_["continuation_indent_width"] = 4; node_["spaces_before_call"] = 1; - node_["keep_simple_block_one_line"] = true; + node_["keep_simple_control_block_one_line"] = true; + node_["keep_simple_function_one_line"] = true; node_["align_args"] = true; node_["break_after_functioncall_lp"] = false; diff --git a/src/FormatVisitor.cpp b/src/FormatVisitor.cpp index 335dad3..24f28eb 100644 --- a/src/FormatVisitor.cpp +++ b/src/FormatVisitor.cpp @@ -406,7 +406,7 @@ antlrcpp::Any FormatVisitor::visitGotoStat(LuaParser::GotoStatContext* ctx) { antlrcpp::Any FormatVisitor::visitDoStat(LuaParser::DoStatContext* ctx) { LOG_FUNCTION_BEGIN("visitDoStat"); cur_writer() << ctx->DO()->getText(); - visitBlockAndComment(ctx->DO(), ctx->block()); + visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK); cur_writer() << ctx->END()->getText(); LOG_FUNCTION_END("visitDoStat"); return nullptr; @@ -420,7 +420,7 @@ antlrcpp::Any FormatVisitor::visitWhileStat(LuaParser::WhileStatContext* ctx) { visitExp(ctx->exp()); cur_writer() << commentAfter(ctx->exp(), " "); cur_writer() << ctx->DO()->getText(); - visitBlockAndComment(ctx->DO(), ctx->block()); + visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK); cur_writer() << ctx->END()->getText(); LOG_FUNCTION_END("visitWhileStat"); return nullptr; @@ -430,7 +430,7 @@ antlrcpp::Any FormatVisitor::visitWhileStat(LuaParser::WhileStatContext* ctx) { antlrcpp::Any FormatVisitor::visitRepeatStat(LuaParser::RepeatStatContext* ctx) { LOG_FUNCTION_BEGIN("visitRepeatStat"); cur_writer() << ctx->REPEAT()->getText(); - visitBlockAndComment(ctx->REPEAT(), ctx->block()); + visitBlockAndComment(ctx->REPEAT(), ctx->block(), CONTROL_BLOCK); cur_writer() << ctx->UNTIL()->getText(); cur_writer() << commentAfter(ctx->UNTIL(), " "); visitExp(ctx->exp()); @@ -452,7 +452,7 @@ antlrcpp::Any FormatVisitor::visitIfStat(LuaParser::IfStatContext* ctx) { cur_writer() << commentAfter(ctx->exp().front(), " "); cur_writer() << ctx->THEN().front()->getText(); if (ctx->ELSEIF().size() == 0 && ctx->ELSE() == NULL) { - if (needKeepBlockOneLine(ctx->THEN().front(), ctx->block().front())) { + if (needKeepBlockOneLine(ctx->THEN().front(), ctx->block().front(), CONTROL_BLOCK)) { cur_writer() << commentAfter(ctx->THEN().front(), " "); bool temp = chop_down_block_; chop_down_block_ = false; @@ -517,7 +517,7 @@ antlrcpp::Any FormatVisitor::visitForStat(LuaParser::ForStatContext* ctx) { cur_writer() << commentAfter(ctx->exp()[1], " "); } cur_writer() << ctx->DO()->getText(); - visitBlockAndComment(ctx->DO(), ctx->block()); + visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK); cur_writer() << ctx->END()->getText(); LOG_FUNCTION_END("visitForStat"); return nullptr; @@ -535,7 +535,7 @@ antlrcpp::Any FormatVisitor::visitForInStat(LuaParser::ForInStatContext* ctx) { visitExplist(ctx->explist()); cur_writer() << commentAfter(ctx->explist(), " "); cur_writer() << ctx->DO()->getText(); - visitBlockAndComment(ctx->DO(), ctx->block()); + visitBlockAndComment(ctx->DO(), ctx->block(), CONTROL_BLOCK); cur_writer() << ctx->END()->getText(); LOG_FUNCTION_END("visitForInStat"); return nullptr; @@ -1413,7 +1413,7 @@ antlrcpp::Any FormatVisitor::visitFuncbody(LuaParser::FuncbodyContext* ctx) { cur_writer() << commentAfter(ctx->LP(), ""); } cur_writer() << ctx->RP()->getText(); - visitBlockAndComment(ctx->RP(), ctx->block()); + visitBlockAndComment(ctx->RP(), ctx->block(), FUNCTION_BLOCK); cur_writer() << ctx->END()->getText(); LOG_FUNCTION_END("visitFuncbody"); return nullptr; @@ -1644,10 +1644,13 @@ antlrcpp::Any FormatVisitor::visitTerminal(tree::TerminalNode* node) { return nullptr; } -bool FormatVisitor::needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx) { - if (!config_.get("keep_simple_block_one_line")) { +bool FormatVisitor::needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType) { + if (blockType == CONTROL_BLOCK && !config_.get("keep_simple_control_block_one_line")){ + return false; + } else if(blockType == FUNCTION_BLOCK && !config_.get("keep_simple_function_one_line")){ return false; } + int stats = 0; for (auto& s : ctx->stat()) { if (s->SEMI() == NULL) { @@ -1687,9 +1690,9 @@ bool FormatVisitor::isBlockEmpty(LuaParser::BlockContext* ctx) { return ctx->stat().size() == 0 && ctx->retstat() == NULL; } -void FormatVisitor::visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx) { +void FormatVisitor::visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType) { LOG_FUNCTION_BEGIN("visitBlockAndComment"); - bool oneline = needKeepBlockOneLine(previousNode, ctx); + bool oneline = needKeepBlockOneLine(previousNode, ctx, blockType); if (oneline) { cur_writer() << commentAfter(previousNode, " "); bool temp = chop_down_block_; diff --git a/src/FormatVisitor.h b/src/FormatVisitor.h index 92cfd2c..115459e 100644 --- a/src/FormatVisitor.h +++ b/src/FormatVisitor.h @@ -8,6 +8,8 @@ using namespace std; using namespace antlr4; + +enum BlockType { CONTROL_BLOCK, FUNCTION_BLOCK }; enum NewLineIndent { NONE_INDENT, INC_INDENT, DEC_INDENT, INC_CONTINUATION_INDENT, DEC_CONTINUATION_INDENT }; class FormatVisitor : public LuaBaseVisitor { @@ -21,7 +23,7 @@ class FormatVisitor : public LuaBaseVisitor { antlrcpp::Any visitVarDecl(LuaParser::VarDeclContext* context) override; antlrcpp::Any visitGotoStat(LuaParser::GotoStatContext* context) override; antlrcpp::Any visitDoStat(LuaParser::DoStatContext* context) override; - antlrcpp::Any visitWhileStat(LuaParser::WhileStatContext* context) override; + antlrcpp::Any visitWhileStat(LuaParser::WhileStatContext* context) override; antlrcpp::Any visitRepeatStat(LuaParser::RepeatStatContext* context) override; antlrcpp::Any visitIfStat(LuaParser::IfStatContext* context) override; antlrcpp::Any visitForStat(LuaParser::ForStatContext* context) override; @@ -80,9 +82,9 @@ class FormatVisitor : public LuaBaseVisitor { string formatLineComment(Token* token); - bool needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx); + bool needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType); bool isBlockEmpty(LuaParser::BlockContext* ctx); - void visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx); + void visitBlockAndComment(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType); void visitNextNameAndArgs(LuaParser::VarSuffixContext* ctx); void pushWriter(); diff --git a/test/test_config.cpp b/test/test_config.cpp index 42cd20d..2710d55 100644 --- a/test/test_config.cpp +++ b/test/test_config.cpp @@ -58,13 +58,13 @@ TEST_CASE("extra_sep_at_table_end", "config") { REQUIRE("x = {\n 1, -- line break\n 2, 3\n}\n" == lua_format("x = {1,-- line break\n2;3}", config)); } -TEST_CASE("keep_simple_block_one_line", "config") { +TEST_CASE("keep_simple_function_one_line", "config") { Config config; config.set("indent_width", 2); REQUIRE("function x() print(1) end\n" == lua_format("function x() print(1) end", config)); - config.set("keep_simple_block_one_line", false); + config.set("keep_simple_function_one_line", false); REQUIRE("function x()\n print(1)\nend\n" == lua_format("function x() print(1) end", config)); } diff --git a/test/test_format_file.cpp b/test/test_format_file.cpp index ec1c00c..8c74118 100644 --- a/test/test_format_file.cpp +++ b/test/test_format_file.cpp @@ -78,3 +78,8 @@ TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-62_1.lua"); TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-62_2.lua"); TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-62_3.lua"); TEST_FILE(PROJECT_PATH "/test/testdata/issues/issue-70.lua"); + +TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/default.lua"); +TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.lua"); +TEST_FILE(PROJECT_PATH "/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.lua"); + diff --git a/test/testdata/issues/issue-36.config b/test/testdata/issues/issue-36.config index 82101cb..f9cd786 100644 --- a/test/testdata/issues/issue-36.config +++ b/test/testdata/issues/issue-36.config @@ -2,7 +2,8 @@ column_limit: 120 indent_width: 4 continuation_indent_width: 4 use_tab: false -keep_simple_block_one_line: false +keep_simple_control_block_one_line: false +keep_simple_function_one_line: false align_args: true break_after_functioncall_lp: true break_before_functioncall_rp: true @@ -16,4 +17,4 @@ break_before_table_rb: true chop_down_kv_table: true table_sep: ',' extra_sep_at_table_end: false -break_after_operator: true \ No newline at end of file +break_after_operator: true diff --git a/test/testdata/keep_simple_block_one_line/_default.lua b/test/testdata/keep_simple_block_one_line/_default.lua new file mode 100644 index 0000000..3896bfd --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/_default.lua @@ -0,0 +1,41 @@ +function x() print("hello") end + +function x() print("hello") end + +local function x() print("hello") end + +local function x() print("hello") end + +x = function() print("hello") end + +x = function() print("hello") end + +if true then print("hello") end + +if true then + print("hello") +elseif true then + print("hello") +end + +if true then print("hello") end + +while true do print("hello") end + +while true do print("hello") end + +repeat print("hello") until true + +repeat print("hello") until true + +do print("hello") end + +do print("hello") end + +for _ = 1, 10, 1 do print("hello") end + +for _ = 1, 10, 1 do print("hello") end + +for _ in pairs({}) do print("hello") end + +for _ in pairs({}) do print("hello") end diff --git a/test/testdata/keep_simple_block_one_line/_keep_simple_control_block_one_line_false.lua b/test/testdata/keep_simple_block_one_line/_keep_simple_control_block_one_line_false.lua new file mode 100644 index 0000000..3f9df41 --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/_keep_simple_control_block_one_line_false.lua @@ -0,0 +1,65 @@ +function x() print("hello") end + +function x() print("hello") end + +local function x() print("hello") end + +local function x() print("hello") end + +x = function() print("hello") end + +x = function() print("hello") end + +if true then + print("hello") +end + +if true then + print("hello") +elseif true then + print("hello") +end + +if true then + print("hello") +end + +while true do + print("hello") +end + +while true do + print("hello") +end + +repeat + print("hello") +until true + +repeat + print("hello") +until true + +do + print("hello") +end + +do + print("hello") +end + +for _ = 1, 10, 1 do + print("hello") +end + +for _ = 1, 10, 1 do + print("hello") +end + +for _ in pairs({}) do + print("hello") +end + +for _ in pairs({}) do + print("hello") +end diff --git a/test/testdata/keep_simple_block_one_line/_keep_simple_function_one_line_false.lua b/test/testdata/keep_simple_block_one_line/_keep_simple_function_one_line_false.lua new file mode 100644 index 0000000..072d3a2 --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/_keep_simple_function_one_line_false.lua @@ -0,0 +1,53 @@ +function x() + print("hello") +end + +function x() + print("hello") +end + +local function x() + print("hello") +end + +local function x() + print("hello") +end + +x = function() + print("hello") +end + +x = function() + print("hello") +end + +if true then print("hello") end + +if true then + print("hello") +elseif true then + print("hello") +end + +if true then print("hello") end + +while true do print("hello") end + +while true do print("hello") end + +repeat print("hello") until true + +repeat print("hello") until true + +do print("hello") end + +do print("hello") end + +for _ = 1, 10, 1 do print("hello") end + +for _ = 1, 10, 1 do print("hello") end + +for _ in pairs({}) do print("hello") end + +for _ in pairs({}) do print("hello") end diff --git a/test/testdata/keep_simple_block_one_line/default.lua b/test/testdata/keep_simple_block_one_line/default.lua new file mode 100644 index 0000000..b8591cf --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/default.lua @@ -0,0 +1,59 @@ +function x() + print("hello") +end + +function x() print("hello") end + +local function x() + print("hello") +end + +local function x() print("hello") end + +x = function() + print("hello") +end + +x = function() print("hello") end + +if true then + print("hello") +end + +if true then + print("hello") +elseif true then + print("hello") +end + +if true then print("hello") end + +while true do + print("hello") +end + +while true do print("hello") end + +repeat + print("hello") +until true + +repeat print("hello") until true + +do + print("hello") +end + +do print("hello") end + +for _ = 1, 10, 1 do + print("hello") +end + +for _ = 1, 10, 1 do print("hello") end + +for _ in pairs({}) do + print("hello") +end + +for _ in pairs({}) do print("hello") end diff --git a/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.config b/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.config new file mode 100644 index 0000000..bced11e --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.config @@ -0,0 +1 @@ +keep_simple_control_block_one_line: false diff --git a/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.lua b/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.lua new file mode 100644 index 0000000..b8591cf --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/keep_simple_control_block_one_line_false.lua @@ -0,0 +1,59 @@ +function x() + print("hello") +end + +function x() print("hello") end + +local function x() + print("hello") +end + +local function x() print("hello") end + +x = function() + print("hello") +end + +x = function() print("hello") end + +if true then + print("hello") +end + +if true then + print("hello") +elseif true then + print("hello") +end + +if true then print("hello") end + +while true do + print("hello") +end + +while true do print("hello") end + +repeat + print("hello") +until true + +repeat print("hello") until true + +do + print("hello") +end + +do print("hello") end + +for _ = 1, 10, 1 do + print("hello") +end + +for _ = 1, 10, 1 do print("hello") end + +for _ in pairs({}) do + print("hello") +end + +for _ in pairs({}) do print("hello") end diff --git a/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.config b/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.config new file mode 100644 index 0000000..93a240c --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.config @@ -0,0 +1 @@ +keep_simple_function_one_line: false diff --git a/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.lua b/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.lua new file mode 100644 index 0000000..b8591cf --- /dev/null +++ b/test/testdata/keep_simple_block_one_line/keep_simple_function_one_line_false.lua @@ -0,0 +1,59 @@ +function x() + print("hello") +end + +function x() print("hello") end + +local function x() + print("hello") +end + +local function x() print("hello") end + +x = function() + print("hello") +end + +x = function() print("hello") end + +if true then + print("hello") +end + +if true then + print("hello") +elseif true then + print("hello") +end + +if true then print("hello") end + +while true do + print("hello") +end + +while true do print("hello") end + +repeat + print("hello") +until true + +repeat print("hello") until true + +do + print("hello") +end + +do print("hello") end + +for _ = 1, 10, 1 do + print("hello") +end + +for _ = 1, 10, 1 do print("hello") end + +for _ in pairs({}) do + print("hello") +end + +for _ in pairs({}) do print("hello") end