Skip to content

Commit 533e2cd

Browse files
committed
option space_after_comment_dash
1 parent dc2a0dd commit 533e2cd

File tree

8 files changed

+86
-28
lines changed

8 files changed

+86
-28
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class LuaStyle {
7979

8080
SpaceBeforeInlineComment space_before_inline_comment;
8181

82+
bool space_after_comment_dash = false;
83+
8284
// [operator space]
8385
SpaceAroundMath space_around_math_operator;
8486

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/FormatStrategy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ enum class TokenStrategy {
2626
TableSepComma,
2727
OriginRange,
2828
StmtEndSemicolon,
29-
NewLineBeforeToken
29+
NewLineBeforeToken,
30+
SpaceAfterCommentDash
3031
};
3132

3233
enum class TokenAddStrategy {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ class TokenAnalyzer : public FormatAnalyzer {
2222
private:
2323
void TableFieldAddSep(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t);
2424

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

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

29+
void AnalyzeComment(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t);
2930

3031
std::unordered_map<std::size_t, TokenStrategy> _tokenStrategies;
3132
std::unordered_map<std::size_t, TokenAddStrategy> _tokenAddStrategies;

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
226226
}
227227
}
228228

229+
BOOL_OPTION(space_after_comment_dash)
230+
229231
IF_EXIST(space_around_math_operator) {
230232
if (value == "true" || value == "always") {
231233
space_around_math_operator.SetAll(true);

CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ void TokenAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
4949

5050
break;
5151
}
52+
case TK_SHORT_COMMENT: {
53+
if (f.GetStyle().space_after_comment_dash) {
54+
AnalyzeComment(f, syntaxNode, t);
55+
}
56+
}
5257
default: {
5358
break;
5459
}
@@ -111,28 +116,28 @@ void TokenAnalyzer::TableFieldAddSep(FormatState &f, LuaSyntaxNode n, const LuaS
111116
}
112117
}
113118

114-
void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
119+
void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t) {
115120
if (f.GetStyle().table_separator_style == TableSeparatorStyle::Semicolon) {
116-
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
121+
auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
117122
auto comma = sep.GetChildToken(',', t);
118123
if (comma.IsToken(t)) {
119124
Mark(comma, t, TokenStrategy::TableSepSemicolon);
120125
}
121126
} else if (f.GetStyle().table_separator_style == TableSeparatorStyle::Comma) {
122-
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
127+
auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
123128
auto semicolon = sep.GetChildToken(';', t);
124129
if (semicolon.IsToken(t)) {
125130
Mark(semicolon, t, TokenStrategy::TableSepComma);
126131
}
127132
} else if (f.GetStyle().table_separator_style == TableSeparatorStyle::OnlyKVColon) {
128-
if (syntaxNode.GetChildToken('=', t).IsToken(t)) {
129-
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
133+
if (n.GetChildToken('=', t).IsToken(t)) {
134+
auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
130135
auto semicolon = sep.GetChildToken(',', t);
131136
if (semicolon.IsToken(t)) {
132137
Mark(semicolon, t, TokenStrategy::TableSepSemicolon);
133138
}
134139
} else {
135-
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
140+
auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
136141
auto semicolon = sep.GetChildToken(';', t);
137142
if (semicolon.IsToken(t)) {
138143
Mark(semicolon, t, TokenStrategy::TableSepComma);
@@ -141,28 +146,28 @@ void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode,
141146
}
142147

143148
if (f.GetStyle().trailing_table_separator != TrailingTableSeparator::Keep) {
144-
auto nextToken = syntaxNode.GetNextTokenSkipComment(t);
149+
auto nextToken = n.GetNextTokenSkipComment(t);
145150
// the last table field
146151
if (nextToken.GetTokenKind(t) == '}') {
147152
switch (f.GetStyle().trailing_table_separator) {
148153
case TrailingTableSeparator::Never: {
149-
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
154+
auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
150155
auto sepToken = sep.GetFirstToken(t);
151156
Mark(sepToken, t, TokenStrategy::Remove);
152157
break;
153158
}
154159
case TrailingTableSeparator::Always: {
155-
TableFieldAddSep(f, syntaxNode, t);
160+
TableFieldAddSep(f, n, t);
156161
break;
157162
}
158163
case TrailingTableSeparator::Smart: {
159-
auto tableFieldList = syntaxNode.GetParent(t);
164+
auto tableFieldList = n.GetParent(t);
160165
if (tableFieldList.GetEndLine(t) == nextToken.GetStartLine(t)) {
161-
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
166+
auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
162167
auto sepToken = sep.GetFirstToken(t);
163168
Mark(sepToken, t, TokenStrategy::Remove);
164169
} else {
165-
TableFieldAddSep(f, syntaxNode, t);
170+
TableFieldAddSep(f, n, t);
166171
}
167172
break;
168173
}
@@ -174,8 +179,8 @@ void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode,
174179
}
175180
}
176181

177-
bool IsSingleTableOrStringArg(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
178-
auto children = syntaxNode.GetChildren(t);
182+
bool IsSingleTableOrStringArg(LuaSyntaxNode n, const LuaSyntaxTree &t) {
183+
auto children = n.GetChildren(t);
179184
for (auto child: children) {
180185
if (child.GetTokenKind(t) == TK_STRING || child.GetTokenKind(t) == TK_LONG_STRING || child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
181186
return true;
@@ -218,39 +223,39 @@ LuaSyntaxNode GetSingleArgStringOrTable(LuaSyntaxNode &syntaxNode, const LuaSynt
218223
return LuaSyntaxNode(0);
219224
}
220225

221-
void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
222-
if (IsSingleTableOrStringArg(syntaxNode, t)) {
226+
void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t) {
227+
if (IsSingleTableOrStringArg(n, t)) {
223228
switch (f.GetStyle().call_arg_parentheses) {
224229
case CallArgParentheses::Remove: {
225-
auto lbrace = syntaxNode.GetChildToken('(', t);
230+
auto lbrace = n.GetChildToken('(', t);
226231
if (lbrace.IsToken(t)) {
227232
Mark(lbrace, t, TokenStrategy::Remove);
228-
auto rbrace = syntaxNode.GetChildToken(')', t);
233+
auto rbrace = n.GetChildToken(')', t);
229234
Mark(rbrace, t, TokenStrategy::Remove);
230235
}
231236

232237
break;
233238
}
234239
case CallArgParentheses::RemoveStringOnly: {
235-
auto node = GetSingleArgStringOrTable(syntaxNode, t);
240+
auto node = GetSingleArgStringOrTable(n, t);
236241
if (node.GetTokenKind(t) == TK_STRING || node.GetTokenKind(t) == TK_LONG_STRING) {
237-
auto lbrace = syntaxNode.GetChildToken('(', t);
242+
auto lbrace = n.GetChildToken('(', t);
238243
if (lbrace.IsToken(t)) {
239244
Mark(lbrace, t, TokenStrategy::Remove);
240-
auto rbrace = syntaxNode.GetChildToken(')', t);
245+
auto rbrace = n.GetChildToken(')', t);
241246
Mark(rbrace, t, TokenStrategy::Remove);
242247
}
243248
}
244249

245250
break;
246251
}
247252
case CallArgParentheses::RemoveTableOnly: {
248-
auto node = GetSingleArgStringOrTable(syntaxNode, t);
253+
auto node = GetSingleArgStringOrTable(n, t);
249254
if (node.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
250-
auto lbrace = syntaxNode.GetChildToken('(', t);
255+
auto lbrace = n.GetChildToken('(', t);
251256
if (lbrace.IsToken(t)) {
252257
Mark(lbrace, t, TokenStrategy::Remove);
253-
auto rbrace = syntaxNode.GetChildToken(')', t);
258+
auto rbrace = n.GetChildToken(')', t);
254259
Mark(rbrace, t, TokenStrategy::Remove);
255260
}
256261
}
@@ -264,7 +269,20 @@ void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode &syntaxN
264269

265270
auto spaceAnalyzer = f.GetAnalyzer<SpaceAnalyzer>();
266271
if (spaceAnalyzer) {
267-
spaceAnalyzer->FunctionCallSingleArgSpace(f, syntaxNode, t);
272+
spaceAnalyzer->FunctionCallSingleArgSpace(f, n, t);
273+
}
274+
}
275+
}
276+
277+
void TokenAnalyzer::AnalyzeComment(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t) {
278+
auto text = n.GetText(t);
279+
auto pos = 0;
280+
while (pos < text.size() && text[pos] == '-') {
281+
pos++;
282+
}
283+
if (pos == 2 || pos == 3) {
284+
if (pos < text.size() && text[pos] != ' ') {
285+
Mark(n, t, TokenStrategy::SpaceAfterCommentDash);
268286
}
269287
}
270288
}

CodeFormatCore/src/Format/FormatBuilder.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
146146
WriteSyntaxNode(syntaxNode, t);
147147
break;
148148
}
149+
case TokenStrategy::SpaceAfterCommentDash: {
150+
auto text = syntaxNode.GetText(t);
151+
auto pos = 0;
152+
while (pos < text.size() && text[pos] == '-') {
153+
WriteChar('-');
154+
pos++;
155+
}
156+
WriteChar(' ');
157+
WriteText(text.substr(pos));
158+
break;
159+
}
149160
default: {
150161
break;
151162
}

Test/src/FormatStyle_unitest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,4 +2025,24 @@ end
20252025
)",
20262026
style));
20272027

2028+
}
2029+
2030+
TEST(FormatByStyleOption, space_after_comment_dash) {
2031+
LuaStyle style;
2032+
2033+
style.space_after_comment_dash = true;
2034+
EXPECT_TRUE(TestHelper::TestFormatted(
2035+
R"(
2036+
--aa
2037+
---aaa
2038+
---@param
2039+
-------
2040+
)",
2041+
R"(
2042+
-- aa
2043+
--- aaa
2044+
--- @param
2045+
-------
2046+
)",
2047+
style));
20282048
}

lua.template.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ ignore_spaces_inside_function_call = false
7676
# detail number or 'keep'
7777
space_before_inline_comment = 1
7878

79+
# convert '---' to '--- ' or '--' to '-- '
80+
space_after_comment_dash = false
81+
7982
# [operator space]
8083
space_around_math_operator = true
8184
# space_around_math_operator.exponent = false

0 commit comments

Comments
 (0)