Skip to content

Commit 9f1f1b4

Browse files
committed
resolve #16
1 parent 10743f2 commit 9f1f1b4

25 files changed

+229
-169
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@
3030
.vs
3131
out
3232
3rd/uriparser/src/config.h
33+
.idea
34+
.vscode

.idea/.gitignore

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/EmmyLuaCodeStyle.iml

Lines changed: 0 additions & 9 deletions
This file was deleted.

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/vcs.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

CodeService/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ target_sources(CodeService
6666
${CodeService_SOURCE_DIR}/src/NameStyle/NameStyleChecker.cpp
6767
${CodeService_SOURCE_DIR}/src/NameStyle/NameStyleRuleMatcher.cpp
6868
${CodeService_SOURCE_DIR}/src/NameStyle/CheckElement.cpp
69-
69+
${CodeService_SOURCE_DIR}/src/AstUtil.cpp
7070
)
7171

7272
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")

CodeService/src/AstUtil.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "CodeService/AstUtil.h"
2+
3+
bool ast_util::IsSingleStringOrTableArg(std::shared_ptr<LuaAstNode> callArgList)
4+
{
5+
if (!callArgList)
6+
{
7+
return false;
8+
}
9+
10+
for (auto child : callArgList->GetChildren())
11+
{
12+
switch (child->GetType())
13+
{
14+
case LuaAstNodeType::StringLiteralExpression:
15+
case LuaAstNodeType::TableExpression:
16+
{
17+
return true;
18+
}
19+
case LuaAstNodeType::ExpressionList:
20+
{
21+
auto expressionList = child;
22+
if (expressionList->GetChildren().empty()
23+
|| expressionList->FindFirstOf(LuaAstNodeType::GeneralOperator))
24+
{
25+
return false;
26+
}
27+
28+
auto expression = expressionList->GetChildren().front();
29+
if (expression->FindFirstOf(LuaAstNodeType::StringLiteralExpression)
30+
|| expression->FindFirstOf(LuaAstNodeType::TableExpression))
31+
{
32+
return true;
33+
}
34+
break;
35+
}
36+
default:
37+
{
38+
break;
39+
}
40+
}
41+
}
42+
43+
return false;
44+
}
45+
46+
bool ast_util::WillCallArgHaveParentheses(std::shared_ptr<LuaAstNode> callArgList,
47+
CallArgParentheses callArgParentheses)
48+
{
49+
if (ast_util::IsSingleStringOrTableArg(callArgList))
50+
{
51+
if (callArgParentheses == CallArgParentheses::Remove)
52+
{
53+
return false;
54+
}
55+
else
56+
{
57+
auto leftParentheses = callArgList->FindFirstOf(LuaAstNodeType::GeneralOperator);
58+
if (leftParentheses && leftParentheses->GetText() == "(")
59+
{
60+
return true;
61+
}
62+
else
63+
{
64+
return false;
65+
}
66+
}
67+
}
68+
69+
return true;
70+
}

CodeService/src/LuaCodeStyleOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LuaCodeStyleOptions::LuaCodeStyleOptions()
3232

3333
local_name_define_style(std::make_shared<NameStyleRuleMatcher>("Local")),
3434
function_param_name_style(std::make_shared<NameStyleRuleMatcher>("Parameters")),
35-
function_name_define_style(std::make_shared<NameStyleRuleMatcher>("Class method")),
35+
function_name_define_style(std::make_shared<NameStyleRuleMatcher>("Class and module method")),
3636
local_function_name_define_style(std::make_shared<NameStyleRuleMatcher>("Local method")),
3737
table_field_name_define_style(std::make_shared<NameStyleRuleMatcher>("Table field")),
3838
global_variable_name_define_style(std::make_shared<NameStyleRuleMatcher>("Global")),

CodeService/src/LuaEditorConfig.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
209209
}
210210
}
211211

212+
if(configMap.count("call_arg_parentheses"))
213+
{
214+
if(configMap.at("call_arg_parentheses") == "keep")
215+
{
216+
options->call_arg_parentheses = CallArgParentheses::Keep;
217+
}
218+
else if(configMap.at("call_arg_parentheses") == "remove")
219+
{
220+
options->call_arg_parentheses = CallArgParentheses::Remove;
221+
}
222+
}
223+
212224
if (configMap.count("continuation_indent_size")
213225
&& isNumber(configMap.at("continuation_indent_size")))
214226
{
@@ -232,12 +244,6 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
232244
options->align_call_args = configMap.at("align_call_args") == "true";
233245
}
234246

235-
if (configMap.count("keep_one_space_between_call_args_and_parentheses"))
236-
{
237-
options->keep_one_space_between_call_args_and_parentheses =
238-
configMap.at("keep_one_space_between_call_args_and_parentheses") == "true";
239-
}
240-
241247
if (configMap.count("keep_one_space_between_table_and_bracket"))
242248
{
243249
options->keep_one_space_between_table_and_bracket =

CodeService/src/LuaFormatter.cpp

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "CodeService/FormatElement/MaxSpaceElement.h"
2323
#include "CodeService/FormatElement/StringLiteralElement.h"
2424
#include "Util/StringUtil.h"
25+
#include "CodeService/AstUtil.h"
2526

2627
bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
2728
{
@@ -1129,17 +1130,29 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionStatement(std::shar
11291130
std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<LuaAstNode> callArgList)
11301131
{
11311132
auto env = std::make_shared<ExpressionElement>();
1132-
auto& children = callArgList->GetChildren();
1133-
for (auto child : children)
1133+
const auto& children = callArgList->GetChildren();
1134+
1135+
std::vector<std::shared_ptr<LuaAstNode>> argList;
1136+
if (ast_util::IsSingleStringOrTableArg(callArgList)
1137+
&& _options.call_arg_parentheses == CallArgParentheses::Remove)
11341138
{
1135-
switch (child->GetType())
1139+
for (auto child : children)
11361140
{
1137-
case LuaAstNodeType::KeyWord:
1141+
if (child->GetType() != LuaAstNodeType::GeneralOperator)
11381142
{
1139-
env->AddChild(FormatNode(child));
1140-
env->Add<KeepElement>(0);
1141-
break;
1143+
argList.push_back(child);
11421144
}
1145+
}
1146+
}
1147+
else
1148+
{
1149+
argList = children;
1150+
}
1151+
1152+
for (auto child : argList)
1153+
{
1154+
switch (child->GetType())
1155+
{
11431156
case LuaAstNodeType::ExpressionList:
11441157
{
11451158
std::shared_ptr<FormatElement> layout = nullptr;
@@ -1169,35 +1182,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
11691182
}
11701183

11711184
env->AddChild(FormatExpressionList(child, layout));
1172-
1173-
if (_options.keep_one_space_between_call_args_and_parentheses)
1174-
{
1175-
env->Add<KeepElement>(1);
1176-
}
1177-
else
1178-
{
1179-
env->Add<KeepElement>(0);
1180-
}
1181-
1185+
env->Add<KeepElement>(0);
11821186
break;
11831187
}
11841188
case LuaAstNodeType::GeneralOperator:
11851189
{
11861190
env->Add<TextElement>(child);
1187-
if (child->GetText() == ",")
1188-
{
1189-
env->Add<KeepElement>(1);
1190-
}
1191-
else if (child->GetText() == "("
1192-
&& _options.keep_one_space_between_call_args_and_parentheses
1193-
&& children.size() > 2)
1194-
{
1195-
env->Add<KeepElement>(1);
1196-
}
1197-
else
1198-
{
1199-
env->Add<KeepElement>(0);
1200-
}
1191+
env->Add<KeepElement>(0);
12011192
break;
12021193
}
12031194
default:
@@ -2120,7 +2111,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt
21202111
auto next = nextNode(it, children);
21212112
if (next && next->GetType() == LuaAstNodeType::CallArgList)
21222113
{
2123-
if (next->GetChildren().size() == 1)
2114+
if (!ast_util::WillCallArgHaveParentheses(next, _options.call_arg_parentheses))
21242115
{
21252116
env->Add<KeepElement>(1);
21262117
}
@@ -2139,16 +2130,27 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt
21392130
case LuaAstNodeType::CallExpression:
21402131
{
21412132
env->AddChild(FormatNode(child));
2142-
auto callArg = child->FindFirstOf(LuaAstNodeType::CallArgList);
2143-
if (callArg->GetChildren().size() <= 1)
2133+
2134+
auto currentCallArgList = child->FindFirstOf(LuaAstNodeType::CallArgList);
2135+
auto nextCallArgList = nextNode(it, children);
2136+
bool needSpace = true;
2137+
if (currentCallArgList && nextCallArgList)
21442138
{
2145-
env->Add<KeepElement>(1);
2139+
bool currentHas = ast_util::WillCallArgHaveParentheses(currentCallArgList, _options.call_arg_parentheses);
2140+
bool nextHas = ast_util::WillCallArgHaveParentheses(nextCallArgList, _options.call_arg_parentheses);
2141+
2142+
if(currentHas && nextHas)
2143+
{
2144+
needSpace = false;
2145+
}
21462146
}
21472147
else
21482148
{
2149-
env->Add<KeepElement>(0);
2149+
needSpace = false;
21502150
}
21512151

2152+
env->Add<KeepElement>(needSpace ? 1 : 0);
2153+
21522154
break;
21532155
}
21542156
case LuaAstNodeType::CallArgList:

0 commit comments

Comments
 (0)