Skip to content

Commit 590f18d

Browse files
committed
support more call_arg_parentheses option
1 parent da71a27 commit 590f18d

File tree

17 files changed

+181
-40
lines changed

17 files changed

+181
-40
lines changed

CodeService/src/AstUtil.cpp

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,102 @@ bool ast_util::IsSingleStringOrTableArg(std::shared_ptr<LuaAstNode> callArgList)
4343
return false;
4444
}
4545

46+
bool ast_util::IsSingleStringArg(std::shared_ptr<LuaAstNode> callArgList)
47+
{
48+
if (!callArgList)
49+
{
50+
return false;
51+
}
52+
53+
for (auto child : callArgList->GetChildren())
54+
{
55+
switch (child->GetType())
56+
{
57+
case LuaAstNodeType::StringLiteralExpression:
58+
{
59+
return true;
60+
}
61+
case LuaAstNodeType::ExpressionList:
62+
{
63+
auto expressionList = child;
64+
if (expressionList->GetChildren().empty()
65+
|| expressionList->FindFirstOf(LuaAstNodeType::GeneralOperator))
66+
{
67+
return false;
68+
}
69+
70+
auto expression = expressionList->GetChildren().front();
71+
if (expression->FindFirstOf(LuaAstNodeType::StringLiteralExpression))
72+
{
73+
return true;
74+
}
75+
break;
76+
}
77+
default:
78+
{
79+
break;
80+
}
81+
}
82+
}
83+
84+
return false;
85+
}
86+
87+
bool ast_util::IsSingleTableArg(std::shared_ptr<LuaAstNode> callArgList)
88+
{
89+
if (!callArgList)
90+
{
91+
return false;
92+
}
93+
94+
for (auto child : callArgList->GetChildren())
95+
{
96+
switch (child->GetType())
97+
{
98+
case LuaAstNodeType::TableExpression:
99+
{
100+
return true;
101+
}
102+
case LuaAstNodeType::ExpressionList:
103+
{
104+
auto expressionList = child;
105+
if (expressionList->GetChildren().empty()
106+
|| expressionList->FindFirstOf(LuaAstNodeType::GeneralOperator))
107+
{
108+
return false;
109+
}
110+
111+
auto expression = expressionList->GetChildren().front();
112+
if (expression->FindFirstOf(LuaAstNodeType::TableExpression))
113+
{
114+
return true;
115+
}
116+
break;
117+
}
118+
default:
119+
{
120+
break;
121+
}
122+
}
123+
}
124+
125+
return false;
126+
}
127+
46128
bool ast_util::WillCallArgHaveParentheses(std::shared_ptr<LuaAstNode> callArgList,
47129
CallArgParentheses callArgParentheses)
48130
{
49131
if (ast_util::IsSingleStringOrTableArg(callArgList))
50132
{
51-
if (callArgParentheses == CallArgParentheses::Remove)
133+
if (IsSingleStringArg(callArgList) && callArgParentheses == CallArgParentheses::RemoveStringLiteralOnly)
134+
{
135+
return false;
136+
}
137+
else if (IsSingleTableArg(callArgList) && callArgParentheses == CallArgParentheses::RemoveTableOnly)
138+
{
139+
return false;
140+
}
141+
else if (callArgParentheses == CallArgParentheses::Remove)
52142
{
53143
return false;
54144
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
219219
{
220220
options->call_arg_parentheses = CallArgParentheses::Remove;
221221
}
222+
else if(configMap.at("call_arg_parentheses") == "remove_string_only")
223+
{
224+
options->call_arg_parentheses = CallArgParentheses::RemoveStringLiteralOnly;
225+
}
226+
else if(configMap.at("call_arg_parentheses") == "remove_table_only")
227+
{
228+
options->call_arg_parentheses = CallArgParentheses::RemoveTableOnly;
229+
}
222230
}
223231

224232
if (configMap.count("continuation_indent_size")

CodeService/src/LuaFormatter.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,24 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlock(std::shared_ptr<LuaAstN
324324
}
325325
case LuaAstNodeType::Comment:
326326
{
327-
auto comment = FormatNode(statement);
328-
auto commentStatement = std::make_shared<StatementElement>();
329-
commentStatement->AddChild(comment);
330-
indentEnv->AddChild(commentStatement);
331-
332-
indentEnv->Add<KeepLineElement>();
327+
auto last = indentEnv->LastValidElement();
328+
if (last && _parser->GetLine(last->GetTextRange().EndOffset)
329+
== _parser->GetLine(statement->GetTextRange().StartOffset))
330+
{
331+
if (!last->GetChildren().empty() && last->GetChildren().back()->HasValidTextRange())
332+
{
333+
last->Add<KeepBlankElement>(1);
334+
}
335+
last->AddChild(FormatComment(statement));
336+
}
337+
else
338+
{
339+
auto comment = FormatComment(statement);
340+
auto commentStatement = std::make_shared<StatementElement>();
341+
commentStatement->AddChild(comment);
342+
indentEnv->AddChild(commentStatement);
343+
indentEnv->Add<KeepLineElement>();
344+
}
333345
break;
334346
}
335347
case LuaAstNodeType::BreakStatement:
@@ -830,7 +842,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatWhileStatement(std::shared_pt
830842
{
831843
bool singleLine = false;
832844
env->AddChild(FormatNodeAndBlockOrEnd(it, singleLine, children));
833-
env->Add<KeepLineElement>();
845+
env->Add<KeepElement>(1);
834846
}
835847
else if (child->GetText() == "while")
836848
{
@@ -1133,8 +1145,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
11331145
const auto& children = callArgList->GetChildren();
11341146

11351147
std::vector<std::shared_ptr<LuaAstNode>> argList;
1136-
if (ast_util::IsSingleStringOrTableArg(callArgList)
1137-
&& _options.call_arg_parentheses == CallArgParentheses::Remove)
1148+
if ((ast_util::IsSingleStringOrTableArg(callArgList)
1149+
&& _options.call_arg_parentheses == CallArgParentheses::Remove)
1150+
|| (_options.call_arg_parentheses == CallArgParentheses::RemoveTableOnly
1151+
&& ast_util::IsSingleTableArg(callArgList))
1152+
|| (_options.call_arg_parentheses == CallArgParentheses::RemoveStringLiteralOnly
1153+
&& ast_util::IsSingleStringArg(callArgList)))
11381154
{
11391155
for (auto child : children)
11401156
{
@@ -2136,10 +2152,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt
21362152
bool needSpace = true;
21372153
if (currentCallArgList && nextCallArgList)
21382154
{
2139-
bool currentHas = ast_util::WillCallArgHaveParentheses(currentCallArgList, _options.call_arg_parentheses);
2155+
bool currentHas = ast_util::WillCallArgHaveParentheses(
2156+
currentCallArgList, _options.call_arg_parentheses);
21402157
bool nextHas = ast_util::WillCallArgHaveParentheses(nextCallArgList, _options.call_arg_parentheses);
21412158

2142-
if(currentHas && nextHas)
2159+
if (currentHas && nextHas)
21432160
{
21442161
needSpace = false;
21452162
}

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ quote_style = double
149149
call_arg_parentheses = remove
150150
[{call_arg_parentheses-eq-keep.lua}]
151151
call_arg_parentheses = keep
152+
[{call_arg_parentheses-eq-remove_table_only.lua}]
153+
call_arg_parentheses = remove_table_only
154+
[{call_arg_parentheses-eq-remove_string_only.lua}]
155+
call_arg_parentheses = remove_string_only
152156
[{minLine-eq-1.lua}]
153157
keep_line_after_if_statement = minLine:1
154158
keep_line_after_do_statement = minLine:1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local t = f("wfwefw") ({ e1231313 }) ("1e1231313131")("wl;jkfgoiwjgiofw") "wmfoiwjeiofjiow"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local t = f("wfwefw") ({ e1231313 }) ("1e1231313131")("wl;jkfgoiwjgiofw") "wmfoiwjeiofjiow"

Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ quote_style = double
147147
call_arg_parentheses = remove
148148
[{call_arg_parentheses-eq-keep.lua}]
149149
call_arg_parentheses = keep
150+
[{call_arg_parentheses-eq-remove_table_only.lua}]
151+
call_arg_parentheses = remove_table_only
152+
[{call_arg_parentheses-eq-remove_string_only.lua}]
153+
call_arg_parentheses = remove_string_only
150154
[{minLine-eq-1.lua}]
151155
keep_line_after_if_statement = minLine:1
152156
keep_line_after_do_statement = minLine:1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local t = f "wfwefw" ({ e1231313 }) "1e1231313131" "wl;jkfgoiwjgiofw" "wmfoiwjeiofjiow"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local t = f("wfwefw") { e1231313 } ("1e1231313131")("wl;jkfgoiwjgiofw") "wmfoiwjeiofjiow"

Test/test_script/format_text/wait_format_should_be/table.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local t = {
44
local t2 = { 1, 2, 3, 4, 5 }
55

66
local t3 = { 123131,
7-
aaa, bbbb, ccc, 1, 2, 3, 4, 5
7+
aaa, bbbb, ccc, 1, 2, 3, 4, 5
88
}
99

1010
local t4 = {

0 commit comments

Comments
 (0)