Skip to content

Commit 66a38c0

Browse files
committed
support unambiguous_remove_string_only
1 parent b124c4a commit 66a38c0

File tree

12 files changed

+126
-28
lines changed

12 files changed

+126
-28
lines changed

CodeService/src/AstUtil.cpp

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,37 @@ bool ast_util::IsSingleStringArg(std::shared_ptr<LuaAstNode> callArgList)
8484
return false;
8585
}
8686

87+
88+
bool ast_util::IsSingleStringArgUnambiguous(std::shared_ptr<LuaAstNode> callArgList)
89+
{
90+
bool isSingleStringArg = IsSingleStringArg(callArgList);
91+
if (!isSingleStringArg)
92+
{
93+
return false;
94+
}
95+
96+
auto callExpression = callArgList->GetParent();
97+
if (!callExpression)
98+
{
99+
return false;
100+
}
101+
if (callExpression->GetType() != LuaAstNodeType::CallExpression)
102+
{
103+
return false;
104+
}
105+
106+
auto indexExpression = callExpression->GetParent();
107+
if (indexExpression && indexExpression->GetType() == LuaAstNodeType::IndexExpression)
108+
{
109+
if (!indexExpression->GetChildren().empty() && callExpression == indexExpression->GetChildren().front())
110+
{
111+
return false;
112+
}
113+
}
114+
115+
return true;
116+
}
117+
87118
bool ast_util::IsSingleTableArg(std::shared_ptr<LuaAstNode> callArgList)
88119
{
89120
if (!callArgList)
@@ -130,29 +161,50 @@ bool ast_util::WillCallArgHaveParentheses(std::shared_ptr<LuaAstNode> callArgLis
130161
{
131162
if (ast_util::IsSingleStringOrTableArg(callArgList))
132163
{
133-
if (IsSingleStringArg(callArgList) && callArgParentheses == CallArgParentheses::RemoveStringLiteralOnly)
164+
switch (callArgParentheses)
134165
{
135-
return false;
136-
}
137-
else if (IsSingleTableArg(callArgList) && callArgParentheses == CallArgParentheses::RemoveTableOnly)
138-
{
139-
return false;
166+
case CallArgParentheses::Remove:
167+
{
168+
return false;
169+
}
170+
case CallArgParentheses::RemoveStringOnly:
171+
{
172+
if(IsSingleStringArg(callArgList))
173+
{
174+
return false;
175+
}
176+
break;
177+
}
178+
case CallArgParentheses::RemoveTableOnly:
179+
{
180+
if(IsSingleTableArg(callArgList))
181+
{
182+
return false;
183+
}
184+
break;
185+
}
186+
case CallArgParentheses::UnambiguousRemoveStringOnly:
187+
{
188+
if(IsSingleStringArgUnambiguous(callArgList))
189+
{
190+
return false;
191+
}
192+
break;
193+
}
194+
default:
195+
{
196+
break;
197+
}
140198
}
141-
else if (callArgParentheses == CallArgParentheses::Remove)
199+
200+
auto leftParentheses = callArgList->FindFirstOf(LuaAstNodeType::GeneralOperator);
201+
if (leftParentheses && leftParentheses->GetText() == "(")
142202
{
143-
return false;
203+
return true;
144204
}
145205
else
146206
{
147-
auto leftParentheses = callArgList->FindFirstOf(LuaAstNodeType::GeneralOperator);
148-
if (leftParentheses && leftParentheses->GetText() == "(")
149-
{
150-
return true;
151-
}
152-
else
153-
{
154-
return false;
155-
}
207+
return false;
156208
}
157209
}
158210

CodeService/src/LuaEditorConfig.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,26 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
211211

212212
if(configMap.count("call_arg_parentheses"))
213213
{
214-
if(configMap.at("call_arg_parentheses") == "keep")
214+
if (configMap.at("call_arg_parentheses") == "keep")
215215
{
216216
options->call_arg_parentheses = CallArgParentheses::Keep;
217217
}
218-
else if(configMap.at("call_arg_parentheses") == "remove")
218+
else if (configMap.at("call_arg_parentheses") == "remove")
219219
{
220220
options->call_arg_parentheses = CallArgParentheses::Remove;
221221
}
222-
else if(configMap.at("call_arg_parentheses") == "remove_string_only")
222+
else if (configMap.at("call_arg_parentheses") == "remove_string_only")
223223
{
224-
options->call_arg_parentheses = CallArgParentheses::RemoveStringLiteralOnly;
224+
options->call_arg_parentheses = CallArgParentheses::RemoveStringOnly;
225225
}
226-
else if(configMap.at("call_arg_parentheses") == "remove_table_only")
226+
else if (configMap.at("call_arg_parentheses") == "remove_table_only")
227227
{
228228
options->call_arg_parentheses = CallArgParentheses::RemoveTableOnly;
229229
}
230+
else if (configMap.at("call_arg_parentheses") == "unambiguous_remove_string_only")
231+
{
232+
options->call_arg_parentheses = CallArgParentheses::UnambiguousRemoveStringOnly;
233+
}
230234
}
231235

232236
if (configMap.count("continuation_indent_size")

CodeService/src/LuaFormatter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
11501150
&& _options.call_arg_parentheses == CallArgParentheses::Remove)
11511151
|| (_options.call_arg_parentheses == CallArgParentheses::RemoveTableOnly
11521152
&& ast_util::IsSingleTableArg(callArgList))
1153-
|| (_options.call_arg_parentheses == CallArgParentheses::RemoveStringLiteralOnly
1154-
&& ast_util::IsSingleStringArg(callArgList)))
1153+
|| (_options.call_arg_parentheses == CallArgParentheses::RemoveStringOnly
1154+
&& ast_util::IsSingleStringArg(callArgList))
1155+
|| (_options.call_arg_parentheses == CallArgParentheses::UnambiguousRemoveStringOnly
1156+
&& ast_util::IsSingleStringArgUnambiguous(callArgList)))
11551157
{
11561158
for (auto child : children)
11571159
{

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ call_arg_parentheses = keep
153153
call_arg_parentheses = remove_table_only
154154
[{call_arg_parentheses-eq-remove_string_only.lua}]
155155
call_arg_parentheses = remove_string_only
156+
[{call_arg_parentheses-eq-unambiguous_remove_string_only.lua}]
157+
call_arg_parentheses = unambiguous_remove_string_only
156158
[{minLine-eq-1.lua}]
157159
keep_line_after_if_statement = minLine:1
158160
keep_line_after_do_statement = minLine:1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
local t = require("aaaaa").mod
2+
local b = require("bbbbb")
3+
local c = require "ddddd"
4+
p("okwjgfiowjiofw").aa = 13

Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ call_arg_parentheses = keep
151151
call_arg_parentheses = remove_table_only
152152
[{call_arg_parentheses-eq-remove_string_only.lua}]
153153
call_arg_parentheses = remove_string_only
154+
[{call_arg_parentheses-eq-unambiguous_remove_string_only.lua}]
155+
call_arg_parentheses = unambiguous_remove_string_only
154156
[{minLine-eq-1.lua}]
155157
keep_line_after_if_statement = minLine:1
156158
keep_line_after_do_statement = minLine:1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
local t = require("aaaaa").mod
2+
local b = require "bbbbb"
3+
local c = require "ddddd"
4+
p("okwjgfiowjiofw").aa = 13

docs/format_config.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ end
7070
## call_arg_parentheses
7171

7272
该选项表示,调用表达式中如果参数仅有单字符串字面值常量或者表表达式,则可以考虑保持或者移除括号。
73-
该选项可选值是 keep/remove/remove_table_only/remove_string_only 默认值是keep。
73+
该选项可选值是 keep/remove/remove_table_only/remove_string_only/unambiguous_remove_string_only 默认值是keep。
7474

7575
```lua
7676
f("wfwefw")({ e1231313 })("1e1231313131")
@@ -91,6 +91,19 @@ f("wfwefw") { e1231313 } ("1e1231313131")
9191
f "wfwefw" ({ e1231313 }) "1e1231313131"
9292
```
9393

94+
如果值为unambiguous_remove_string_only则不存在歧义的时候会移除调用表达式中字符串的括号,例如:
95+
```lua
96+
local t = require("aaaa").bbb
97+
local d = require("cccc")
98+
```
99+
会被格式化为:
100+
```lua
101+
local t = require("aaaa").bbb
102+
local d = require "cccc"
103+
```
104+
105+
106+
94107
## continuation_indent_size
95108

96109
该选项表示长表达式或者表达式列表在换行表达时的后续缩进列数,该选项默认值是4,常见的还有8

docs/format_config_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ f("wfwefw") { e1231313 } ("1e1231313131")
9090
f "wfwefw" ({ e1231313 }) "1e1231313131"
9191
```
9292

93+
If the value is unambiguous_remove_string_only, the parentheses of the string in the call expression will be removed when there is no ambiguity, for example:
94+
```lua
95+
local t = require("aaaa").bbb
96+
local d = require("cccc")
97+
```
98+
will be formatted as:
99+
```lua
100+
local t = require("aaaa").bbb
101+
local d = require "cccc"
102+
```
103+
104+
93105
## continuation_indent_size
94106

95107
This option indicates the number of subsequent indented columns when a long expression or expression list is expressed in a new line. The default value of this option is 4, and 8 is common

include/CodeService/AstUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace ast_util
99

1010
bool IsSingleStringArg(std::shared_ptr<LuaAstNode> callArgList);
1111

12+
bool IsSingleStringArgUnambiguous(std::shared_ptr<LuaAstNode> callArgList);
13+
1214
bool IsSingleTableArg(std::shared_ptr<LuaAstNode> callArgList);
1315

1416
bool WillCallArgHaveParentheses(std::shared_ptr<LuaAstNode> callArgList, CallArgParentheses callArgParentheses);

0 commit comments

Comments
 (0)