Skip to content

Commit

Permalink
impl call_arg_parentheses = always
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jun 4, 2024
1 parent a8bf11b commit 5b5f544
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum class TokenStrategy {

WithParentheses,
WithLeftParentheses,
WithRightParentheses
WithRightParentheses,
SpaceAfterCommentDash
};

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
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
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
20 changes: 14 additions & 6 deletions CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ 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 ||
if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression ||
child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
return true;
} else if (
Expand All @@ -201,12 +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 ||
if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression ||
child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
return syntaxNode;
return child;
} else if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::ExpressionList) {
auto exprs = child.GetChildSyntaxNodes(LuaSyntaxMultiKind::Expression, t);
if (exprs.size() == 1) {
Expand Down Expand Up @@ -266,14 +266,22 @@ void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode n, const
}
case CallArgParentheses::Always: {
auto lbrace = n.GetChildToken('(', t);
if (!lbrace.IsToken(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;
Expand Down
4 changes: 3 additions & 1 deletion CodeFormatCore/src/Format/FormatBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
WriteSyntaxNode(syntaxNode, t);
break;
}
case TokenStrategy::WithRightParentheses:{
case TokenStrategy::WithRightParentheses: {
WriteSyntaxNode(syntaxNode, t);
WriteChar(')');
break;
}
case TokenStrategy::SpaceAfterCommentDash: {
auto text = syntaxNode.GetText(t);
std::size_t pos = 0;
Expand Down
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

0 comments on commit 5b5f544

Please sign in to comment.