Skip to content

Commit 424fcd5

Browse files
committed
end_with_semicolon support never
1 parent 48c2b37 commit 424fcd5

File tree

8 files changed

+46
-4
lines changed

8 files changed

+46
-4
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyleEnum.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ enum class EndStmtWithSemicolon {
8989
Keep,
9090
ReplaceWithNewline,
9191
Always,
92-
SameLine
92+
SameLine,
93+
Never
9394
};
9495

9596
enum class BreakTableList {

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/SemicolonAnalyzer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SemicolonAnalyzer : public FormatAnalyzer {
2323
bool EndsWithSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t);
2424
bool ContainsSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t);
2525
LuaSyntaxNode GetLastNonCommentToken(LuaSyntaxNode n, const LuaSyntaxTree &t);
26+
bool CanCorrectRemoveSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t);
2627

2728
std::unordered_map<std::size_t, SemicolonStrategy> _semicolon;
2829
};

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
313313
end_statement_with_semicolon = EndStmtWithSemicolon::ReplaceWithNewline;
314314
} else if (configMap.at("end_statement_with_semicolon") == "same_line") {
315315
end_statement_with_semicolon = EndStmtWithSemicolon::SameLine;
316+
} else if (configMap.at("end_statement_with_semicolon") == "never") {
317+
end_statement_with_semicolon = EndStmtWithSemicolon::Never;
316318
}
317319
}
318320
}

CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void LineBreakAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
4242
switch (syntaxNode.GetSyntaxKind(t)) {
4343
case LuaSyntaxNodeKind::Block: {
4444
auto children = syntaxNode.GetChildren(t);
45-
if (syntaxNode.GetParent(t).IsSingleLineNode(t)) {
45+
if (syntaxNode.IsSingleLineNode(t)) {
4646
if (children.size() <= 1) {
4747
auto spaceAnalyzer = f.GetAnalyzer<SpaceAnalyzer>();
4848
spaceAnalyzer->SpaceAround(syntaxNode, t);

CodeFormatCore/src/Format/Analyzer/SemicolonAnalyzer.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ void SemicolonAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
4242
}
4343
break;
4444
}
45+
case EndStmtWithSemicolon::Never: {
46+
if (CanCorrectRemoveSemicolon(syntaxNode, t)) {
47+
RemoveSemicolon(syntaxNode, t);
48+
}
49+
break;
50+
}
4551
default: {
4652
break;
4753
}
@@ -129,3 +135,21 @@ LuaSyntaxNode SemicolonAnalyzer::GetLastNonCommentToken(LuaSyntaxNode n, const L
129135
}
130136
}
131137
}
138+
139+
bool SemicolonAnalyzer::CanCorrectRemoveSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t) {
140+
if(!EndsWithSemicolon(n, t)){
141+
return false;
142+
}
143+
auto semicolon = GetLastNonCommentToken(n, t);
144+
auto nextToken = semicolon.GetNextToken(t);
145+
if(n.GetEndLine(t) == nextToken.GetStartLine(t)){
146+
return false;
147+
}
148+
149+
auto nextTokenKind = nextToken.GetTokenKind(t);
150+
if (nextTokenKind == '{' || nextTokenKind == '(') {
151+
return false;
152+
}
153+
154+
return true;
155+
}

Test/src/FormatResult_unitest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,4 +1125,4 @@ only_latin = {
11251125
{ 'e', nil },
11261126
}
11271127
)", style));
1128-
}
1128+
}

Test/src/FormatStyle_unitest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,19 @@ for i, v in ipairs(values) do
16821682
end
16831683
end
16841684
local table1 = { 1, 2, 3 }
1685+
)",
1686+
style));
1687+
style.end_statement_with_semicolon = EndStmtWithSemicolon::Never;
1688+
EXPECT_TRUE(TestHelper::TestFormatted(
1689+
R"(
1690+
local t = pp;
1691+
("aa"):foramt()
1692+
local d = {};
1693+
)",
1694+
R"(
1695+
local t = pp;
1696+
("aa"):foramt()
1697+
local d = {}
16851698
)",
16861699
style));
16871700
}

lua.template.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ break_all_list_when_line_exceed = false
145145
auto_collapse_lines = false
146146

147147
break_before_braces = false
148+
148149
# [preference]
149150
ignore_space_after_colon = false
150151

151152
remove_call_expression_list_finish_comma = false
152-
# keep / always / same_line / repalce_with_newline
153+
# keep / always / same_line / replace_with_newline / never
153154
end_statement_with_semicolon = keep

0 commit comments

Comments
 (0)