Skip to content

Commit

Permalink
Merge pull request #79 from 5lbBookOfGre/01_type_based_single_block
Browse files Browse the repository at this point in the history
Support "disable_simple_one_line_block" for specific type of code block.
  • Loading branch information
Koihik authored Jan 9, 2020
2 parents c214194 + 3d3bdb9 commit 310cecb
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions docs/Style-Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions src/FormatVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<bool>("keep_simple_block_one_line")) {
bool FormatVisitor::needKeepBlockOneLine(tree::ParseTree* previousNode, LuaParser::BlockContext* ctx, BlockType blockType) {
if (blockType == CONTROL_BLOCK && !config_.get<bool>("keep_simple_control_block_one_line")){
return false;
} else if(blockType == FUNCTION_BLOCK && !config_.get<bool>("keep_simple_function_one_line")){
return false;
}

int stats = 0;
for (auto& s : ctx->stat()) {
if (s->SEMI() == NULL) {
Expand Down Expand Up @@ -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_;
Expand Down
8 changes: 5 additions & 3 deletions src/FormatVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/test_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
5 changes: 5 additions & 0 deletions test/test_format_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

5 changes: 3 additions & 2 deletions test/testdata/issues/issue-36.config
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
break_after_operator: true
41 changes: 41 additions & 0 deletions test/testdata/keep_simple_block_one_line/_default.lua
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 310cecb

Please sign in to comment.