From 533e2cdd789f59779034a90ddff92536129e0644 Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Mon, 8 Apr 2024 11:01:04 +0800 Subject: [PATCH] option space_after_comment_dash --- .../include/CodeFormatCore/Config/LuaStyle.h | 2 + .../Format/Analyzer/FormatStrategy.h | 3 +- .../Format/Analyzer/TokenAnalyzer.h | 5 +- CodeFormatCore/src/Config/LuaStyle.cpp | 2 + .../src/Format/Analyzer/TokenAnalyzer.cpp | 68 ++++++++++++------- CodeFormatCore/src/Format/FormatBuilder.cpp | 11 +++ Test/src/FormatStyle_unitest.cpp | 20 ++++++ lua.template.editorconfig | 3 + 8 files changed, 86 insertions(+), 28 deletions(-) diff --git a/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h b/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h index eb35ed84..1e4f4848 100644 --- a/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h +++ b/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h @@ -79,6 +79,8 @@ class LuaStyle { SpaceBeforeInlineComment space_before_inline_comment; + bool space_after_comment_dash = false; + // [operator space] SpaceAroundMath space_around_math_operator; diff --git a/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/FormatStrategy.h b/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/FormatStrategy.h index bf066574..8a3a974e 100644 --- a/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/FormatStrategy.h +++ b/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/FormatStrategy.h @@ -26,7 +26,8 @@ enum class TokenStrategy { TableSepComma, OriginRange, StmtEndSemicolon, - NewLineBeforeToken + NewLineBeforeToken, + SpaceAfterCommentDash }; enum class TokenAddStrategy { diff --git a/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/TokenAnalyzer.h b/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/TokenAnalyzer.h index f12cd23b..ecdd1a32 100644 --- a/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/TokenAnalyzer.h +++ b/CodeFormatCore/include/CodeFormatCore/Format/Analyzer/TokenAnalyzer.h @@ -22,10 +22,11 @@ class TokenAnalyzer : public FormatAnalyzer { private: void TableFieldAddSep(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t); - void AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t); + void AnalyzeTableField(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t); - void AnalyzeCallExpression(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t); + void AnalyzeCallExpression(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t); + void AnalyzeComment(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t); std::unordered_map _tokenStrategies; std::unordered_map _tokenAddStrategies; diff --git a/CodeFormatCore/src/Config/LuaStyle.cpp b/CodeFormatCore/src/Config/LuaStyle.cpp index 3e9b9bf0..7e0bb431 100644 --- a/CodeFormatCore/src/Config/LuaStyle.cpp +++ b/CodeFormatCore/src/Config/LuaStyle.cpp @@ -226,6 +226,8 @@ void LuaStyle::Parse(std::map> &configMap) } } + BOOL_OPTION(space_after_comment_dash) + IF_EXIST(space_around_math_operator) { if (value == "true" || value == "always") { space_around_math_operator.SetAll(true); diff --git a/CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp b/CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp index 61f44423..705a41ed 100644 --- a/CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp +++ b/CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp @@ -49,6 +49,11 @@ void TokenAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) { break; } + case TK_SHORT_COMMENT: { + if (f.GetStyle().space_after_comment_dash) { + AnalyzeComment(f, syntaxNode, t); + } + } default: { break; } @@ -111,28 +116,28 @@ void TokenAnalyzer::TableFieldAddSep(FormatState &f, LuaSyntaxNode n, const LuaS } } -void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) { +void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t) { if (f.GetStyle().table_separator_style == TableSeparatorStyle::Semicolon) { - auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); + auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); auto comma = sep.GetChildToken(',', t); if (comma.IsToken(t)) { Mark(comma, t, TokenStrategy::TableSepSemicolon); } } else if (f.GetStyle().table_separator_style == TableSeparatorStyle::Comma) { - auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); + auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); auto semicolon = sep.GetChildToken(';', t); if (semicolon.IsToken(t)) { Mark(semicolon, t, TokenStrategy::TableSepComma); } } else if (f.GetStyle().table_separator_style == TableSeparatorStyle::OnlyKVColon) { - if (syntaxNode.GetChildToken('=', t).IsToken(t)) { - auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); + if (n.GetChildToken('=', t).IsToken(t)) { + auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); auto semicolon = sep.GetChildToken(',', t); if (semicolon.IsToken(t)) { Mark(semicolon, t, TokenStrategy::TableSepSemicolon); } } else { - auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); + auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); auto semicolon = sep.GetChildToken(';', t); if (semicolon.IsToken(t)) { Mark(semicolon, t, TokenStrategy::TableSepComma); @@ -141,28 +146,28 @@ void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode, } if (f.GetStyle().trailing_table_separator != TrailingTableSeparator::Keep) { - auto nextToken = syntaxNode.GetNextTokenSkipComment(t); + auto nextToken = n.GetNextTokenSkipComment(t); // the last table field if (nextToken.GetTokenKind(t) == '}') { switch (f.GetStyle().trailing_table_separator) { case TrailingTableSeparator::Never: { - auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); + auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); auto sepToken = sep.GetFirstToken(t); Mark(sepToken, t, TokenStrategy::Remove); break; } case TrailingTableSeparator::Always: { - TableFieldAddSep(f, syntaxNode, t); + TableFieldAddSep(f, n, t); break; } case TrailingTableSeparator::Smart: { - auto tableFieldList = syntaxNode.GetParent(t); + auto tableFieldList = n.GetParent(t); if (tableFieldList.GetEndLine(t) == nextToken.GetStartLine(t)) { - auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); + auto sep = n.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t); auto sepToken = sep.GetFirstToken(t); Mark(sepToken, t, TokenStrategy::Remove); } else { - TableFieldAddSep(f, syntaxNode, t); + TableFieldAddSep(f, n, t); } break; } @@ -174,8 +179,8 @@ void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode, } } -bool IsSingleTableOrStringArg(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) { - auto children = syntaxNode.GetChildren(t); +bool IsSingleTableOrStringArg(LuaSyntaxNode n, const LuaSyntaxTree &t) { + auto children = n.GetChildren(t); for (auto child: children) { if (child.GetTokenKind(t) == TK_STRING || child.GetTokenKind(t) == TK_LONG_STRING || child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) { return true; @@ -218,26 +223,26 @@ LuaSyntaxNode GetSingleArgStringOrTable(LuaSyntaxNode &syntaxNode, const LuaSynt return LuaSyntaxNode(0); } -void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) { - if (IsSingleTableOrStringArg(syntaxNode, t)) { +void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t) { + if (IsSingleTableOrStringArg(n, t)) { switch (f.GetStyle().call_arg_parentheses) { case CallArgParentheses::Remove: { - auto lbrace = syntaxNode.GetChildToken('(', t); + auto lbrace = n.GetChildToken('(', t); if (lbrace.IsToken(t)) { Mark(lbrace, t, TokenStrategy::Remove); - auto rbrace = syntaxNode.GetChildToken(')', t); + auto rbrace = n.GetChildToken(')', t); Mark(rbrace, t, TokenStrategy::Remove); } break; } case CallArgParentheses::RemoveStringOnly: { - auto node = GetSingleArgStringOrTable(syntaxNode, t); + auto node = GetSingleArgStringOrTable(n, t); if (node.GetTokenKind(t) == TK_STRING || node.GetTokenKind(t) == TK_LONG_STRING) { - auto lbrace = syntaxNode.GetChildToken('(', t); + auto lbrace = n.GetChildToken('(', t); if (lbrace.IsToken(t)) { Mark(lbrace, t, TokenStrategy::Remove); - auto rbrace = syntaxNode.GetChildToken(')', t); + auto rbrace = n.GetChildToken(')', t); Mark(rbrace, t, TokenStrategy::Remove); } } @@ -245,12 +250,12 @@ void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode &syntaxN break; } case CallArgParentheses::RemoveTableOnly: { - auto node = GetSingleArgStringOrTable(syntaxNode, t); + auto node = GetSingleArgStringOrTable(n, t); if (node.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) { - auto lbrace = syntaxNode.GetChildToken('(', t); + auto lbrace = n.GetChildToken('(', t); if (lbrace.IsToken(t)) { Mark(lbrace, t, TokenStrategy::Remove); - auto rbrace = syntaxNode.GetChildToken(')', t); + auto rbrace = n.GetChildToken(')', t); Mark(rbrace, t, TokenStrategy::Remove); } } @@ -264,7 +269,20 @@ void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode &syntaxN auto spaceAnalyzer = f.GetAnalyzer(); if (spaceAnalyzer) { - spaceAnalyzer->FunctionCallSingleArgSpace(f, syntaxNode, t); + spaceAnalyzer->FunctionCallSingleArgSpace(f, n, t); + } + } +} + +void TokenAnalyzer::AnalyzeComment(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t) { + auto text = n.GetText(t); + auto pos = 0; + while (pos < text.size() && text[pos] == '-') { + pos++; + } + if (pos == 2 || pos == 3) { + if (pos < text.size() && text[pos] != ' ') { + Mark(n, t, TokenStrategy::SpaceAfterCommentDash); } } } diff --git a/CodeFormatCore/src/Format/FormatBuilder.cpp b/CodeFormatCore/src/Format/FormatBuilder.cpp index 65c8bb4e..2e7c4570 100644 --- a/CodeFormatCore/src/Format/FormatBuilder.cpp +++ b/CodeFormatCore/src/Format/FormatBuilder.cpp @@ -146,6 +146,17 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, WriteSyntaxNode(syntaxNode, t); break; } + case TokenStrategy::SpaceAfterCommentDash: { + auto text = syntaxNode.GetText(t); + auto pos = 0; + while (pos < text.size() && text[pos] == '-') { + WriteChar('-'); + pos++; + } + WriteChar(' '); + WriteText(text.substr(pos)); + break; + } default: { break; } diff --git a/Test/src/FormatStyle_unitest.cpp b/Test/src/FormatStyle_unitest.cpp index c6967762..8cdbabd4 100644 --- a/Test/src/FormatStyle_unitest.cpp +++ b/Test/src/FormatStyle_unitest.cpp @@ -2025,4 +2025,24 @@ end )", style)); +} + +TEST(FormatByStyleOption, space_after_comment_dash) { + LuaStyle style; + + style.space_after_comment_dash = true; + EXPECT_TRUE(TestHelper::TestFormatted( + R"( +--aa +---aaa +---@param +------- +)", + R"( +-- aa +--- aaa +--- @param +------- +)", + style)); } \ No newline at end of file diff --git a/lua.template.editorconfig b/lua.template.editorconfig index 680d839b..f2850e00 100644 --- a/lua.template.editorconfig +++ b/lua.template.editorconfig @@ -76,6 +76,9 @@ ignore_spaces_inside_function_call = false # detail number or 'keep' space_before_inline_comment = 1 +# convert '---' to '--- ' or '--' to '-- ' +space_after_comment_dash = false + # [operator space] space_around_math_operator = true # space_around_math_operator.exponent = false