Skip to content

Commit 5d6c086

Browse files
committed
fix #131, close #132
1 parent 7b1f517 commit 5d6c086

File tree

9 files changed

+133
-44
lines changed

9 files changed

+133
-44
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class LuaStyle {
142142

143143
LineSpace line_space_after_comment = LineSpace(LineSpaceType::Keep);
144144

145+
LineSpace line_space_around_block = LineSpace(LineSpaceType::Fixed, 1);
145146
// [line break]
146147
bool break_all_list_when_line_exceed = false;
147148

@@ -157,7 +158,5 @@ class LuaStyle {
157158
// not implement now
158159
bool leading_comma_style = false;
159160

160-
bool table_list_special_continue_indent = true;
161-
162161
EndStmtWithSemicolon end_statement_with_semicolon = EndStmtWithSemicolon::Keep;
163162
};

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/SpaceAnalyzer.h

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ class SpaceAnalyzer : public FormatAnalyzer {
88
public:
99
DECLARE_FORMAT_ANALYZER(SpaceAnalyzer)
1010

11-
// workaround for mac 10.13
12-
struct OptionalInt {
13-
OptionalInt() : HasValue(false), Value(0) {}
14-
15-
explicit OptionalInt(std::size_t value) : HasValue(true), Value(value) {}
16-
17-
bool HasValue;
18-
std::size_t Value;
11+
enum class SpacePriority : std::size_t {
12+
Normal = 0,
13+
CommentFirst,
1914
};
2015

2116
SpaceAnalyzer();
@@ -26,21 +21,39 @@ class SpaceAnalyzer : public FormatAnalyzer {
2621

2722
void Query(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) override;
2823

29-
void SpaceAround(LuaSyntaxNode &n, const LuaSyntaxTree &t, std::size_t space = 1);
24+
void SpaceAround(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space = 1, SpacePriority priority = SpacePriority::Normal);
3025

31-
void SpaceLeft(LuaSyntaxNode &n, const LuaSyntaxTree &t, std::size_t space = 1);
26+
void SpaceLeft(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space = 1, SpacePriority priority = SpacePriority::Normal);
3227

33-
void SpaceRight(LuaSyntaxNode &n, const LuaSyntaxTree &t, std::size_t space = 1);
28+
void SpaceRight(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space = 1, SpacePriority priority = SpacePriority::Normal);
3429

35-
void SpaceIgnore(LuaSyntaxNode &n, const LuaSyntaxTree &t);
30+
void SpaceIgnore(LuaSyntaxNode n);
3631

3732
private:
38-
OptionalInt GetRightSpace(LuaSyntaxNode &n) const;
33+
// workaround for mac 10.13
34+
struct OptionalInt {
35+
OptionalInt() : HasValue(false), Value(0) {}
3936

40-
std::size_t ProcessSpace(const LuaSyntaxTree &t, LuaSyntaxNode &left, LuaSyntaxNode &right);
37+
explicit OptionalInt(std::size_t value) : HasValue(true), Value(value) {}
4138

42-
std::unordered_map<std::size_t, std::size_t> _rightSpaces;
43-
std::unordered_set<std::size_t> _ignoreSpace;
39+
bool HasValue;
40+
std::size_t Value;
41+
};
42+
43+
// This is to ensure that the settings on the right take priority.
44+
struct SpaceData {
45+
SpaceData() : SpaceData(0) {}
4446

47+
explicit SpaceData(std::size_t space, SpacePriority priority = SpacePriority::Normal) : Priority(priority), Value(space) {}
4548

49+
std::size_t Value;
50+
SpacePriority Priority;
51+
};
52+
53+
OptionalInt GetRightSpace(LuaSyntaxNode n) const;
54+
55+
std::size_t ProcessSpace(LuaSyntaxNode left, LuaSyntaxNode right, const LuaSyntaxTree &t);
56+
57+
std::unordered_map<std::size_t, SpaceData> _rightSpaces;
58+
std::unordered_set<std::size_t> _ignoreSpace;
4659
};

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
231231
{"line_space_after_local_or_assign_statement", line_space_after_local_or_assign_statement},
232232
{"line_space_after_function_statement", line_space_after_function_statement },
233233
{"line_space_after_expression_statement", line_space_after_expression_statement },
234-
{"line_space_after_comment", line_space_after_comment }
234+
{"line_space_after_comment", line_space_after_comment },
235+
{"line_space_around_block", line_space_around_block }
235236
};
236237

237238
for (auto &lineOption: fieldList) {

CodeFormatCore/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ AlignAnalyzer::AlignAnalyzer() {
99
}
1010

1111
void AlignAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
12-
std::unordered_map<std::size_t, std::size_t> marks;
1312
for (auto syntaxNode: t.GetSyntaxNodes()) {
1413
if (syntaxNode.IsNode(t)) {
1514
switch (syntaxNode.GetSyntaxKind(t)) {

CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ void LineBreakAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
121121
}
122122
}
123123

124-
BreakBefore(syntaxNode, t);
125-
BreakAfter(syntaxNode, t);
124+
BreakBefore(syntaxNode, t, f.GetStyle().line_space_around_block);
125+
BreakAfter(syntaxNode, t, f.GetStyle().line_space_around_block);
126126
break;
127127
}
128128
case LuaSyntaxNodeKind::LocalStatement:
@@ -372,10 +372,9 @@ void LineBreakAnalyzer::AnalyzeExpr(FormatState &f, LuaSyntaxNode &expr, const L
372372
}
373373
bool forceBreak = f.GetStyle().break_all_list_when_line_exceed && CanBreakAll(f, tableFieldList, t);
374374
if (!forceBreak) {
375-
forceBreak = tableFieldList.GetStartLine(t) != tableFieldList.GetEndLine(t) && std::any_of(fields.begin(), fields.end(),
376-
[&](LuaSyntaxNode &node) {
377-
return node.GetChildToken('=', t).IsToken(t);
378-
});
375+
forceBreak = tableFieldList.GetStartLine(t) != tableFieldList.GetEndLine(t) && std::any_of(fields.begin(), fields.end(), [&](LuaSyntaxNode &node) {
376+
return node.GetChildToken('=', t).IsToken(t);
377+
});
379378
}
380379
if (forceBreak) {
381380
auto leftBrace = expr.GetChildToken('{', t);

CodeFormatCore/src/Format/Analyzer/SpaceAnalyzer.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
121121
}
122122
case TK_LONG_COMMENT:
123123
case TK_SHORT_COMMENT: {
124-
SpaceAround(syntaxNode, t, f.GetStyle().space_before_inline_comment);
124+
SpaceLeft(syntaxNode, t, f.GetStyle().space_before_inline_comment, SpacePriority::CommentFirst);
125+
SpaceRight(syntaxNode, t, 1);
125126
break;
126127
}
127128
default: {
@@ -149,7 +150,7 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
149150
if (exprList.IsNode(t)) {
150151
auto commas = exprList.GetChildTokens(',', t);
151152
for (auto &comma: commas) {
152-
SpaceIgnore(comma, t);
153+
SpaceIgnore(comma);
153154
}
154155
}
155156
}
@@ -255,7 +256,7 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
255256
if (f.GetStyle().ignore_space_after_colon) {
256257
auto colon = syntaxNode.GetChildToken(':', t);
257258
if (colon.IsToken(t)) {
258-
SpaceIgnore(colon, t);
259+
SpaceIgnore(colon);
259260
}
260261
}
261262

@@ -329,27 +330,45 @@ void SpaceAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
329330
void SpaceAnalyzer::Query(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
330331
if (syntaxNode.IsToken(t)) {
331332
auto nextToken = syntaxNode.GetNextToken(t);
332-
auto space = ProcessSpace(t, syntaxNode, nextToken);
333+
auto space = ProcessSpace(syntaxNode, nextToken, t);
333334
resolve.SetNextSpace(space);
334335
}
335336
}
336337

337-
void SpaceAnalyzer::SpaceAround(LuaSyntaxNode &n, const LuaSyntaxTree &t, std::size_t space) {
338-
SpaceLeft(n, t, space);
339-
SpaceRight(n, t, space);
338+
void SpaceAnalyzer::SpaceAround(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space, SpacePriority priority) {
339+
SpaceLeft(n, t, space, priority);
340+
SpaceRight(n, t, space, priority);
340341
}
341342

342-
void SpaceAnalyzer::SpaceLeft(LuaSyntaxNode &n, const LuaSyntaxTree &t, std::size_t space) {
343+
void SpaceAnalyzer::SpaceLeft(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space, SpacePriority priority) {
343344
auto token = n.GetPrevToken(t);
344-
_rightSpaces[token.GetIndex()] = space;
345+
auto it = _rightSpaces.find(token.GetIndex());
346+
if (it != _rightSpaces.end()) {
347+
if (it->second.Priority > priority) {
348+
return;
349+
}
350+
}
351+
352+
_rightSpaces[token.GetIndex()] = SpaceData(space, priority);
345353
}
346354

347-
void SpaceAnalyzer::SpaceRight(LuaSyntaxNode &n, const LuaSyntaxTree &t, std::size_t space) {
355+
void SpaceAnalyzer::SpaceRight(LuaSyntaxNode n, const LuaSyntaxTree &t, std::size_t space, SpacePriority priority) {
348356
auto token = n.GetLastToken(t);
349-
_rightSpaces[token.GetIndex()] = space;
357+
auto it = _rightSpaces.find(token.GetIndex());
358+
if (it != _rightSpaces.end()) {
359+
if (it->second.Priority > priority) {
360+
return;
361+
}
362+
}
363+
364+
_rightSpaces[token.GetIndex()] = SpaceData(space, priority);
350365
}
351366

352-
SpaceAnalyzer::OptionalInt SpaceAnalyzer::GetRightSpace(LuaSyntaxNode &n) const {
367+
void SpaceAnalyzer::SpaceIgnore(LuaSyntaxNode n) {
368+
_ignoreSpace.insert(n.GetIndex());
369+
}
370+
371+
SpaceAnalyzer::OptionalInt SpaceAnalyzer::GetRightSpace(LuaSyntaxNode n) const {
353372
if (_ignoreSpace.count(n.GetIndex())) {
354373
return OptionalInt();
355374
}
@@ -358,11 +377,11 @@ SpaceAnalyzer::OptionalInt SpaceAnalyzer::GetRightSpace(LuaSyntaxNode &n) const
358377
if (it == _rightSpaces.end()) {
359378
return OptionalInt();
360379
}
361-
return OptionalInt(it->second);
380+
return OptionalInt(it->second.Value);
362381
}
363382

364383
std::size_t
365-
SpaceAnalyzer::ProcessSpace(const LuaSyntaxTree &t, LuaSyntaxNode &left, LuaSyntaxNode &right) {
384+
SpaceAnalyzer::ProcessSpace(LuaSyntaxNode left, LuaSyntaxNode right, const LuaSyntaxTree &t) {
366385
auto rightSpaceOfLeftToken = GetRightSpace(left);
367386
if (rightSpaceOfLeftToken.HasValue) {
368387
return rightSpaceOfLeftToken.Value;
@@ -372,7 +391,3 @@ SpaceAnalyzer::ProcessSpace(const LuaSyntaxTree &t, LuaSyntaxNode &left, LuaSynt
372391
}
373392
return 0;
374393
}
375-
376-
void SpaceAnalyzer::SpaceIgnore(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
377-
_ignoreSpace.insert(n.GetIndex());
378-
}

Test/src/FormatResult_unitest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,3 +1036,23 @@ local t = {
10361036
}
10371037
)"));
10381038
}
1039+
1040+
TEST(Format, bug_131) {
1041+
LuaStyle style;
1042+
1043+
style.space_before_inline_comment = 2;
1044+
style.align_continuous_inline_comment = false;
1045+
EXPECT_TRUE(TestHelper::TestFormatted(
1046+
R"(
1047+
local table = { -- My comment
1048+
elem1 = {}, --12313
1049+
elem2 = {}
1050+
}
1051+
)",
1052+
R"(
1053+
local table = { -- My comment
1054+
elem1 = {}, --12313
1055+
elem2 = {}
1056+
}
1057+
)", style));
1058+
}

Test/src/FormatStyle_unitest.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,3 +1752,45 @@ end
17521752
)",
17531753
style));
17541754
}
1755+
1756+
TEST(FormatByStyleOption, line_space_around_block) {
1757+
LuaStyle style;
1758+
1759+
style.line_space_around_block = LineSpace(LineSpaceType::Keep);
1760+
EXPECT_TRUE(TestHelper::TestFormatted(
1761+
R"(
1762+
function f()
1763+
1764+
local t = 123
1765+
1766+
end
1767+
)",
1768+
R"(
1769+
function f()
1770+
1771+
local t = 123
1772+
1773+
end
1774+
)",
1775+
style));
1776+
1777+
style.line_space_around_block = LineSpace(LineSpaceType::Max, 2);
1778+
EXPECT_TRUE(TestHelper::TestFormatted(
1779+
R"(
1780+
function f()
1781+
1782+
1783+
local t = 123
1784+
1785+
1786+
end
1787+
)",
1788+
R"(
1789+
function f()
1790+
1791+
local t = 123
1792+
1793+
end
1794+
)",
1795+
style));
1796+
}

lua.template.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ line_space_after_expression_statement = keep
132132

133133
line_space_after_comment = keep
134134

135+
line_space_around_block = fixed(1)
135136
# [line break]
136137
break_all_list_when_line_exceed = false
137138

0 commit comments

Comments
 (0)