Skip to content

Commit c88a84f

Browse files
committed
修复索引表达式中特殊情况下的格式化问题
1 parent ffd2205 commit c88a84f

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

CodeService/src/LuaFormatter.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,21 +1618,21 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
16181618
}
16191619
case LuaAstNodeType::IndexOperator:
16201620
{
1621-
if(child->GetTokenType() == '[')
1621+
if (child->GetTokenType() == '[')
16221622
{
16231623
auto nextNode = NextNode(it, children);
1624-
if(nextNode && nextNode->GetType() == LuaAstNodeType::Expression)
1624+
if (nextNode && nextNode->GetType() == LuaAstNodeType::Expression
1625+
&& (StringUtil::StartWith(nextNode->GetText(), "[")
1626+
|| StringUtil::EndWith(nextNode->GetText(), "]"))
1627+
)
16251628
{
1626-
auto stringExpr = nextNode->FindFirstOf(LuaAstNodeType::StringLiteralExpression);
1627-
if (stringExpr && StringUtil::StartWith(stringExpr->GetText(), "[")) {
1628-
isIndexExprLongString = true;
1629-
env->Add<TextElement>(child);
1630-
env->Add<KeepElement>(1);
1631-
continue;
1632-
}
1629+
isIndexExprLongString = true;
1630+
env->Add<TextElement>(child);
1631+
env->Add<KeepElement>(1);
1632+
continue;
16331633
}
16341634
}
1635-
else if(child->GetTokenType() == ']' && isIndexExprLongString)
1635+
else if (child->GetTokenType() == ']' && isIndexExprLongString)
16361636
{
16371637
env->Add<KeepElement>(1);
16381638
}
@@ -2226,23 +2226,22 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
22262226
{
22272227
expressionAfterIndexOperator = true;
22282228
auto nextNode = NextNode(it, children);
2229-
if (nextNode && nextNode->GetType() == LuaAstNodeType::Expression)
2229+
if (nextNode && nextNode->GetType() == LuaAstNodeType::Expression
2230+
&& (StringUtil::StartWith(nextNode->GetText(), "[")
2231+
|| StringUtil::EndWith(nextNode->GetText(), "]")))
22302232
{
2231-
auto stringExpr = nextNode->FindFirstOf(LuaAstNodeType::StringLiteralExpression);
2232-
if (stringExpr && StringUtil::StartWith(stringExpr->GetText(), "[")) {
2233-
isIndexExprLongString = true;
2234-
env->Add<TextElement>(child);
2235-
env->Add<KeepElement>(1);
2236-
continue;
2237-
}
2233+
isIndexExprLongString = true;
2234+
env->Add<TextElement>(child);
2235+
env->Add<KeepElement>(1);
2236+
continue;
22382237
}
22392238

22402239
env->Add<TextElement>(child);
22412240
env->Add<KeepElement>(0);
22422241
}
2243-
else if (child->GetTokenType() == ']' )
2242+
else if (child->GetTokenType() == ']')
22442243
{
2245-
env->Add<KeepElement>(isIndexExprLongString? 1:0);
2244+
env->Add<KeepElement>(isIndexExprLongString ? 1 : 0);
22462245
env->Add<TextElement>(child);
22472246
env->Add<KeepElement>(0);
22482247
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

22
# see https://github.com/CppCXY/EmmyLuaCodeStyle
33
[*.lua]
4-
enable_check_codestyle = false
4+
enable_check_codestyle = false
5+
indent_style = space

Test/test_script/format_text/wait_format/expression.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function f()
2525
.fffff()
2626
.jjjjj()
2727
aaaa[ [[bbbb]] ] = 123
28+
aaaa[ [[bbbbb]] + 1 ] = 456
29+
aaaa[ ~ [[cccccc]] ] = 789
2830
local t = {
2931
aaa,bbbb,cccc
3032
}

Test/test_script/format_text/wait_format_should_be/expression.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function f()
2525
.fffff()
2626
.jjjjj()
2727
aaaa[ [[bbbb]] ] = 123
28+
aaaa[ [[bbbbb]] + 1 ] = 456
29+
aaaa[ ~[[cccccc]] ] = 789
2830
local t = {
2931
aaa, bbbb, cccc
3032
}

0 commit comments

Comments
 (0)