Skip to content

Commit

Permalink
support allow_non_indented_comments, FIX #162
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Feb 22, 2024
1 parent 0f816cd commit 70cc120
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 38 deletions.
1 change: 1 addition & 0 deletions CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class LuaStyle {
*/
bool keep_indents_on_empty_lines = false;

bool allow_non_indented_comments = false;
// [line space]

LineSpace line_space_after_if_statement = LineSpace(LineSpaceType::Keep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CodeStyleChecker {
const LuaSyntaxTree &t,
DiagnosticBuilder &d);

std::string GetIndentNote(IndentState indent, IndentStyle style);
std::string GetIndentNote(IndentState indent, std::size_t checkSpace, std::size_t checkTab, IndentStyle style);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ enum class IndentType {
InvertIndentation,
WhenNewLine,
WhenPrevIndent,
WhenExceedLinebreak
WhenExceedLinebreak,
Keep,
};

struct IndentData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class IndentationAnalyzer : public FormatAnalyzer {

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

void NeverIndentCommentOnIfBranch(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t);

void AllowNonIndentedComment(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t);

std::unordered_map<std::size_t, IndentData> _indent;
std::unordered_set<std::size_t> _indentMark;

Expand Down
2 changes: 2 additions & 0 deletions CodeFormatCore/src/Config/LuaStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)

BOOL_OPTION(keep_indents_on_empty_lines)

BOOL_OPTION(allow_non_indented_comments)

std::vector<std::pair<std::string, LineSpace &>> fieldList = {
{"line_space_after_if_statement", line_space_after_if_statement },
{"line_space_after_do_statement", line_space_after_do_statement },
Expand Down
11 changes: 5 additions & 6 deletions CodeFormatCore/src/Diagnostic/CodeStyle/CodeStyleChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,17 @@ void CodeStyleChecker::ProcessIndentDiagnostic(LuaSyntaxNode &node, const LuaSyn
}

if (indent.TabSize != checkIndent.Tab || indent.SpaceSize != checkIndent.Space) {
// if(state.GetStyle().indent_style)
d.PushDiagnostic(DiagnosticType::Indent,
currentIndentRange,
util::format("{}, found {} whitespace, {} tab",
GetIndentNote(indent, state.GetStyle().indent_style),
checkIndent.Space, checkIndent.Tab));
GetIndentNote(indent,checkIndent.Space, checkIndent.Tab, state.GetStyle().indent_style));
}
}

std::string CodeStyleChecker::GetIndentNote(IndentState indent, IndentStyle style) {
std::string CodeStyleChecker::GetIndentNote(IndentState indent, std::size_t checkSpace, std::size_t checkTab, IndentStyle style) {
if (style == IndentStyle::Tab) {
return util::format("expected {} tab indent", indent.TabSize);
return util::format("expected {} tab indent, found {} tab", indent.TabSize, checkTab);
} else {
return util::format("expected {} whitespace indent", indent.SpaceSize);
return util::format("expected {} whitespace indent, found {} whitespace", indent.SpaceSize, checkSpace);
}
}
79 changes: 50 additions & 29 deletions CodeFormatCore/src/Format/Analyzer/IndentationAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,10 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
case LuaSyntaxNodeKind::Block: {
AddIndenter(syntaxNode, t);
if (f.GetStyle().never_indent_comment_on_if_branch) {
auto ifStmt = syntaxNode.GetParent(t);
if (ifStmt.GetSyntaxKind(t) == LuaSyntaxNodeKind::IfStatement) {
auto ifBranch = syntaxNode.GetNextToken(t);
if (ifBranch.GetTokenKind(t) == TK_ELSEIF || ifBranch.GetTokenKind(t) == TK_ELSE) {
auto bodyChildren = syntaxNode.GetChildren(t);
bool isCommentOnly = true;
for (auto bodyChild: bodyChildren) {
if (bodyChild.IsNode(t)) {
isCommentOnly = false;
break;
}
}
if (isCommentOnly) {
break;
}
std::size_t siblingLine = ifBranch.GetStartLine(t);
for (auto it = bodyChildren.rbegin(); it != bodyChildren.rend(); it++) {
auto n = *it;
if (n.GetTokenKind(t) != TK_SHORT_COMMENT) {
break;
}
auto commentLine = n.GetStartLine(t);
if (commentLine + 1 == siblingLine) {
AddIndenter(n, t, IndentData(IndentType::InvertIndentation));
siblingLine = commentLine;
}
}
}
}
NeverIndentCommentOnIfBranch(f, syntaxNode, t);
}
if (f.GetStyle().allow_non_indented_comments) {
AllowNonIndentedComment(f, syntaxNode, t);
}
break;
}
Expand Down Expand Up @@ -176,6 +151,10 @@ void IndentationAnalyzer::Query(FormatState &f, LuaSyntaxNode n, const LuaSyntax
}
break;
}
case IndentType::Keep: {
resolve.SetIndent(0, IndentStrategy::Absolute);
break;
}
default: {
break;
}
Expand Down Expand Up @@ -354,3 +333,45 @@ void IndentationAnalyzer::ProcessExceedLinebreak(FormatState &f, LuaSyntaxNode s
AddIndenter(c, t, IndentData(IndentType::WhenNewLine, group.Indent));
}
}

void IndentationAnalyzer::NeverIndentCommentOnIfBranch(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t) {
auto ifStmt = syntaxNode.GetParent(t);
if (ifStmt.GetSyntaxKind(t) == LuaSyntaxNodeKind::IfStatement) {
auto ifBranch = syntaxNode.GetNextToken(t);
if (ifBranch.GetTokenKind(t) == TK_ELSEIF || ifBranch.GetTokenKind(t) == TK_ELSE) {
auto bodyChildren = syntaxNode.GetChildren(t);
bool isCommentOnly = true;
for (auto bodyChild: bodyChildren) {
if (bodyChild.IsNode(t)) {
isCommentOnly = false;
break;
}
}
if (isCommentOnly) {
return;
}
std::size_t siblingLine = ifBranch.GetStartLine(t);
for (auto it = bodyChildren.rbegin(); it != bodyChildren.rend(); it++) {
auto n = *it;
if (n.GetTokenKind(t) != TK_SHORT_COMMENT) {
break;
}
auto commentLine = n.GetStartLine(t);
if (commentLine + 1 == siblingLine) {
AddIndenter(n, t, IndentData(IndentType::InvertIndentation));
siblingLine = commentLine;
}
}
}
}
}

void IndentationAnalyzer::AllowNonIndentedComment(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t) {
for (auto n: syntaxNode.GetChildren(t)) {
if (n.IsToken(t) && (n.GetTokenKind(t) == TK_SHORT_COMMENT || n.GetTokenKind(t) == TK_LONG_COMMENT)) {
if (n.GetStartCol(t) == 0) {
AddIndenter(n, t, IndentData(IndentType::Keep));
}
}
}
}
2 changes: 1 addition & 1 deletion CodeFormatCore/src/Format/FormatState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,

if (resolve.GetIndentStrategy() != IndentStrategy::None) {
auto indent = resolve.GetIndent();
if (indent == 0) {
if (indent == 0 && resolve.GetIndentStrategy() != IndentStrategy::Absolute) {
if (_formatStyle.indent_style == IndentStyle::Space) {
indent = _formatStyle.indent_size;
} else {
Expand Down
35 changes: 35 additions & 0 deletions Test/src/FormatStyle_unitest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
local t = true==false or a<2 and b>3 or c<=4 and d>=5 or e~=6 and f==7
)",
style));
}

TEST(FormatByStyleOption, allow_non_indented_comments) {
LuaStyle style;

style.allow_non_indented_comments = false;
EXPECT_TRUE(TestHelper::TestFormatted(
R"(
function f()
--wjfowoj
local t = 123
end
do
--12313
local t = 123
print(t)
end
)",
R"(
function f()
--wjfowoj
local t = 123
end
do
--12313
local t = 123
print(t)
end
)",
style));

}
3 changes: 3 additions & 0 deletions lua.template.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ space_before_inline_comment = 1

# [operator space]
space_around_math_operator = true
# space_around_math_operator.exponent = false

space_after_comma = true

Expand Down Expand Up @@ -121,6 +122,8 @@ never_indent_before_if_condition = false
never_indent_comment_on_if_branch = false

keep_indents_on_empty_lines = false

allow_non_indented_comments = false
# [line space]

# The following configuration supports four expressions
Expand Down

0 comments on commit 70cc120

Please sign in to comment.