Skip to content

Commit 457910d

Browse files
committed
Merge branch 'master' of github.com:CppCXY/EmmyLuaCodeStyle
2 parents 44b40eb + 1289f3f commit 457910d

File tree

7 files changed

+70
-7
lines changed

7 files changed

+70
-7
lines changed

CodeService/src/AstUtil.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,20 +267,53 @@ std::shared_ptr<LuaAstNode> ast_util::FindLeftIndexExpression(std::shared_ptr<Lu
267267
bool ast_util::WillIndexExpressionFormatError(std::shared_ptr<LuaAstNode> expression)
268268
{
269269
auto text = expression->GetText();
270-
if(text.empty())
270+
if (text.empty())
271271
{
272272
return false;
273273
}
274274

275-
if(text.front() == '[')
275+
if (text.front() == '[')
276276
{
277277
return text.length() > 2 && (text[1] == '[' || text[1] == '=');
278278
}
279279

280-
if(text.back() == ']')
280+
if (text.back() == ']')
281281
{
282282
return text.length() > 2 && (text[text.length() - 2] == ']' || text[text.length() - 2] == '=');
283283
}
284284

285285
return false;
286286
}
287+
288+
bool ast_util::IsNodeAfterMoreIndentionStatement(std::shared_ptr<LuaAstNode> node)
289+
{
290+
if (!node)
291+
{
292+
return false;
293+
}
294+
295+
std::shared_ptr<LuaAstNode> parent = node->GetParent();
296+
while (parent)
297+
{
298+
switch (parent->GetType())
299+
{
300+
case LuaAstNodeType::LocalStatement:
301+
case LuaAstNodeType::AssignStatement:
302+
case LuaAstNodeType::ReturnStatement:
303+
{
304+
return true;
305+
}
306+
case LuaAstNodeType::Block:
307+
{
308+
return false;
309+
}
310+
default:
311+
{
312+
break;
313+
}
314+
}
315+
parent = parent->GetParent();
316+
}
317+
318+
return false;
319+
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,19 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
263263

264264
if (configMap.count("align_call_args"))
265265
{
266-
options->align_call_args = configMap.at("align_call_args") == "true";
266+
auto& value = configMap.at("align_call_args");
267+
if(value == "true")
268+
{
269+
options->align_call_args = AlignCallArgs::True;
270+
}
271+
else if(value == "false")
272+
{
273+
options->align_call_args = AlignCallArgs::False;
274+
}
275+
else if(value == "only_after_more_indention_statement")
276+
{
277+
options->align_call_args = AlignCallArgs::OnlyAfterMoreIndentionStatement;
278+
}
267279
}
268280

269281
if (configMap.count("align_chained_expression_statement"))

CodeService/src/LuaFormatter.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
12451245
case LuaAstNodeType::ExpressionList:
12461246
{
12471247
std::shared_ptr<FormatElement> layout = nullptr;
1248-
if (_options.align_call_args)
1248+
if (_options.align_call_args != AlignCallArgs::False)
12491249
{
12501250
bool canAligned = true;
12511251
// 但是如果表达式列表中出现跨行表达式,则采用长表达式对齐
@@ -1272,6 +1272,14 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
12721272
}
12731273
}
12741274

1275+
if(canAligned
1276+
&& _options.align_call_args == AlignCallArgs::OnlyAfterMoreIndentionStatement
1277+
&& (!ast_util::IsNodeAfterMoreIndentionStatement(child))
1278+
)
1279+
{
1280+
canAligned = false;
1281+
}
1282+
12751283
if (canAligned)
12761284
{
12771285
layout = std::make_shared<AlignToFirstElement>();

include/CodeService/AstUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ namespace ast_util
1818
std::shared_ptr<LuaAstNode> FindLeftIndexExpression(std::shared_ptr<LuaAstNode> expression);
1919

2020
bool WillIndexExpressionFormatError(std::shared_ptr<LuaAstNode> expression);
21+
22+
bool IsNodeAfterMoreIndentionStatement(std::shared_ptr<LuaAstNode> node);
2123
}

include/CodeService/LuaCodeStyleEnum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ enum class CallArgParentheses : int
2222
UnambiguousRemoveStringOnly
2323
};
2424

25+
enum class AlignCallArgs
26+
{
27+
True,
28+
False,
29+
OnlyAfterMoreIndentionStatement
30+
};

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class LuaCodeStyleOptions
5252
/*
5353
* 调用参数对齐到第一个参数
5454
*/
55-
bool align_call_args = false;
55+
AlignCallArgs align_call_args = AlignCallArgs::False;
56+
57+
bool only_align_call_args_after_assignment = false;
5658

5759
/*
5860
* 函数定义的参数保持对齐到第一个参数

lua.template.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ keep_one_space_between_namedef_and_attribute = true
2323

2424
# [function]
2525
# function call expression's args will align to first arg
26-
# however, if the args has cross row arg, it will not be aligned to the first arg
26+
# optional true/false/only_after_more_indention_statement
2727
align_call_args = false
2828

2929

0 commit comments

Comments
 (0)