File tree Expand file tree Collapse file tree 7 files changed +70
-7
lines changed
Expand file tree Collapse file tree 7 files changed +70
-7
lines changed Original file line number Diff line number Diff line change @@ -267,20 +267,53 @@ std::shared_ptr<LuaAstNode> ast_util::FindLeftIndexExpression(std::shared_ptr<Lu
267267bool 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+ }
Original file line number Diff line number Diff 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" ))
Original file line number Diff line number Diff 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>();
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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+ };
Original file line number Diff line number Diff 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 * 函数定义的参数保持对齐到第一个参数
Original file line number Diff line number Diff 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
2727align_call_args = false
2828
2929
You can’t perform that action at this time.
0 commit comments