Skip to content

Commit 943da94

Browse files
committed
修复bug,提供大量测试用例
1 parent 58a71a9 commit 943da94

File tree

13 files changed

+223
-5
lines changed

13 files changed

+223
-5
lines changed

CodeService/src/FormatElement/FormatElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TextRange FormatElement::GetTextRange()
2222

2323
void FormatElement::AddChild(std::shared_ptr<FormatElement> child)
2424
{
25-
if (static_cast<int>(child->GetType()) < static_cast<int>(FormatElementType::ControlStart))
25+
if (!child->GetTextRange().IsEmpty())
2626
{
2727
const auto range = child->GetTextRange();
2828
AddTextRange(range);

CodeService/src/LuaFormatter.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlock(std::shared_ptr<LuaAstN
262262
}
263263
case LuaAstNodeType::RepeatStatement:
264264
{
265-
indentEnv->AddChild(FormatNode(statement));
265+
auto child = FormatNode(statement);
266+
indentEnv->AddChild(child);
266267
indentEnv->AddChild(_options.keep_line_after_repeat_statement);
267268
break;
268269
}
@@ -868,7 +869,14 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRepeatStatement(std::shared_p
868869
{
869870
bool singleLine = false;
870871
env->AddChild(FormatNodeAndBlockOrEnd(it, singleLine, children));
871-
env->Add<KeepLineElement>();
872+
if (singleLine)
873+
{
874+
env->Add<KeepElement>(1);
875+
}
876+
else
877+
{
878+
env->Add<KeepLineElement>();
879+
}
872880
}
873881
else
874882
{
@@ -1723,9 +1731,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBinaryExpression(std::shared_
17231731
std::shared_ptr<FormatElement> LuaFormatter::FormatUnaryExpression(std::shared_ptr<LuaAstNode> unaryExpression)
17241732
{
17251733
auto env = std::make_shared<SubExpressionElement>();
1726-
1727-
for (const auto& child : unaryExpression->GetChildren())
1734+
auto& children = unaryExpression->GetChildren();
1735+
for (auto it = children.begin(); it != children.end(); ++it)
17281736
{
1737+
auto child = *it;
17291738
switch (child->GetType())
17301739
{
17311740
case LuaAstNodeType::UnaryOperator:
@@ -1737,6 +1746,13 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatUnaryExpression(std::shared_p
17371746
}
17381747
else
17391748
{
1749+
auto next = nextNode(it, children);
1750+
if (next && (next->GetType() == LuaAstNodeType::UnaryExpression || next->GetType() ==
1751+
LuaAstNodeType::Comment))
1752+
{
1753+
env->Add<KeepElement>(1);
1754+
break;
1755+
}
17401756
env->Add<KeepElement>(0);
17411757
}
17421758
break;

Test/src/CodeFormatTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ bool TestGrammar(std::string input)
3838
parser->GetLine(error.ErrorRange.EndOffset),
3939
parser->GetColumn(error.ErrorRange.EndOffset)
4040
) << std::endl;
41+
return result;
42+
}
43+
44+
auto options = std::make_shared<LuaCodeStyleOptions>();
45+
46+
LuaFormatter formatter(parser, *options);
47+
48+
formatter.BuildFormattedElement();
49+
50+
auto formattedText = formatter.GetFormattedText();
51+
auto formattedParser = LuaParser::LoadFromBuffer(std::move(formattedText));
52+
53+
formattedParser->BuildAstWithComment();
54+
result = formattedParser->GetErrors().empty();
55+
56+
if (!result)
57+
{
58+
for (auto& error : formattedParser->GetErrors())
59+
std::cout << format("error after formatted: {}, from [{},{}] to [{},{}]", error.ErrorMessage,
60+
formattedParser->GetLine(error.ErrorRange.StartOffset),
61+
formattedParser->GetColumn(error.ErrorRange.StartOffset),
62+
formattedParser->GetLine(error.ErrorRange.EndOffset),
63+
formattedParser->GetColumn(error.ErrorRange.EndOffset)
64+
) << std::endl;
4165
}
4266

4367
return result;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
do return end
2+
do print("end") end
3+
do
4+
return x,b,v
5+
end
6+
do
7+
8+
do return end
9+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local function f()
2+
end
3+
local function func()
4+
-- body
5+
end
6+
7+
local function f2() end
8+
9+
local function func() --[[hh]] end
10+
11+
function c2()end
12+
13+
function ccc.bbbb()end
14+
15+
function ttt.bbb:ccc() end
16+
17+
function f3()
18+
local function f() end
19+
20+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
if aaa then end
2+
if aaa+bbb+ccc == 123 then end
3+
4+
if aaa+bbb and
5+
ccc or eee then
6+
local t = 123
7+
if aaa then
8+
end
9+
10+
if ddd then
11+
else --hahh
12+
end
13+
end
14+
if a then
15+
elseif aa then
16+
elseif ccc then
17+
else
18+
end
19+
20+
21+
22+
23+
24+
25+
26+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local a = 123
2+
local c,d=123
3+
local c<const> = 123
4+
local d<close> = {}
5+
ccc=123
6+
ddd = a+b+c+d
7+
eee = b,1321,353
8+
hhh,vvv=123,456
9+
10+
11+
aaa = 123
12+
yyy =2342
13+
mmmmmmm = 098
14+
15+
16+
local f = function()
17+
end
18+
19+
local f2 = function(a,b,c) end
20+
21+
local f3,f4 = function()end,
22+
function (x,y) return x+y end
23+
24+
aaa.bbb = 123
25+
aaa().ccc =1231
26+
27+
yyy,iiii.cccc=123,456
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
repeat
2+
do print() end
3+
until true
4+
repeat
5+
local function f() do return end end
6+
until aa + bbb+ccc
7+
> 123
8+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
do return end
2+
3+
do print("end") end
4+
5+
do
6+
return x, b, v
7+
end
8+
9+
do
10+
11+
do return end
12+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local function f()
2+
end
3+
4+
local function func()
5+
-- body
6+
end
7+
8+
local function f2() end
9+
10+
local function func() --[[hh]] end
11+
12+
function c2() end
13+
14+
function ccc.bbbb() end
15+
16+
function ttt.bbb:ccc() end
17+
18+
function f3()
19+
local function f() end
20+
21+
end

0 commit comments

Comments
 (0)