Skip to content

Commit fae0e8f

Browse files
committed
close #135
1 parent e6f382c commit fae0e8f

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class LuaStyle {
8787

8888
bool space_around_concat_operator = true;
8989

90+
bool space_around_logical_operator = true;
91+
92+
bool space_around_assign_operator = true;
93+
9094
// [Align]
9195
bool align_call_args = false;
9296

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
160160

161161
BOOL_OPTION(space_around_concat_operator)
162162

163+
BOOL_OPTION(space_around_logical_operator)
164+
165+
BOOL_OPTION(space_around_assign_operator)
166+
163167
BOOL_OPTION(align_call_args)
164168

165169
BOOL_OPTION(align_function_params)

CodeFormatCore/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ 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 newPos = prev.GetTextRange(t).GetEndOffset() + 2 - node.GetTextRange(t).StartOffset;
379+
auto prevSpace = f.GetStyle().space_around_assign_operator ? 2 : 1;
380+
auto newPos = prev.GetTextRange(t).GetEndOffset() + prevSpace - node.GetTextRange(t).StartOffset;
380381
if (newPos > maxDis) {
381382
maxDis = newPos;
382383
}
@@ -398,7 +399,8 @@ void AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, Al
398399
if (file.CheckCurrentLineUnicodeBefore(eq.GetTextRange(t).StartOffset)) {
399400
return;
400401
}
401-
auto newPos = prev.GetTextRange(t).GetEndOffset() + 2 - node.GetTextRange(t).StartOffset;
402+
auto prevSpace = f.GetStyle().space_around_assign_operator ? 2 : 1;
403+
auto newPos = prev.GetTextRange(t).GetEndOffset() + prevSpace - node.GetTextRange(t).StartOffset;
402404
if (newPos > maxDis) {
403405
maxDis = newPos;
404406
}

CodeFormatCore/src/Format/Analyzer/SpaceAnalyzer.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
2929
SpaceAround(syntaxNode, t, f.GetStyle().space_around_concat_operator ? 1 : 0);
3030
break;
3131
}
32-
case '=':
33-
case TK_AND:
34-
case TK_OR:
32+
case '=': {
33+
SpaceAround(syntaxNode, t, f.GetStyle().space_around_assign_operator ? 1 : 0);
34+
break;
35+
}
3536
case TK_GE:
3637
case TK_LE:
3738
case TK_NE:
38-
case TK_EQ:
39+
case TK_EQ: {
40+
SpaceAround(syntaxNode, t, f.GetStyle().space_around_logical_operator ? 1 : 0);
41+
break;
42+
}
43+
case TK_AND:
44+
case TK_OR:
3945
case TK_IN: {
4046
SpaceAround(syntaxNode, t, 1);
4147
break;
@@ -60,7 +66,7 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
6066
case '>': {
6167
auto p = syntaxNode.GetParent(t);
6268
if (p.GetSyntaxKind(t) == LuaSyntaxNodeKind::BinaryExpression) {
63-
SpaceAround(syntaxNode, t);
69+
SpaceAround(syntaxNode, t, f.GetStyle().space_around_logical_operator ? 1 : 0);
6470
} else if (syntaxNode.GetTokenKind(t) == '<') {
6571
SpaceRight(syntaxNode, t, 0);
6672
} else {

Test/src/FormatStyle_unitest.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,3 +1794,62 @@ end
17941794
)",
17951795
style));
17961796
}
1797+
TEST(FormatByStyleOption, space_around_assign_operator) {
1798+
LuaStyle style;
1799+
1800+
style.space_around_assign_operator = true;
1801+
EXPECT_TRUE(TestHelper::TestFormatted(
1802+
R"(
1803+
local t = 123
1804+
local d = {
1805+
d = 123
1806+
}
1807+
)",
1808+
R"(
1809+
local t = 123
1810+
local d = {
1811+
d = 123
1812+
}
1813+
)",
1814+
style));
1815+
1816+
style.space_around_assign_operator = false;
1817+
EXPECT_TRUE(TestHelper::TestFormatted(
1818+
R"(
1819+
local t = 123
1820+
local d = {
1821+
d = 123
1822+
}
1823+
)",
1824+
R"(
1825+
local t=123
1826+
local d={
1827+
d=123
1828+
}
1829+
)",
1830+
style));
1831+
}
1832+
1833+
TEST(FormatByStyleOption, space_around_logical_operator) {
1834+
LuaStyle style;
1835+
1836+
style.space_around_logical_operator = true;
1837+
EXPECT_TRUE(TestHelper::TestFormatted(
1838+
R"(
1839+
local t = true == false or a < 2 and b > 3 or c <= 4 and d >= 5 or e ~= 6 and f == 7
1840+
)",
1841+
R"(
1842+
local t = true == false or a < 2 and b > 3 or c <= 4 and d >= 5 or e ~= 6 and f == 7
1843+
)",
1844+
style));
1845+
1846+
style.space_around_logical_operator = false;
1847+
EXPECT_TRUE(TestHelper::TestFormatted(
1848+
R"(
1849+
local t = true == false or a < 2 and b > 3 or c <= 4 and d >= 5 or e ~= 6 and f == 7
1850+
)",
1851+
R"(
1852+
local t = true==false or a<2 and b>3 or c<=4 and d>=5 or e~=6 and f==7
1853+
)",
1854+
style));
1855+
}

0 commit comments

Comments
 (0)