Skip to content

Commit 05566b0

Browse files
committed
Merge branch 'master' of github.com:CppCXY/EmmyLuaCodeStyle
2 parents a84b487 + f389817 commit 05566b0

File tree

11 files changed

+213
-8
lines changed

11 files changed

+213
-8
lines changed

CodeService/src/LuaEditorConfig.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,30 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
381381
configMap.at("space_before_function_open_parenthesis") == "true";
382382
}
383383

384+
if(configMap.count("space_inside_function_call_parentheses"))
385+
{
386+
options->space_inside_function_call_parentheses =
387+
configMap.at("space_inside_function_call_parentheses") == "true";
388+
}
389+
390+
if(configMap.count("space_inside_function_param_list_parentheses"))
391+
{
392+
options->space_inside_function_param_list_parentheses =
393+
configMap.at("space_inside_function_param_list_parentheses") == "true";
394+
}
395+
384396
if (configMap.count("space_before_open_square_bracket"))
385397
{
386398
options->space_before_open_square_bracket =
387399
configMap.at("space_before_open_square_bracket") == "true";
388400
}
389401

402+
if (configMap.count("space_inside_square_brackets"))
403+
{
404+
options->space_inside_square_brackets =
405+
configMap.at("space_inside_square_brackets") == "true";
406+
}
407+
390408
if(configMap.count("table_separator_style"))
391409
{
392410
auto style = configMap.at("table_separator_style");

CodeService/src/LuaFormatter.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,14 +1350,25 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
13501350
}
13511351

13521352
env->AddChild(FormatCallArgsExpressionList(child, layout));
1353-
env->Add<KeepElement>(0);
1353+
env->Add<KeepElement>(_options.space_inside_function_call_parentheses ? 1 : 0);
1354+
13541355
break;
13551356
}
13561357
case LuaAstNodeType::GeneralOperator:
13571358
{
1358-
env->Add<OperatorElement>(child);
1359-
env->Add<KeepElement>(0);
1360-
break;
1359+
if (child->GetTokenType() == '(')
1360+
{
1361+
env->Add<OperatorElement>(child);
1362+
env->Add<KeepElement>(_options.space_inside_function_call_parentheses
1363+
&& children.size() > 2 ? 1 : 0);
1364+
break;
1365+
}
1366+
else
1367+
{
1368+
env->Add<OperatorElement>(child);
1369+
env->Add<KeepElement>(0);
1370+
break;
1371+
}
13611372
}
13621373
case LuaAstNodeType::StringLiteralExpression:
13631374
case LuaAstNodeType::TableExpression:
@@ -1465,12 +1476,18 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatParamList(std::shared_ptr<Lua
14651476
paramListLayoutEnv->Add<OperatorElement>(child);
14661477
paramListLayoutEnv->Add<KeepElement>(1);
14671478
}
1479+
else if (child->GetTokenType() == '(')
1480+
{
1481+
env->Add<OperatorElement>(child);
1482+
env->Add<KeepElement>(_options.space_inside_function_param_list_parentheses
1483+
&& children.size() > 2 ? 1 : 0);
1484+
}
14681485
else if (child->GetTokenType() == ')')
14691486
{
14701487
env->AddChild(paramListLayoutEnv);
14711488
if (!paramListLayoutEnv->GetChildren().empty())
14721489
{
1473-
env->Add<KeepElement>(0);
1490+
env->Add<KeepElement>(_options.space_inside_function_param_list_parentheses ? 1 : 0);
14741491
}
14751492

14761493
env->Add<OperatorElement>(child);
@@ -1749,8 +1766,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
17491766
env->Add<KeepElement>(1);
17501767
continue;
17511768
}
1769+
1770+
env->Add<OperatorElement>(child);
1771+
env->Add<KeepElement>(_options.space_inside_square_brackets ? 1 : 0);
1772+
break;
17521773
}
1753-
else if (child->GetTokenType() == ']' && isIndexExprLongString)
1774+
else if (child->GetTokenType() == ']' && (isIndexExprLongString
1775+
|| _options.space_inside_square_brackets))
17541776
{
17551777
env->Add<KeepElement>(1);
17561778
}
@@ -2461,11 +2483,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
24612483
}
24622484

24632485
env->Add<OperatorElement>(child);
2464-
env->Add<KeepElement>(0);
2486+
env->Add<KeepElement>(_options.space_inside_square_brackets ? 1 : 0);
24652487
}
24662488
else if (child->GetTokenType() == ']')
24672489
{
2468-
env->Add<KeepElement>(isIndexExprLongString ? 1 : 0);
2490+
env->Add<KeepElement>(isIndexExprLongString || _options.space_inside_square_brackets ? 1 : 0);
24692491
env->Add<OperatorElement>(child);
24702492
env->Add<KeepElement>(0);
24712493
}

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ remove_expression_list_finish_comma = true
168168
[{space_option.lua}]
169169
space_before_function_open_parenthesis = true
170170
space_before_open_square_bracket = true
171+
[{space_inside_function_call_parentheses.lua}]
172+
space_inside_function_call_parentheses = true
173+
[{space_inside_function_param_list_parentheses.lua}]
174+
space_inside_function_param_list_parentheses = true
175+
[{space_inside_square_brackets.lua}]
176+
space_inside_square_brackets = true
171177
[{minLine-eq-1.lua}]
172178
keep_line_after_if_statement = minLine:1
173179
keep_line_after_do_statement = minLine:1
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local f0 = function()
2+
end
3+
4+
local f1 = function(a)
5+
return a
6+
end
7+
8+
local f2 = function(a,b)
9+
return a, b
10+
end
11+
12+
local f3 = function(a,b,c)
13+
return a, b, c
14+
end
15+
16+
f0()
17+
18+
local a = f1(123)
19+
local b1, b2 = f2( 'b1','b2')
20+
local c1, c2, c3 = f3('c1', 'c2','c3' )
21+
22+
local d1, d2, d3 = f3(
23+
'c1',
24+
'c2',
25+
'c3'
26+
)
27+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local f0 = function()
2+
end
3+
4+
local f1 = function(a)
5+
return a
6+
end
7+
8+
local f2 = function(a,b)
9+
return a, b
10+
end
11+
12+
local f3 = function(a,b,c)
13+
return a, b, c
14+
end
15+
16+
f0()
17+
18+
local a = f1(123)
19+
local b1, b2 = f2( 'b1','b2')
20+
local c1, c2, c3 = f3('c1', 'c2','c3' )
21+
22+
function t:fffff(
23+
aaa
24+
)
25+
local t = 13
26+
end
27+
28+
local already_formatted = function( x, y, z )
29+
return x, y, z
30+
end
31+
32+
already_formatted( 'a', 'b', 'c' )
33+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
c["abc"] = 10
2+
c[123] = c["123"]["12345"]
3+
c[ [[a]] ] = c[ [[1]] ]
4+
5+
local t4 = {
6+
aaa = 1,
7+
bbbb = 4456,
8+
["wgfwwfw" ] = 890,
9+
[{}] = 131,
10+
[function() end] = 131,
11+
[1231] = 12321,
12+
[ [[123]] ] = 456
13+
}
14+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local f0 = function()
2+
end
3+
4+
local f1 = function(a)
5+
return a
6+
end
7+
8+
local f2 = function(a, b)
9+
return a, b
10+
end
11+
12+
local f3 = function(a, b, c)
13+
return a, b, c
14+
end
15+
16+
f0()
17+
18+
local a = f1( 123 )
19+
local b1, b2 = f2( 'b1', 'b2' )
20+
local c1, c2, c3 = f3( 'c1', 'c2', 'c3' )
21+
22+
local d1, d2, d3 = f3(
23+
'c1',
24+
'c2',
25+
'c3'
26+
)
27+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local f0 = function()
2+
end
3+
4+
local f1 = function( a )
5+
return a
6+
end
7+
8+
local f2 = function( a, b )
9+
return a, b
10+
end
11+
12+
local f3 = function( a, b, c )
13+
return a, b, c
14+
end
15+
16+
f0()
17+
18+
local a = f1(123)
19+
local b1, b2 = f2('b1', 'b2')
20+
local c1, c2, c3 = f3('c1', 'c2', 'c3')
21+
22+
function t:fffff(
23+
aaa
24+
)
25+
local t = 13
26+
end
27+
28+
local already_formatted = function( x, y, z )
29+
return x, y, z
30+
end
31+
32+
already_formatted('a', 'b', 'c')
33+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
c[ "abc" ] = 10
2+
c[ 123 ] = c[ "123" ][ "12345" ]
3+
c[ [[a]] ] = c[ [[1]] ]
4+
5+
local t4 = {
6+
aaa = 1,
7+
bbbb = 4456,
8+
[ "wgfwwfw" ] = 890,
9+
[ {} ] = 131,
10+
[ function() end ] = 131,
11+
[ 1231 ] = 12321,
12+
[ [[123]] ] = 456
13+
}

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ class LuaCodeStyleOptions
7878

7979
bool space_before_function_open_parenthesis = false;
8080

81+
bool space_inside_function_call_parentheses = false;
82+
83+
bool space_inside_function_param_list_parentheses = false;
84+
8185
bool space_before_open_square_bracket = false;
8286

87+
bool space_inside_square_brackets = false;
88+
8389
/*
8490
* 标签无缩进
8591
*/

0 commit comments

Comments
 (0)