Skip to content

Commit 2a2e585

Browse files
committed
support some space option
1 parent e001ca6 commit 2a2e585

File tree

9 files changed

+71
-7
lines changed

9 files changed

+71
-7
lines changed

CodeService/src/LuaEditorConfig.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
358358
configMap.at("remove_expression_list_finish_comma") == "true";
359359
}
360360

361+
if(configMap.count("space_before_function_open_parenthesis"))
362+
{
363+
options->space_before_function_open_parenthesis =
364+
configMap.at("space_before_function_open_parenthesis") == "true";
365+
}
366+
367+
if (configMap.count("space_before_open_square_bracket"))
368+
{
369+
options->space_before_open_square_bracket =
370+
configMap.at("space_before_open_square_bracket") == "true";
371+
}
372+
361373
if (configMap.count("end_of_line"))
362374
{
363375
auto lineSeparatorSymbol = configMap.at("end_of_line");

CodeService/src/LuaFormatter.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatFunctionStatement(std::shared
13201320
}
13211321
case LuaAstNodeType::FunctionBody:
13221322
{
1323+
if (_options.space_before_function_open_parenthesis)
1324+
{
1325+
env->Add<KeepBlankElement>(1);
1326+
}
1327+
13231328
env->AddChild(FormatNode(child));
13241329
break;
13251330
}
@@ -1449,6 +1454,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatClosureExpression(std::shared
14491454
}
14501455
case LuaAstNodeType::FunctionBody:
14511456
{
1457+
if (_options.space_before_function_open_parenthesis)
1458+
{
1459+
env->Add<KeepBlankElement>(1);
1460+
}
14521461
env->AddChild(FormatNode(child));
14531462
break;
14541463
}
@@ -2287,7 +2296,20 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
22872296
default:
22882297
{
22892298
FormatSubExpression(child, env);
2290-
env->Add<KeepElement>(0);
2299+
2300+
auto nextNode = NextNode(it, children);
2301+
if (_options.space_before_open_square_bracket
2302+
&& nextNode != nullptr
2303+
&& nextNode->GetType() == LuaAstNodeType::IndexOperator
2304+
&& nextNode->GetTokenType() == '['
2305+
&& !StringUtil::EndWith(child->GetText(), "]"))
2306+
{
2307+
env->Add<KeepElement>(1);
2308+
}
2309+
else
2310+
{
2311+
env->Add<KeepElement>(0);
2312+
}
22912313
}
22922314
}
22932315
}
@@ -2313,7 +2335,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt
23132335
auto next = NextNode(it, children);
23142336
if (next && next->GetType() == LuaAstNodeType::CallArgList)
23152337
{
2316-
if (!ast_util::WillCallArgHaveParentheses(next, _options.call_arg_parentheses))
2338+
if (!ast_util::WillCallArgHaveParentheses(next, _options.call_arg_parentheses) || _options.
2339+
space_before_function_open_parenthesis)
23172340
{
23182341
//TODO workaround
23192342
env->Add<KeepElement>(1, false, false);

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ align_chained_expression_statement = true
163163
remove_empty_header_and_footer_lines_in_function = true
164164
[{remove_expression_list_finish_comma-eq-true.lua}]
165165
remove_expression_list_finish_comma = true
166+
[{space_option.lua}]
167+
space_before_function_open_parenthesis = true
168+
space_before_open_square_bracket = true
166169
[{minLine-eq-1.lua}]
167170
keep_line_after_if_statement = minLine:1
168171
keep_line_after_do_statement = minLine:1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local t = 13
2+
c.t ["12313"] = t ["123131"]["123131"]
3+
t.cccc.ddd [1313131] = 1231
4+
local cc = 123
5+
print(123131)()

Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ align_chained_expression_statement = true
161161
remove_empty_header_and_footer_lines_in_function = true
162162
[{remove_expression_list_finish_comma-eq-true.lua}]
163163
remove_expression_list_finish_comma = true
164+
[{space_option.lua}]
165+
space_before_function_open_parenthesis = true
166+
space_before_open_square_bracket = true
164167
[{minLine-eq-1.lua}]
165168
keep_line_after_if_statement = minLine:1
166169
keep_line_after_do_statement = minLine:1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local t = 13
2+
c.t ["12313"] = t ["123131"]["123131"]
3+
t.cccc.ddd [1313131] = 1231
4+
local cc = 123
5+
print (123131)()

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class LuaCodeStyleOptions
7272
*/
7373
bool keep_one_space_between_namedef_and_attribute = true;
7474

75+
bool space_before_function_open_parenthesis = false;
76+
77+
bool space_before_open_square_bracket = false;
78+
7579
/*
7680
* 标签无缩进
7781
*/

include/CodeService/LuaFormatter.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
#include "LuaCodeStyleOptions.h"
66
#include "FormatElement/FormatElement.h"
77
#include "LuaFormatRange.h"
8+
#include <functional>
89

910
class LuaFormatter
1011
{
1112
public:
13+
using BlockFilter = std::function<bool(std::shared_ptr<LuaAstNode>)>;
14+
1215
LuaFormatter(std::shared_ptr<LuaParser> luaParser, LuaCodeStyleOptions& options);
1316
~LuaFormatter() = default;
1417

@@ -27,6 +30,9 @@ class LuaFormatter
2730

2831
std::shared_ptr<FormatElement> FormatBlock(std::shared_ptr<LuaAstNode> blockNode);
2932

33+
// special handle range format
34+
std::shared_ptr<FormatElement> FormatRangeBlock(std::shared_ptr<LuaAstNode> blockNode, LuaFormatRange& validRange);
35+
3036
std::shared_ptr<FormatElement> FormatLocalStatement(std::shared_ptr<LuaAstNode> localStatement);
3137

3238
std::shared_ptr<FormatElement> FormatAssignment(std::shared_ptr<LuaAstNode> assignStatement);
@@ -132,9 +138,6 @@ class LuaFormatter
132138
// special rule [#8](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/8)
133139
std::shared_ptr<FormatElement> FormatTableAppendExpression(std::shared_ptr<LuaAstNode> expression);
134140

135-
// special handle range format
136-
std::shared_ptr<FormatElement> FormatRangeBlock(std::shared_ptr<LuaAstNode> blockNode, LuaFormatRange& validRange);
137-
138141
private:
139142
std::shared_ptr<LuaParser> _parser;
140143
LuaCodeStyleOptions& _options;

lua.template.editorconfig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ end_of_line = auto
5959
#
6060
detect_end_of_line = false
6161

62+
space_before_function_open_parenthesis = false
63+
64+
space_before_open_square_bracket = false
65+
66+
# this mean utf8 length , if this is 'unset' then the line width is no longer checked
67+
max_line_length = 120
68+
6269
# [line layout]
6370
# The following configuration supports four expressions
6471
# minLine:${n}
@@ -78,8 +85,7 @@ keep_line_after_expression_statement = keepLine
7885
# [diagnostic]
7986
# the following is code diagnostic options
8087
enable_check_codestyle = true
81-
# this mean utf8 length , if this is 'unset' then the line width is no longer checked
82-
max_line_length = 120
88+
8389
# this will check text end with new line(format always end with new line)
8490
insert_final_newline = true
8591

0 commit comments

Comments
 (0)