|
17 | 17 | #include "CodeService/FormatElement/NoIndentElement.h" |
18 | 18 | #include "CodeService/FormatElement/SerializeContext.h" |
19 | 19 | #include "CodeService/FormatElement/IndentOnLineBreakElement.h" |
| 20 | +#include "Util/StringUtil.h" |
20 | 21 |
|
21 | 22 | bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container) |
22 | 23 | { |
@@ -1431,7 +1432,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p |
1431 | 1432 | ++it; |
1432 | 1433 | bool allowAlign = _options.continuous_assign_table_field_align_to_equal_sign |
1433 | 1434 | && _parser->GetLine((*it)->GetTextRange().StartOffset) != operatorLine; |
1434 | | - env->AddChild(FormatAlignTableField(it, allowAlign ,children)); |
| 1435 | + env->AddChild(FormatAlignTableField(it, allowAlign, children)); |
1435 | 1436 | env->Add<KeepElement>(_options.keep_one_space_between_table_and_bracket ? 1 : 0); |
1436 | 1437 | } |
1437 | 1438 | } |
@@ -1988,17 +1989,53 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatPrimaryExpression(std::shared |
1988 | 1989 |
|
1989 | 1990 | std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_ptr<LuaAstNode> indexExpression) |
1990 | 1991 | { |
| 1992 | + bool expressionAfterIndexOperator = false; |
1991 | 1993 | auto env = std::make_shared<SubExpressionElement>(); |
1992 | 1994 | for (auto& child : indexExpression->GetChildren()) |
1993 | 1995 | { |
1994 | | - FormatSubExpressionNode(child, env); |
1995 | | - if (child->GetType() == LuaAstNodeType::Comment) |
1996 | | - { |
1997 | | - env->Add<KeepElement>(1); |
1998 | | - } |
1999 | | - else |
| 1996 | + switch (child->GetType()) |
2000 | 1997 | { |
2001 | | - env->Add<KeepElement>(0); |
| 1998 | + case LuaAstNodeType::IndexOperator: |
| 1999 | + { |
| 2000 | + if (_options.table_append_expression_no_space) |
| 2001 | + { |
| 2002 | + if (child->GetText() == "[") |
| 2003 | + { |
| 2004 | + expressionAfterIndexOperator = true; |
| 2005 | + } |
| 2006 | + } |
| 2007 | + env->Add<TextElement>(child); |
| 2008 | + env->Add<KeepElement>(0); |
| 2009 | + break; |
| 2010 | + } |
| 2011 | + case LuaAstNodeType::Comment: |
| 2012 | + { |
| 2013 | + env->Add<TextElement>(child); |
| 2014 | + env->Add<KeepElement>(1); |
| 2015 | + break; |
| 2016 | + } |
| 2017 | + case LuaAstNodeType::Expression: |
| 2018 | + { |
| 2019 | + if (_options.table_append_expression_no_space && expressionAfterIndexOperator) |
| 2020 | + { |
| 2021 | + auto text = child->GetText(); |
| 2022 | + if (StringUtil::StartWith(text, "#")) |
| 2023 | + { |
| 2024 | + env->AddChild(FormatTableAppendExpression(child)); |
| 2025 | + env->Add<KeepElement>(0); |
| 2026 | + continue; |
| 2027 | + } |
| 2028 | + } |
| 2029 | + |
| 2030 | + FormatSubExpressionNode(child, env); |
| 2031 | + env->Add<KeepElement>(0); |
| 2032 | + break; |
| 2033 | + } |
| 2034 | + default: |
| 2035 | + { |
| 2036 | + FormatSubExpressionNode(child, env); |
| 2037 | + env->Add<KeepElement>(0); |
| 2038 | + } |
2002 | 2039 | } |
2003 | 2040 | } |
2004 | 2041 | return env; |
@@ -2069,6 +2106,35 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt |
2069 | 2106 | return env; |
2070 | 2107 | } |
2071 | 2108 |
|
| 2109 | +std::shared_ptr<FormatElement> LuaFormatter::FormatTableAppendExpression(std::shared_ptr<LuaAstNode> expression) |
| 2110 | +{ |
| 2111 | + auto env = std::make_shared<LongExpressionLayoutElement>(_options.continuation_indent_size); |
| 2112 | + |
| 2113 | + if (expression->GetChildren().size() == 1) |
| 2114 | + { |
| 2115 | + auto root = expression->GetChildren().front(); |
| 2116 | + if (root->GetType() == LuaAstNodeType::BinaryExpression && root->GetChildren().size() == 3) |
| 2117 | + { |
| 2118 | + auto& binaryExpressionChildren = root->GetChildren(); |
| 2119 | + if (binaryExpressionChildren[0]->GetType() == LuaAstNodeType::UnaryExpression |
| 2120 | + && (binaryExpressionChildren[1]->GetType() == LuaAstNodeType::BinaryOperator |
| 2121 | + && binaryExpressionChildren[1]->GetText() == "+") |
| 2122 | + && binaryExpressionChildren[2]->GetText() == "1") |
| 2123 | + { |
| 2124 | + FormatSubExpressionNode(binaryExpressionChildren[0], env); |
| 2125 | + env->Add<KeepElement>(0); |
| 2126 | + env->Add<TextElement>(binaryExpressionChildren[1]); |
| 2127 | + env->Add<KeepElement>(0); |
| 2128 | + env->Add<TextElement>(binaryExpressionChildren[2]); |
| 2129 | + return env; |
| 2130 | + } |
| 2131 | + } |
| 2132 | + } |
| 2133 | + |
| 2134 | + return FormatExpression(expression, env); |
| 2135 | +} |
| 2136 | + |
| 2137 | + |
2072 | 2138 | std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<LuaAstNode> blockNode, |
2073 | 2139 | LuaFormatRange& validRange) |
2074 | 2140 | { |
|
0 commit comments