Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Aug 13, 2024
2 parents b9401ce + abdea2d commit 22e1bc9
Show file tree
Hide file tree
Showing 17 changed files with 426 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
include:
- { os: ubuntu-20.04, target: linux, platform: linux-x64 }
- { os: ubuntu-22.04, target: linux, platform: linux-x64 }
- { os: ubuntu-22.04, target: linux, platform: linux-aarch64 }
- { os: macos-latest, target: darwin, platform: darwin-x64 }
- { os: macos-latest, target: darwin, platform: darwin-arm64 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ class LuaDiagnosticStyle {
std::vector<NameStyleRule> const_variable_name_style = {
NameStyleRule(NameStyleType::SnakeCase),
NameStyleRule(NameStyleType::UpperSnakeCase)};

std::vector<NameStyleRule> module_local_name_style = {NameStyleRule(NameStyleType::SnakeCase)};
};
4 changes: 2 additions & 2 deletions CodeFormatCore/include/CodeFormatCore/Config/LuaStyleEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ enum class CallArgParentheses : int {
Remove,
RemoveStringOnly,
RemoveTableOnly,
// 应该没人用
//UnambiguousRemoveStringOnly
Always

};

enum class ContinuousAlign {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ enum class NameDefineType {
ImportModuleName,
ModuleDefineName,
TableFieldDefineName,
ConstVariableName
ConstVariableName,
ModuleLocalVariableName
};

struct NameStyleInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ enum class TokenStrategy {
OriginRange,
StmtEndSemicolon,
NewLineBeforeToken,

WithParentheses,
WithLeftParentheses,
WithRightParentheses,
SpaceAfterCommentDash
};

enum class TokenAddStrategy {
None,
TableAddColon,
TableAddComma,
StmtEndSemicolon
StmtEndSemicolon,
};

enum class IndentStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SpaceAnalyzer : public FormatAnalyzer {

enum class SpacePriority : std::size_t {
Normal = 0,
CommentFirst,
First,
};

SpaceAnalyzer();
Expand Down
18 changes: 17 additions & 1 deletion CodeFormatCore/src/Config/LuaDiagnosticStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
{global_variable_name_style, "global_variable_name_style"},
{module_name_style, "module_name_style" },
{require_module_name_style, "require_module_name_style" },
{class_name_style, "class_name_style" }
{class_name_style, "class_name_style" },
{const_variable_name_style, "const_variable_name_style" },
{module_local_name_style, "module_local_name_style" }
};
for (auto &pair: name_styles) {
if (auto n = root.GetValue(pair.second); !n.IsNull()) {
Expand All @@ -118,4 +120,18 @@ void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
}
}
}
// module_local_name_style should fallback on local_name_style if not defined
if (root.GetValue("module_local_name_style").IsNull() && !root.GetValue("local_name_style").IsNull()) {
auto moduleLocalNameStyle = std::find_if(name_styles.begin(), name_styles.end(), [&](const std::pair<std::vector<NameStyleRule> &, std::string> &pair) {
return pair.second == "module_local_name_style";
});
auto localNameStyle = std::find_if(name_styles.begin(), name_styles.end(), [&](const std::pair<std::vector<NameStyleRule> &, std::string> &pair) {
return pair.second == "local_name_style";
});

// overwrite the namestyle of module_local_name_style with the namestyle of local_name_style
if (moduleLocalNameStyle != name_styles.end() && localNameStyle != name_styles.end()) {
moduleLocalNameStyle->first = localNameStyle->first;
}
}
}
2 changes: 2 additions & 0 deletions CodeFormatCore/src/Config/LuaStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
call_arg_parentheses = CallArgParentheses::RemoveStringOnly;
} else if (value == "remove_table_only") {
call_arg_parentheses = CallArgParentheses::RemoveTableOnly;
} else if (value == "always") {
call_arg_parentheses = CallArgParentheses::Always;
}
}

Expand Down
15 changes: 15 additions & 0 deletions CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ void NameStyleChecker::CheckInBody(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
}

if (!matchConstRule) {
// check for non-special, non-const variables that are in the outermost scope
if (_scopeStack.size() == 1) {
PushStyleCheck(NameDefineType::ModuleLocalVariableName, name);
break;
}

PushStyleCheck(NameDefineType::LocalVariableName, name);
}
}
Expand Down Expand Up @@ -459,6 +465,15 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
}
break;
}
case NameDefineType::ModuleLocalVariableName: {
if (!matcher.Match(n, t, state.GetDiagnosticStyle().module_local_name_style)) {
d.PushDiagnostic(DiagnosticType::NameStyle,
n.GetTextRange(t),
MakeDiagnosticInfo("ModuleLocalVariableName", n, t,
state.GetDiagnosticStyle().module_local_name_style));
}
break;
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ void LineBreakAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {

} else {
switch (stmt.GetTokenKind(t)) {
case TK_SHORT_COMMENT:
case TK_LONG_COMMENT:
{
auto nextToken = stmt.GetNextToken(t).GetTokenKind(t);
if (nextToken == TK_SHORT_COMMENT) {
break;
}
// Fall through
}
case TK_SHORT_COMMENT:
case TK_SHEBANG: {
BreakAfter(stmt, t, style.line_space_after_comment);
break;
Expand Down
18 changes: 9 additions & 9 deletions CodeFormatCore/src/Format/Analyzer/SpaceAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
case TK_LONG_COMMENT:
case TK_SHORT_COMMENT: {
if (f.GetStyle().space_before_inline_comment.Style == SpaceBeforeInlineCommentStyle::Fixed) {
SpaceLeft(syntaxNode, t, f.GetStyle().space_before_inline_comment.Space, SpacePriority::CommentFirst);
SpaceLeft(syntaxNode, t, f.GetStyle().space_before_inline_comment.Space, SpacePriority::First);
} else {
auto prevToken = syntaxNode.GetPrevToken(t);
if (prevToken.GetEndLine(t) == syntaxNode.GetStartLine(t)) {
auto space = syntaxNode.GetStartCol(t) - prevToken.GetEndCol(t) - 1;
SpaceLeft(syntaxNode, t, space, SpacePriority::CommentFirst);
SpaceLeft(syntaxNode, t, space, SpacePriority::First);
} else {
SpaceLeft(syntaxNode, t, 0, SpacePriority::CommentFirst);
SpaceLeft(syntaxNode, t, 0, SpacePriority::First);
}
}
SpaceRight(syntaxNode, t, 1);
Expand Down Expand Up @@ -421,15 +421,15 @@ void SpaceAnalyzer::FunctionCallSingleArgSpace(FormatState &f, LuaSyntaxNode n,
firstToken.GetTokenKind(t) == TK_LONG_STRING) {
switch (f.GetStyle().space_before_function_call_single_arg.string) {
case FunctionSingleArgSpace::None: {
SpaceLeft(n, t, 0);
SpaceLeft(firstToken, t, 0);
break;
}
case FunctionSingleArgSpace::Always: {
SpaceLeft(n, t, 1);
SpaceLeft(firstToken, t, 1);
break;
}
case FunctionSingleArgSpace::Keep: {
SpaceIgnore(n);
SpaceIgnore(firstToken);
break;
}
default: {
Expand All @@ -439,15 +439,15 @@ void SpaceAnalyzer::FunctionCallSingleArgSpace(FormatState &f, LuaSyntaxNode n,
} else {
switch (f.GetStyle().space_before_function_call_single_arg.table) {
case FunctionSingleArgSpace::None: {
SpaceLeft(n, t, 0);
SpaceLeft(firstToken, t, 0);
break;
}
case FunctionSingleArgSpace::Always: {
SpaceLeft(n, t, 1);
SpaceLeft(firstToken, t, 1);
break;
}
case FunctionSingleArgSpace::Keep: {
SpaceIgnore(n);
SpaceIgnore(firstToken);
break;
}
default: {
Expand Down
34 changes: 29 additions & 5 deletions CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode n, const Lua
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) {
if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression ||
child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
return true;
} else if (
child.GetSyntaxKind(t) == LuaSyntaxNodeKind::ExpressionList) {
Expand All @@ -200,11 +201,12 @@ bool IsSingleTableOrStringArg(LuaSyntaxNode n, const LuaSyntaxTree &t) {
return false;
}

LuaSyntaxNode GetSingleArgStringOrTable(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
auto children = syntaxNode.GetChildren(t);
LuaSyntaxNode GetSingleArgStringOrTable(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 syntaxNode;
if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression ||
child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
return child;
} else if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::ExpressionList) {
auto exprs = child.GetChildSyntaxNodes(LuaSyntaxMultiKind::Expression, t);
if (exprs.size() == 1) {
Expand Down Expand Up @@ -262,6 +264,28 @@ void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode n, const

break;
}
case CallArgParentheses::Always: {
auto lbrace = n.GetChildToken('(', t);
auto spaceAnalyzer = f.GetAnalyzer<SpaceAnalyzer>();
if (!lbrace.IsToken(t) && spaceAnalyzer) {
auto node = GetSingleArgStringOrTable(n, t);
if (node.IsToken(t)) {
Mark(node, t, TokenStrategy::WithParentheses);
spaceAnalyzer->SpaceAround(node, t, 0, SpaceAnalyzer::SpacePriority::First);
} else if (node.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression) {
Mark(node.GetFirstToken(t), t, TokenStrategy::WithParentheses);
spaceAnalyzer->SpaceLeft(node.GetFirstToken(t), t, 0, SpaceAnalyzer::SpacePriority::First);
} else {
Mark(node.GetFirstToken(t), t, TokenStrategy::WithLeftParentheses);
spaceAnalyzer->SpaceLeft(node.GetFirstToken(t), t, 0, SpaceAnalyzer::SpacePriority::First);
Mark(node.GetLastToken(t), t, TokenStrategy::WithRightParentheses);
}

return;
}

break;
}
default: {
break;
}
Expand Down
16 changes: 16 additions & 0 deletions CodeFormatCore/src/Format/FormatBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
WriteSyntaxNode(syntaxNode, t);
break;
}
case TokenStrategy::WithParentheses: {
WriteChar('(');
WriteSyntaxNode(syntaxNode, t);
WriteChar(')');
break;
}
case TokenStrategy::WithLeftParentheses:{
WriteChar('(');
WriteSyntaxNode(syntaxNode, t);
break;
}
case TokenStrategy::WithRightParentheses: {
WriteSyntaxNode(syntaxNode, t);
WriteChar(')');
break;
}
case TokenStrategy::SpaceAfterCommentDash: {
auto text = syntaxNode.GetText(t);
std::size_t pos = 0;
Expand Down
28 changes: 28 additions & 0 deletions Test/src/FormatResult_unitest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,31 @@ only_latin = {
}
)", style));
}

TEST(Format, issue_170) {
EXPECT_TRUE(TestHelper::TestFormatted(
R"(
--[[]]
)",
R"(
--[[]]
)"));
EXPECT_TRUE(TestHelper::TestFormatted(
R"(
--[[]]--
)",
R"(
--[[]] --
)"));
EXPECT_TRUE(TestHelper::TestFormatted(
R"(
--[[-----------
]]-------------
)",
R"(
--[[-----------
]] -------------
)"));
}
8 changes: 3 additions & 5 deletions Test2/src/FormatTest2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

int main() {
std::string buffer = R"(
with_latin_extended_a = {
{ 'aaaa', nil },
{ 'ő', nil }, -- U+0151
{ 'ű', nil }, -- U+0171
}
require "check"
)";

auto file = std::make_shared<LuaSource>(std::move(buffer));
Expand Down
Loading

0 comments on commit 22e1bc9

Please sign in to comment.