@@ -1195,7 +1195,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionStatement(std::shar
11951195 env->AddChild (FormatExpression (child, expressionEnv));
11961196 break ;
11971197 }
1198- // default 一般只有一个分号
1198+ // default 一般只有一个分号
11991199 default :
12001200 {
12011201 DefaultHandle (child, env);
@@ -1570,7 +1570,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
15701570 if (tableFieldLayout)
15711571 {
15721572 auto fields = FormatAlignTableField (it, leftBraceLine, children);
1573- if (fields->Is (FormatElementType::ExpressionElement)) {
1573+ if (fields->Is (FormatElementType::ExpressionElement))
1574+ {
15741575 tableFieldLayout->AddChildren (fields->GetChildren ());
15751576 }
15761577 else
@@ -1593,8 +1594,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
15931594{
15941595 auto env = std::make_shared<ExpressionElement>();
15951596 auto eqSignFounded = false ;
1596- for (auto & child : tableField->GetChildren ())
1597+ auto & children = tableField->GetChildren ();
1598+ bool isIndexExprLongString = false ;
1599+ for (auto it = children.begin (); it != children.end (); ++it)
15971600 {
1601+ auto & child = *it;
15981602 switch (child->GetType ())
15991603 {
16001604 case LuaAstNodeType::GeneralOperator:
@@ -1612,6 +1616,30 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
16121616 }
16131617 break ;
16141618 }
1619+ case LuaAstNodeType::IndexOperator:
1620+ {
1621+ if (child->GetTokenType () == ' [' )
1622+ {
1623+ auto nextNode = NextNode (it, children);
1624+ if (nextNode && nextNode->GetType () == LuaAstNodeType::Expression)
1625+ {
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+ }
1633+ }
1634+ }
1635+ else if (child->GetTokenType () == ' ]' && isIndexExprLongString)
1636+ {
1637+ env->Add <KeepElement>(1 );
1638+ }
1639+
1640+ env->Add <TextElement>(child);
1641+ break ;
1642+ }
16151643 case LuaAstNodeType::Identify:
16161644 {
16171645 env->Add <TextElement>(child);
@@ -2184,33 +2212,58 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatPrimaryExpression(std::shared
21842212std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression (std::shared_ptr<LuaAstNode> indexExpression)
21852213{
21862214 bool expressionAfterIndexOperator = false ;
2215+ bool isIndexExprLongString = false ;
21872216 auto env = std::make_shared<SubExpressionElement>();
2188- for (auto & child : indexExpression->GetChildren ())
2217+ auto & children = indexExpression->GetChildren ();
2218+ for (auto it = children.begin (); it != children.end (); it++)
21892219 {
2220+ auto & child = *it;
21902221 switch (child->GetType ())
21912222 {
21922223 case LuaAstNodeType::IndexOperator:
21932224 {
2194- if (_options. table_append_expression_no_space )
2225+ if (child-> GetTokenType () == ' [ ' )
21952226 {
2196- if (child->GetTokenType () == ' [' )
2227+ expressionAfterIndexOperator = true ;
2228+ auto nextNode = NextNode (it, children);
2229+ if (nextNode && nextNode->GetType () == LuaAstNodeType::Expression)
21972230 {
2198- expressionAfterIndexOperator = true ;
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+ }
21992238 }
2200- }
22012239
2202- env->Add <TextElement>(child);
2203- if (_options.long_chain_expression_allow_one_space_after_colon )
2240+ env->Add <TextElement>(child);
2241+ env->Add <KeepElement>(0 );
2242+ }
2243+ else if (child->GetTokenType () == ' ]' )
2244+ {
2245+ env->Add <KeepElement>(isIndexExprLongString? 1 :0 );
2246+ env->Add <TextElement>(child);
2247+ env->Add <KeepElement>(0 );
2248+ }
2249+ else if (child->GetTokenType () == ' :' )
22042250 {
2205- if (child-> GetTokenType () == ' : ' && _parser-> GetLuaFile ()-> OnlyEmptyCharBefore (
2206- child-> GetTextRange (). StartOffset ) )
2251+ env-> Add <TextElement>(child);
2252+ if (_options. long_chain_expression_allow_one_space_after_colon )
22072253 {
2208- env->Add <MaxSpaceElement>(1 );
2209- continue ;
2254+ if (_parser->GetLuaFile ()->OnlyEmptyCharBefore (
2255+ child->GetTextRange ().StartOffset ))
2256+ {
2257+ env->Add <MaxSpaceElement>(1 );
2258+ continue ;
2259+ }
22102260 }
2261+ env->Add <KeepElement>(0 );
2262+ }
2263+ else
2264+ {
2265+ DefaultHandle (child, env);
22112266 }
2212-
2213- env->Add <KeepElement>(0 );
22142267
22152268 break ;
22162269 }
@@ -2228,13 +2281,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
22282281 if (StringUtil::StartWith (text, " #" ))
22292282 {
22302283 env->AddChild (FormatTableAppendExpression (child));
2231- env->Add <KeepElement>(0 );
22322284 continue ;
22332285 }
22342286 }
22352287
22362288 FormatSubExpression (child, env);
2237- env->Add <KeepElement>(0 );
22382289 break ;
22392290 }
22402291 default :
0 commit comments