Skip to content

Commit 5d6fae5

Browse files
committed
FIX #144
1 parent 6753393 commit 5d6fae5

File tree

9 files changed

+128
-21
lines changed

9 files changed

+128
-21
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ class LuaStyle {
8585

8686
bool space_after_comma_in_for_statement = true;
8787

88-
bool space_around_concat_operator = true;
88+
SpaceAroundStyle space_around_concat_operator = SpaceAroundStyle::Always;
8989

9090
bool space_around_logical_operator = true;
9191

92-
bool space_around_assign_operator = true;
92+
SpaceAroundStyle space_around_assign_operator = SpaceAroundStyle::Always;
9393

9494
// [Align]
9595
bool align_call_args = false;

CodeFormatCore/include/CodeFormatCore/Config/LuaStyleEnum.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ enum class BreakTableList {
9797
Smart,
9898
Lazy
9999
};
100+
101+
enum class SpaceAroundStyle {
102+
// for false and none
103+
None,
104+
// for true and always
105+
Always,
106+
// for no_space_asym
107+
NoSpaceAsym,
108+
};

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/TokenAnalyzer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ class TokenAnalyzer : public FormatAnalyzer {
1818
void MarkAdd(LuaSyntaxNode n, const LuaSyntaxTree &t, TokenAddStrategy strategy);
1919

2020
bool IsRemove(LuaSyntaxNode n, const LuaSyntaxTree &t) const;
21+
2122
private:
2223
void TableFieldAddSep(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t);
2324

24-
void AnalyzeTableField(FormatState &f, LuaSyntaxNode& syntaxNode , const LuaSyntaxTree &t);
25+
void AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t);
2526

26-
void AnalyzeCallExpression(FormatState &f, LuaSyntaxNode& syntaxNode , const LuaSyntaxTree &t);
27+
void AnalyzeCallExpression(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t);
2728

2829

2930
std::unordered_map<std::size_t, TokenStrategy> _tokenStrategies;

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,29 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
158158

159159
BOOL_OPTION(space_after_comma_in_for_statement)
160160

161-
BOOL_OPTION(space_around_concat_operator)
161+
if(configMap.count("space_around_concat_operator")) {
162+
auto &value = configMap.at("space_around_concat_operator");
163+
if (value == "true" || value == "always") {
164+
space_around_concat_operator = SpaceAroundStyle::Always;
165+
} else if (value == "false" || value == "none") {
166+
space_around_concat_operator = SpaceAroundStyle::None;
167+
} else if (value == "no_space_asym") {
168+
space_around_concat_operator = SpaceAroundStyle::NoSpaceAsym;
169+
}
170+
}
162171

163172
BOOL_OPTION(space_around_logical_operator)
164173

165-
BOOL_OPTION(space_around_assign_operator)
174+
if (configMap.count("space_around_assign_operator")) {
175+
auto &value = configMap.at("space_around_assign_operator");
176+
if (value == "true" || value == "always") {
177+
space_around_assign_operator = SpaceAroundStyle::Always;
178+
} else if (value == "false" || value == "none") {
179+
space_around_assign_operator = SpaceAroundStyle::None;
180+
} else if (value == "no_space_asym") {
181+
space_around_assign_operator = SpaceAroundStyle::NoSpaceAsym;
182+
}
183+
}
166184

167185
BOOL_OPTION(align_call_args)
168186

CodeFormatCore/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ void AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, Al
376376
auto eq = node.GetChildToken('=', t);
377377
if (eq.IsToken(t)) {
378378
auto prev = eq.GetPrevToken(t);
379-
auto prevSpace = f.GetStyle().space_around_assign_operator ? 2 : 1;
379+
auto prevSpace = f.GetStyle().space_around_assign_operator == SpaceAroundStyle::Always ? 2 : 1;
380380
auto newPos = prev.GetTextRange(t).GetEndOffset() + prevSpace - node.GetTextRange(t).StartOffset;
381381
if (newPos > maxDis) {
382382
maxDis = newPos;
@@ -399,7 +399,7 @@ void AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, Al
399399
if (file.CheckCurrentLineUnicodeBefore(eq.GetTextRange(t).StartOffset)) {
400400
return;
401401
}
402-
auto prevSpace = f.GetStyle().space_around_assign_operator ? 2 : 1;
402+
auto prevSpace = f.GetStyle().space_around_assign_operator == SpaceAroundStyle::Always ? 2 : 1;
403403
auto newPos = prev.GetTextRange(t).GetEndOffset() + prevSpace - node.GetTextRange(t).StartOffset;
404404
if (newPos > maxDis) {
405405
maxDis = newPos;

CodeFormatCore/src/Format/Analyzer/SpaceAnalyzer.cpp

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,65 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
2626
break;
2727
}
2828
case TK_CONCAT: {
29-
if (!f.GetStyle().space_around_concat_operator &&
30-
syntaxNode.GetPrevToken(t).GetTokenKind(t) != TK_NUMBER) {
31-
SpaceAround(syntaxNode, t, 0);
32-
break;
29+
switch(f.GetStyle().space_around_concat_operator){
30+
case SpaceAroundStyle::Always: {
31+
SpaceAround(syntaxNode, t, 1);
32+
break;
33+
}
34+
case SpaceAroundStyle::None: {
35+
if(syntaxNode.GetPrevToken(t).GetTokenKind(t) == TK_NUMBER) {
36+
SpaceAround(syntaxNode, t, 1);
37+
}
38+
else {
39+
SpaceAround(syntaxNode, t, 0);
40+
}
41+
break;
42+
}
43+
case SpaceAroundStyle::NoSpaceAsym: {
44+
if(syntaxNode.GetPrevToken(t).GetTokenKind(t) == TK_NUMBER) {
45+
SpaceLeft(syntaxNode, t, 1);
46+
SpaceRight(syntaxNode, t, 0);
47+
}
48+
else {
49+
SpaceAround(syntaxNode, t, 0);
50+
}
51+
break;
52+
}
53+
default: {
54+
break;
55+
}
3356
}
34-
35-
SpaceAround(syntaxNode, t, 1);
3657
break;
3758
}
3859
case '=': {
39-
SpaceAround(syntaxNode, t, f.GetStyle().space_around_assign_operator ? 1 : 0);
60+
switch(f.GetStyle().space_around_assign_operator){
61+
case SpaceAroundStyle::Always: {
62+
SpaceAround(syntaxNode, t, 1);
63+
break;
64+
}
65+
case SpaceAroundStyle::None: {
66+
if(syntaxNode.GetPrevToken(t).GetTokenKind(t) == '>') {
67+
SpaceAround(syntaxNode, t, 1);
68+
}
69+
else {
70+
SpaceAround(syntaxNode, t, 0);
71+
}
72+
break;
73+
}
74+
case SpaceAroundStyle::NoSpaceAsym: {
75+
if(syntaxNode.GetPrevToken(t).GetTokenKind(t) == '>') {
76+
SpaceLeft(syntaxNode, t, 1);
77+
SpaceRight(syntaxNode, t, 0);
78+
}
79+
else {
80+
SpaceAround(syntaxNode, t, 0);
81+
}
82+
break;
83+
}
84+
default: {
85+
break;
86+
}
87+
}
4088
break;
4189
}
4290
case TK_GE:

Test/src/FormatResult_unitest.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,43 @@ local table = { -- My comment
10601060
TEST(Format, bug_144) {
10611061
LuaStyle style;
10621062

1063-
style.space_around_concat_operator = false;
1063+
style.space_around_concat_operator = SpaceAroundStyle::None;
10641064
EXPECT_TRUE(TestHelper::TestFormatted(
10651065
R"(
10661066
local t = 1 .. "1231"..bb..1111
10671067
)",
10681068
R"(
10691069
local t = 1 .. "1231"..bb..1111
1070+
)", style));
1071+
1072+
style.space_around_concat_operator = SpaceAroundStyle::NoSpaceAsym;
1073+
EXPECT_TRUE(TestHelper::TestFormatted(
1074+
R"(
1075+
local t = 1 .. "1231"..bb..1111
1076+
)",
1077+
R"(
1078+
local t = 1 .."1231"..bb..1111
1079+
)", style));
1080+
1081+
style.space_around_concat_operator = SpaceAroundStyle::Always;
1082+
style.space_around_assign_operator = SpaceAroundStyle::None;
1083+
EXPECT_TRUE(TestHelper::TestFormatted(
1084+
R"(
1085+
local t = 1
1086+
local t <const> = 1
1087+
)",
1088+
R"(
1089+
local t=1
1090+
local t <const> = 1
1091+
)", style));
1092+
style.space_around_assign_operator = SpaceAroundStyle::NoSpaceAsym;
1093+
EXPECT_TRUE(TestHelper::TestFormatted(
1094+
R"(
1095+
local t = 1
1096+
local t <const> = 1
1097+
)",
1098+
R"(
1099+
local t=1
1100+
local t <const> =1
10701101
)", style));
10711102
}

Test/src/FormatStyle_unitest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ end
881881
TEST(FormatByStyleOption, space_around_concat_operator) {
882882
LuaStyle style;
883883

884-
style.space_around_concat_operator = true;
884+
style.space_around_concat_operator = SpaceAroundStyle::Always;
885885
EXPECT_TRUE(TestHelper::TestFormatted(
886886
R"(
887887
local t = "123" .. 1
@@ -890,7 +890,7 @@ local t = "123" .. 1
890890
local t = "123" .. 1
891891
)",
892892
style));
893-
style.space_around_concat_operator = false;
893+
style.space_around_concat_operator = SpaceAroundStyle::None;
894894
EXPECT_TRUE(TestHelper::TestFormatted(
895895
R"(
896896
local t = "123" .. 1
@@ -1802,7 +1802,7 @@ end
18021802
TEST(FormatByStyleOption, space_around_assign_operator) {
18031803
LuaStyle style;
18041804

1805-
style.space_around_assign_operator = true;
1805+
style.space_around_assign_operator = SpaceAroundStyle::Always;
18061806
EXPECT_TRUE(TestHelper::TestFormatted(
18071807
R"(
18081808
local t = 123
@@ -1818,7 +1818,7 @@ local d = {
18181818
)",
18191819
style));
18201820

1821-
style.space_around_assign_operator = false;
1821+
style.space_around_assign_operator = SpaceAroundStyle::None;
18221822
EXPECT_TRUE(TestHelper::TestFormatted(
18231823
R"(
18241824
local t = 123

Test2/src/FormatTest2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ local t = 1 .. "1231"
2424
std::cout << t.GetDebugView() << std::endl;
2525

2626
LuaStyle s;
27-
s.space_around_concat_operator = false;
27+
s.space_around_concat_operator = SpaceAroundStyle::None;
2828
FormatBuilder b(s);
2929
auto text = b.GetFormatResult(t);
3030
std::cout<< text << std::endl;

0 commit comments

Comments
 (0)