1818#include " CodeService/FormatElement/SerializeContext.h"
1919#include " CodeService/FormatElement/IndentOnLineBreakElement.h"
2020#include " CodeService/FormatElement/PlaceholderElement.h"
21+ #include " CodeService/FormatElement/AlignIfLayoutElement.h"
2122#include " Util/StringUtil.h"
2223
2324bool nextMatch (LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
@@ -1029,6 +1030,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRepeatStatement(std::shared_p
10291030std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement (std::shared_ptr<LuaAstNode> ifStatement)
10301031{
10311032 auto env = std::make_shared<StatementElement>();
1033+ bool canAlignCondition = false ;
10321034 auto & children = ifStatement->GetChildren ();
10331035
10341036 std::vector<std::shared_ptr<PlaceholderElement>> placeholderExpressions;
@@ -1050,6 +1052,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10501052 {
10511053 env->Add <TextElement>(child);
10521054 env->Add <KeepBlankElement>(1 );
1055+ if (_options.if_condition_align_with_each_other && child->GetText () == " elseif" )
1056+ {
1057+ canAlignCondition = true ;
1058+ }
10531059 }
10541060 else // 然而end是在 FormatNodeAndBlockOrEnd 中完成的
10551061 {
@@ -1060,21 +1066,14 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10601066 }
10611067 case LuaAstNodeType::Expression:
10621068 {
1063- if (_options.if_condition_align_with_each_other )
1069+ std::shared_ptr<FormatElement> expression = nullptr ;
1070+ if (_options.if_condition_no_continuation_indent
1071+ || _options.if_condition_align_with_each_other )
10641072 {
1065- placeholderExpressions.push_back (std::make_shared<PlaceholderElement>(env, child));
1066- env->AddChild (placeholderExpressions.back ());
1073+ expression = std::make_shared<LongExpressionLayoutElement>(0 );
10671074 }
1068- else
1069- {
1070- std::shared_ptr<FormatElement> expression = nullptr ;
1071- if (_options.if_condition_no_continuation_indent )
1072- {
1073- expression = std::make_shared<LongExpressionLayoutElement>(0 );
1074- }
10751075
1076- env->AddChild (FormatExpression (child, expression));
1077- }
1076+ env->AddChild (FormatExpression (child, expression));
10781077 env->Add <KeepBlankElement>(1 );
10791078 break ;
10801079 }
@@ -1086,9 +1085,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10861085 }
10871086 }
10881087
1089- if (!placeholderExpressions. empty () )
1088+ if (canAlignCondition )
10901089 {
1091- FormatIfConditionAlign (placeholderExpressions);
1090+ auto ifAlignLayout = std::make_shared<AlignIfElement>();
1091+ ifAlignLayout->CopyFrom (env);
1092+ env->Reset ();
1093+ env->AddChild (ifAlignLayout);
10921094 }
10931095
10941096 return env;
@@ -2171,20 +2173,6 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableAppendExpression(std::sh
21712173 }
21722174
21732175 return FormatExpression (expression, env);
2174- }
2175-
2176- void LuaFormatter::FormatIfConditionAlign (std::vector<std::shared_ptr<PlaceholderElement>>& placeholders)
2177- {
2178- bool canAlign = true ;
2179- for (auto placeholder: placeholders)
2180- {
2181-
2182- }
2183-
2184-
2185-
2186-
2187-
21882176}
21892177
21902178std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock (std::shared_ptr<LuaAstNode> blockNode,
0 commit comments