Skip to content

Commit 1fffc9f

Browse files
committed
Add space_inside_square_brackets option.
1 parent 9643d20 commit 1fffc9f

File tree

7 files changed

+47
-3
lines changed

7 files changed

+47
-3
lines changed

CodeService/src/LuaEditorConfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
399399
configMap.at("space_before_open_square_bracket") == "true";
400400
}
401401

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+
402408
if(configMap.count("table_separator_style"))
403409
{
404410
auto style = configMap.at("table_separator_style");

CodeService/src/LuaFormatter.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,8 +1766,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
17661766
env->Add<KeepElement>(1);
17671767
continue;
17681768
}
1769+
1770+
env->Add<OperatorElement>(child);
1771+
env->Add<KeepElement>(_options.space_inside_square_brackets ? 1 : 0);
1772+
break;
17691773
}
1770-
else if (child->GetTokenType() == ']' && isIndexExprLongString)
1774+
else if (child->GetTokenType() == ']' && (isIndexExprLongString
1775+
|| _options.space_inside_square_brackets))
17711776
{
17721777
env->Add<KeepElement>(1);
17731778
}
@@ -2478,11 +2483,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
24782483
}
24792484

24802485
env->Add<OperatorElement>(child);
2481-
env->Add<KeepElement>(0);
2486+
env->Add<KeepElement>(_options.space_inside_square_brackets ? 1 : 0);
24822487
}
24832488
else if (child->GetTokenType() == ']')
24842489
{
2485-
env->Add<KeepElement>(isIndexExprLongString ? 1 : 0);
2490+
env->Add<KeepElement>(isIndexExprLongString || _options.space_inside_square_brackets ? 1 : 0);
24862491
env->Add<OperatorElement>(child);
24872492
env->Add<KeepElement>(0);
24882493
}

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ space_before_open_square_bracket = true
172172
space_inside_function_call_parentheses = true
173173
[{space_inside_function_param_list_parentheses.lua}]
174174
space_inside_function_param_list_parentheses = true
175+
[{space_inside_square_brackets.lua}]
176+
space_inside_square_brackets = true
175177
[{minLine-eq-1.lua}]
176178
keep_line_after_if_statement = minLine:1
177179
keep_line_after_do_statement = minLine:1
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: 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class LuaCodeStyleOptions
8484

8585
bool space_before_open_square_bracket = false;
8686

87+
bool space_inside_square_brackets = false;
88+
8789
/*
8890
* 标签无缩进
8991
*/

lua.template.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ space_inside_function_param_list_parentheses = false
105105

106106
space_before_open_square_bracket = false
107107

108+
space_inside_square_brackets = false
109+
108110
# if true, ormat like this "local t <const> = 1"
109111
keep_one_space_between_namedef_and_attribute = true
110112

0 commit comments

Comments
 (0)