@@ -652,7 +652,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatDoStatement(std::shared_ptr<L
652652 auto env = std::make_shared<StatementElement>();
653653 auto & children = doStatement->GetChildren ();
654654 int i = 0 ;
655- env->AddChild (FormatNodeAndBlockOrEnd (i, children));
655+ bool singleLine = false ;
656+ env->AddChild (FormatNodeAndBlockOrEnd (i, singleLine, children));
656657 return env;
657658}
658659
@@ -669,7 +670,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatWhileStatement(std::shared_pt
669670 {
670671 if (child->GetText () == " do" )
671672 {
672- env->AddChild (FormatNodeAndBlockOrEnd (i, children));
673+ bool singleLine = false ;
674+ env->AddChild (FormatNodeAndBlockOrEnd (i, singleLine, children));
673675 env->Add <KeepLineElement>();
674676 }
675677 else if (child->GetText () == " while" )
@@ -792,7 +794,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatForBody(std::shared_ptr<LuaAs
792794 {
793795 if (child->GetText () == " do" )
794796 {
795- env->AddChild (FormatNodeAndBlockOrEnd (i, children));
797+ bool singleLine = false ;
798+ env->AddChild (FormatNodeAndBlockOrEnd (i, singleLine, children));
796799 env->Add <KeepLineElement>();
797800 }
798801 else
@@ -843,7 +846,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRepeatStatement(std::shared_p
843846 {
844847 if (child->GetText () == " repeat" )
845848 {
846- env->AddChild (FormatNodeAndBlockOrEnd (i, children));
849+ bool singleLine = false ;
850+ env->AddChild (FormatNodeAndBlockOrEnd (i, singleLine, children));
847851 env->Add <KeepLineElement>();
848852 }
849853 else
@@ -878,15 +882,16 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
878882 {
879883 if (child->GetText () == " then" || child->GetText () == " else" )
880884 {
881- env->AddChild (FormatNodeAndBlockOrEnd (i, children));
882- env->Add <KeepElement>(1 );
885+ bool singleLine = false ;
886+ env->AddChild (FormatNodeAndBlockOrEnd (i, singleLine, children));
887+ env->Add <KeepElement>(1 , !singleLine);
883888 }
884889 else if (child->GetText () == " if" || child->GetText () == " elseif" )
885890 {
886891 env->Add <TextElement>(child);
887892 env->Add <KeepBlankElement>(1 );
888893 }
889- else // 然而end是在 formatkeyAndBlockAndEnd 中完成的
894+ else // 然而end是在 FormatNodeAndBlockOrEnd 中完成的
890895 {
891896 env->Add <TextElement>(child);
892897 }
@@ -1124,7 +1129,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatParamList(std::shared_ptr<Lua
11241129std::shared_ptr<FormatElement> LuaFormatter::FormatFunctionBody (std::shared_ptr<LuaAstNode> functionBody)
11251130{
11261131 int index = 0 ;
1127- return FormatNodeAndBlockOrEnd (index, functionBody->GetChildren ());
1132+ bool singleLine = false ;
1133+ return FormatNodeAndBlockOrEnd (index, singleLine, functionBody->GetChildren ());
11281134}
11291135
11301136
@@ -1480,6 +1486,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(int& currentI
14801486}
14811487
14821488std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd (int & currentIndex,
1489+ bool & singleLineBlock,
14831490 const std::vector<std::shared_ptr<LuaAstNode>>&
14841491 vec)
14851492{
@@ -1505,7 +1512,6 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(int& curren
15051512 bool blockExist = false ;
15061513 auto block = FormatBlockFromParent (currentIndex, vec);
15071514
1508-
15091515 if (!block->GetChildren ().empty ())
15101516 {
15111517 blockExist = true ;
@@ -1518,6 +1524,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(int& curren
15181524 }
15191525 else
15201526 {
1527+ singleLineBlock = true ;
15211528 env->Add <KeepBlankElement>(1 );
15221529
15231530 for (auto blockChild : block->GetChildren ())
@@ -1549,10 +1556,6 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(int& curren
15491556 {
15501557 env->Add <LineElement>();
15511558 }
1552- else
1553- {
1554- env->Add <KeepElement>(1 );
1555- }
15561559 }
15571560 else // 下一个不是关键词那能是什么那就换个行吧
15581561 {
0 commit comments