Skip to content

Commit 70cc120

Browse files
committed
support allow_non_indented_comments, FIX #162
1 parent 0f816cd commit 70cc120

File tree

10 files changed

+104
-38
lines changed

10 files changed

+104
-38
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class LuaStyle {
127127
*/
128128
bool keep_indents_on_empty_lines = false;
129129

130+
bool allow_non_indented_comments = false;
130131
// [line space]
131132

132133
LineSpace line_space_after_if_statement = LineSpace(LineSpaceType::Keep);

CodeFormatCore/include/CodeFormatCore/Diagnostic/CodeStyle/CodeStyleChecker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CodeStyleChecker {
3232
const LuaSyntaxTree &t,
3333
DiagnosticBuilder &d);
3434

35-
std::string GetIndentNote(IndentState indent, IndentStyle style);
35+
std::string GetIndentNote(IndentState indent, std::size_t checkSpace, std::size_t checkTab, IndentStyle style);
3636

3737
std::string GetAdditionalNote(LuaSyntaxNode &left, LuaSyntaxNode &right, const LuaSyntaxTree &t);
3838

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ enum class IndentType {
5050
InvertIndentation,
5151
WhenNewLine,
5252
WhenPrevIndent,
53-
WhenExceedLinebreak
53+
WhenExceedLinebreak,
54+
Keep,
5455
};
5556

5657
struct IndentData {

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/IndentationAnalyzer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class IndentationAnalyzer : public FormatAnalyzer {
4848

4949
void ProcessExceedLinebreak(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t);
5050

51+
void NeverIndentCommentOnIfBranch(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t);
52+
53+
void AllowNonIndentedComment(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t);
54+
5155
std::unordered_map<std::size_t, IndentData> _indent;
5256
std::unordered_set<std::size_t> _indentMark;
5357

CodeFormatCore/src/Config/LuaStyle.cpp

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

331331
BOOL_OPTION(keep_indents_on_empty_lines)
332332

333+
BOOL_OPTION(allow_non_indented_comments)
334+
333335
std::vector<std::pair<std::string, LineSpace &>> fieldList = {
334336
{"line_space_after_if_statement", line_space_after_if_statement },
335337
{"line_space_after_do_statement", line_space_after_do_statement },

CodeFormatCore/src/Diagnostic/CodeStyle/CodeStyleChecker.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,17 @@ void CodeStyleChecker::ProcessIndentDiagnostic(LuaSyntaxNode &node, const LuaSyn
268268
}
269269

270270
if (indent.TabSize != checkIndent.Tab || indent.SpaceSize != checkIndent.Space) {
271+
// if(state.GetStyle().indent_style)
271272
d.PushDiagnostic(DiagnosticType::Indent,
272273
currentIndentRange,
273-
util::format("{}, found {} whitespace, {} tab",
274-
GetIndentNote(indent, state.GetStyle().indent_style),
275-
checkIndent.Space, checkIndent.Tab));
274+
GetIndentNote(indent,checkIndent.Space, checkIndent.Tab, state.GetStyle().indent_style));
276275
}
277276
}
278277

279-
std::string CodeStyleChecker::GetIndentNote(IndentState indent, IndentStyle style) {
278+
std::string CodeStyleChecker::GetIndentNote(IndentState indent, std::size_t checkSpace, std::size_t checkTab, IndentStyle style) {
280279
if (style == IndentStyle::Tab) {
281-
return util::format("expected {} tab indent", indent.TabSize);
280+
return util::format("expected {} tab indent, found {} tab", indent.TabSize, checkTab);
282281
} else {
283-
return util::format("expected {} whitespace indent", indent.SpaceSize);
282+
return util::format("expected {} whitespace indent, found {} whitespace", indent.SpaceSize, checkSpace);
284283
}
285284
}

CodeFormatCore/src/Format/Analyzer/IndentationAnalyzer.cpp

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,10 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
1818
case LuaSyntaxNodeKind::Block: {
1919
AddIndenter(syntaxNode, t);
2020
if (f.GetStyle().never_indent_comment_on_if_branch) {
21-
auto ifStmt = syntaxNode.GetParent(t);
22-
if (ifStmt.GetSyntaxKind(t) == LuaSyntaxNodeKind::IfStatement) {
23-
auto ifBranch = syntaxNode.GetNextToken(t);
24-
if (ifBranch.GetTokenKind(t) == TK_ELSEIF || ifBranch.GetTokenKind(t) == TK_ELSE) {
25-
auto bodyChildren = syntaxNode.GetChildren(t);
26-
bool isCommentOnly = true;
27-
for (auto bodyChild: bodyChildren) {
28-
if (bodyChild.IsNode(t)) {
29-
isCommentOnly = false;
30-
break;
31-
}
32-
}
33-
if (isCommentOnly) {
34-
break;
35-
}
36-
std::size_t siblingLine = ifBranch.GetStartLine(t);
37-
for (auto it = bodyChildren.rbegin(); it != bodyChildren.rend(); it++) {
38-
auto n = *it;
39-
if (n.GetTokenKind(t) != TK_SHORT_COMMENT) {
40-
break;
41-
}
42-
auto commentLine = n.GetStartLine(t);
43-
if (commentLine + 1 == siblingLine) {
44-
AddIndenter(n, t, IndentData(IndentType::InvertIndentation));
45-
siblingLine = commentLine;
46-
}
47-
}
48-
}
49-
}
21+
NeverIndentCommentOnIfBranch(f, syntaxNode, t);
22+
}
23+
if (f.GetStyle().allow_non_indented_comments) {
24+
AllowNonIndentedComment(f, syntaxNode, t);
5025
}
5126
break;
5227
}
@@ -176,6 +151,10 @@ void IndentationAnalyzer::Query(FormatState &f, LuaSyntaxNode n, const LuaSyntax
176151
}
177152
break;
178153
}
154+
case IndentType::Keep: {
155+
resolve.SetIndent(0, IndentStrategy::Absolute);
156+
break;
157+
}
179158
default: {
180159
break;
181160
}
@@ -354,3 +333,45 @@ void IndentationAnalyzer::ProcessExceedLinebreak(FormatState &f, LuaSyntaxNode s
354333
AddIndenter(c, t, IndentData(IndentType::WhenNewLine, group.Indent));
355334
}
356335
}
336+
337+
void IndentationAnalyzer::NeverIndentCommentOnIfBranch(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t) {
338+
auto ifStmt = syntaxNode.GetParent(t);
339+
if (ifStmt.GetSyntaxKind(t) == LuaSyntaxNodeKind::IfStatement) {
340+
auto ifBranch = syntaxNode.GetNextToken(t);
341+
if (ifBranch.GetTokenKind(t) == TK_ELSEIF || ifBranch.GetTokenKind(t) == TK_ELSE) {
342+
auto bodyChildren = syntaxNode.GetChildren(t);
343+
bool isCommentOnly = true;
344+
for (auto bodyChild: bodyChildren) {
345+
if (bodyChild.IsNode(t)) {
346+
isCommentOnly = false;
347+
break;
348+
}
349+
}
350+
if (isCommentOnly) {
351+
return;
352+
}
353+
std::size_t siblingLine = ifBranch.GetStartLine(t);
354+
for (auto it = bodyChildren.rbegin(); it != bodyChildren.rend(); it++) {
355+
auto n = *it;
356+
if (n.GetTokenKind(t) != TK_SHORT_COMMENT) {
357+
break;
358+
}
359+
auto commentLine = n.GetStartLine(t);
360+
if (commentLine + 1 == siblingLine) {
361+
AddIndenter(n, t, IndentData(IndentType::InvertIndentation));
362+
siblingLine = commentLine;
363+
}
364+
}
365+
}
366+
}
367+
}
368+
369+
void IndentationAnalyzer::AllowNonIndentedComment(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t) {
370+
for (auto n: syntaxNode.GetChildren(t)) {
371+
if (n.IsToken(t) && (n.GetTokenKind(t) == TK_SHORT_COMMENT || n.GetTokenKind(t) == TK_LONG_COMMENT)) {
372+
if (n.GetStartCol(t) == 0) {
373+
AddIndenter(n, t, IndentData(IndentType::Keep));
374+
}
375+
}
376+
}
377+
}

CodeFormatCore/src/Format/FormatState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
173173

174174
if (resolve.GetIndentStrategy() != IndentStrategy::None) {
175175
auto indent = resolve.GetIndent();
176-
if (indent == 0) {
176+
if (indent == 0 && resolve.GetIndentStrategy() != IndentStrategy::Absolute) {
177177
if (_formatStyle.indent_style == IndentStyle::Space) {
178178
indent = _formatStyle.indent_size;
179179
} else {

Test/src/FormatStyle_unitest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,4 +1990,39 @@ local t = true == false or a < 2 and b > 3 or c <= 4 and d >= 5 or e ~= 6 and f
19901990
local t = true==false or a<2 and b>3 or c<=4 and d>=5 or e~=6 and f==7
19911991
)",
19921992
style));
1993+
}
1994+
1995+
TEST(FormatByStyleOption, allow_non_indented_comments) {
1996+
LuaStyle style;
1997+
1998+
style.allow_non_indented_comments = false;
1999+
EXPECT_TRUE(TestHelper::TestFormatted(
2000+
R"(
2001+
function f()
2002+
--wjfowoj
2003+
2004+
local t = 123
2005+
end
2006+
2007+
do
2008+
--12313
2009+
local t = 123
2010+
print(t)
2011+
end
2012+
)",
2013+
R"(
2014+
function f()
2015+
--wjfowoj
2016+
2017+
local t = 123
2018+
end
2019+
2020+
do
2021+
--12313
2022+
local t = 123
2023+
print(t)
2024+
end
2025+
)",
2026+
style));
2027+
19932028
}

lua.template.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ space_before_inline_comment = 1
7878

7979
# [operator space]
8080
space_around_math_operator = true
81+
# space_around_math_operator.exponent = false
8182

8283
space_after_comma = true
8384

@@ -121,6 +122,8 @@ never_indent_before_if_condition = false
121122
never_indent_comment_on_if_branch = false
122123

123124
keep_indents_on_empty_lines = false
125+
126+
allow_non_indented_comments = false
124127
# [line space]
125128

126129
# The following configuration supports four expressions

0 commit comments

Comments
 (0)