Skip to content

Commit 99ec9e6

Browse files
committed
impl #8
1 parent 99a7c7e commit 99ec9e6

File tree

4 files changed

+85
-8
lines changed

4 files changed

+85
-8
lines changed

CodeService/src/LuaEditorConfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
284284
configMap.at("continuous_assign_table_field_align_to_equal_sign") == "true";
285285
}
286286

287+
if (configMap.count("table_append_expression_no_space"))
288+
{
289+
options->table_append_expression_no_space =
290+
configMap.at("table_append_expression_no_space") == "true";
291+
}
292+
287293
if (configMap.count("end_of_line"))
288294
{
289295
auto lineSeparatorSymbol = configMap.at("end_of_line");

CodeService/src/LuaFormatter.cpp

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "CodeService/FormatElement/NoIndentElement.h"
1818
#include "CodeService/FormatElement/SerializeContext.h"
1919
#include "CodeService/FormatElement/IndentOnLineBreakElement.h"
20+
#include "Util/StringUtil.h"
2021

2122
bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
2223
{
@@ -1431,7 +1432,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableExpression(std::shared_p
14311432
++it;
14321433
bool allowAlign = _options.continuous_assign_table_field_align_to_equal_sign
14331434
&& _parser->GetLine((*it)->GetTextRange().StartOffset) != operatorLine;
1434-
env->AddChild(FormatAlignTableField(it, allowAlign ,children));
1435+
env->AddChild(FormatAlignTableField(it, allowAlign, children));
14351436
env->Add<KeepElement>(_options.keep_one_space_between_table_and_bracket ? 1 : 0);
14361437
}
14371438
}
@@ -1988,17 +1989,53 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatPrimaryExpression(std::shared
19881989

19891990
std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_ptr<LuaAstNode> indexExpression)
19901991
{
1992+
bool expressionAfterIndexOperator = false;
19911993
auto env = std::make_shared<SubExpressionElement>();
19921994
for (auto& child : indexExpression->GetChildren())
19931995
{
1994-
FormatSubExpressionNode(child, env);
1995-
if (child->GetType() == LuaAstNodeType::Comment)
1996-
{
1997-
env->Add<KeepElement>(1);
1998-
}
1999-
else
1996+
switch (child->GetType())
20001997
{
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+
}
20022039
}
20032040
}
20042041
return env;
@@ -2069,6 +2106,35 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallExpression(std::shared_pt
20692106
return env;
20702107
}
20712108

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+
20722138
std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<LuaAstNode> blockNode,
20732139
LuaFormatRange& validRange)
20742140
{

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class LuaCodeStyleOptions
105105

106106
bool continuous_assign_table_field_align_to_equal_sign = true;
107107

108+
bool table_append_expression_no_space = false;
109+
108110
std::string end_of_line = "\r\n";
109111

110112
bool insert_final_newline = true;

include/CodeService/LuaFormatter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ class LuaFormatter
125125

126126
std::shared_ptr<FormatElement> FormatCallExpression(std::shared_ptr<LuaAstNode> callExpression);
127127

128+
// special rule [#8](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/8)
129+
std::shared_ptr<FormatElement> FormatTableAppendExpression(std::shared_ptr<LuaAstNode> expression);
128130
// special handle range format
129131
std::shared_ptr<FormatElement> FormatRangeBlock(std::shared_ptr<LuaAstNode> blockNode, LuaFormatRange& validRange);
130132
private:
131133
std::shared_ptr<LuaParser> _parser;
132134
LuaCodeStyleOptions& _options;
133135
std::shared_ptr<FormatElement> _env;
134136
};
137+

0 commit comments

Comments
 (0)