Skip to content

Commit ff3e11d

Browse files
committed
临时修复一个算法不稳定的bug
1 parent e3b30b0 commit ff3e11d

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

CodeService/src/LuaFormatter.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
11241129
std::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

14821488
std::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
{

include/CodeService/LuaFormatter.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,24 @@ class LuaFormatter
9090

9191
// 以下是特殊格式化规则
9292
// 规则1 连续赋值语句会试图检测并对齐等号
93-
std::shared_ptr<FormatElement> FormatAlignStatement(int& currentIndex,const std::vector<std::shared_ptr<LuaAstNode>>& vec);
93+
std::shared_ptr<FormatElement> FormatAlignStatement(int& currentIndex,
94+
const std::vector<std::shared_ptr<LuaAstNode>>& vec);
9495

95-
std::shared_ptr<FormatElement> FormatAlignTableField(int& currentIndex,const std::vector<std::shared_ptr<LuaAstNode>>& vec);
96+
std::shared_ptr<FormatElement> FormatAlignTableField(int& currentIndex,
97+
const std::vector<std::shared_ptr<LuaAstNode>>& vec);
9698
//意思其实是格式化任意节点加上Block和可选的接受一个end
97-
std::shared_ptr<FormatElement> FormatNodeAndBlockOrEnd(int& currentIndex,const std::vector<std::shared_ptr<LuaAstNode>>& vec);
99+
std::shared_ptr<FormatElement> FormatNodeAndBlockOrEnd(int& currentIndex,
100+
bool& singleLineBlock,
101+
const std::vector<std::shared_ptr<LuaAstNode>>& vec);
98102
//当前索引必须是已经消耗过的索引
99-
std::shared_ptr<FormatElement> FormatBlockFromParent(int& currentIndex,const std::vector<std::shared_ptr<LuaAstNode>>& vec);
103+
std::shared_ptr<FormatElement> FormatBlockFromParent(int& currentIndex,
104+
const std::vector<std::shared_ptr<LuaAstNode>>& vec);
100105

101106
// 以下是表达式相关,表达式会联合布局
102107
void FormatSubExpressionNode(std::shared_ptr<LuaAstNode> expression, std::shared_ptr<FormatElement> env);
103108

104-
std::shared_ptr<FormatElement> FormatExpression(std::shared_ptr<LuaAstNode> expression, std::shared_ptr<FormatElement> env = nullptr);
109+
std::shared_ptr<FormatElement> FormatExpression(std::shared_ptr<LuaAstNode> expression,
110+
std::shared_ptr<FormatElement> env = nullptr);
105111

106112
std::shared_ptr<FormatElement> FormatBinaryExpression(std::shared_ptr<LuaAstNode> binaryExpression);
107113

@@ -113,24 +119,7 @@ class LuaFormatter
113119

114120
std::shared_ptr<FormatElement> FormatCallExpression(std::shared_ptr<LuaAstNode> callExpression);
115121
private:
116-
117122
std::shared_ptr<LuaParser> _parser;
118123
LuaCodeStyleOptions& _options;
119124
std::shared_ptr<FormatElement> _env;
120125
};
121-
122-
123-
124-
125-
126-
127-
128-
129-
130-
131-
132-
133-
134-
135-
136-

0 commit comments

Comments
 (0)