Skip to content

Commit 5b5f544

Browse files
committed
impl call_arg_parentheses = always
1 parent a8bf11b commit 5b5f544

File tree

7 files changed

+33
-23
lines changed

7 files changed

+33
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ enum class TokenStrategy {
3030

3131
WithParentheses,
3232
WithLeftParentheses,
33-
WithRightParentheses
33+
WithRightParentheses,
3434
SpaceAfterCommentDash
3535
};
3636

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/SpaceAnalyzer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SpaceAnalyzer : public FormatAnalyzer {
1010

1111
enum class SpacePriority : std::size_t {
1212
Normal = 0,
13-
CommentFirst,
13+
First,
1414
};
1515

1616
SpaceAnalyzer();

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
131131
call_arg_parentheses = CallArgParentheses::RemoveStringOnly;
132132
} else if (value == "remove_table_only") {
133133
call_arg_parentheses = CallArgParentheses::RemoveTableOnly;
134+
} else if (value == "always") {
135+
call_arg_parentheses = CallArgParentheses::Always;
134136
}
135137
}
136138

CodeFormatCore/src/Format/Analyzer/SpaceAnalyzer.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
181181
case TK_LONG_COMMENT:
182182
case TK_SHORT_COMMENT: {
183183
if (f.GetStyle().space_before_inline_comment.Style == SpaceBeforeInlineCommentStyle::Fixed) {
184-
SpaceLeft(syntaxNode, t, f.GetStyle().space_before_inline_comment.Space, SpacePriority::CommentFirst);
184+
SpaceLeft(syntaxNode, t, f.GetStyle().space_before_inline_comment.Space, SpacePriority::First);
185185
} else {
186186
auto prevToken = syntaxNode.GetPrevToken(t);
187187
if (prevToken.GetEndLine(t) == syntaxNode.GetStartLine(t)) {
188188
auto space = syntaxNode.GetStartCol(t) - prevToken.GetEndCol(t) - 1;
189-
SpaceLeft(syntaxNode, t, space, SpacePriority::CommentFirst);
189+
SpaceLeft(syntaxNode, t, space, SpacePriority::First);
190190
} else {
191-
SpaceLeft(syntaxNode, t, 0, SpacePriority::CommentFirst);
191+
SpaceLeft(syntaxNode, t, 0, SpacePriority::First);
192192
}
193193
}
194194
SpaceRight(syntaxNode, t, 1);
@@ -421,15 +421,15 @@ void SpaceAnalyzer::FunctionCallSingleArgSpace(FormatState &f, LuaSyntaxNode n,
421421
firstToken.GetTokenKind(t) == TK_LONG_STRING) {
422422
switch (f.GetStyle().space_before_function_call_single_arg.string) {
423423
case FunctionSingleArgSpace::None: {
424-
SpaceLeft(n, t, 0);
424+
SpaceLeft(firstToken, t, 0);
425425
break;
426426
}
427427
case FunctionSingleArgSpace::Always: {
428-
SpaceLeft(n, t, 1);
428+
SpaceLeft(firstToken, t, 1);
429429
break;
430430
}
431431
case FunctionSingleArgSpace::Keep: {
432-
SpaceIgnore(n);
432+
SpaceIgnore(firstToken);
433433
break;
434434
}
435435
default: {
@@ -439,15 +439,15 @@ void SpaceAnalyzer::FunctionCallSingleArgSpace(FormatState &f, LuaSyntaxNode n,
439439
} else {
440440
switch (f.GetStyle().space_before_function_call_single_arg.table) {
441441
case FunctionSingleArgSpace::None: {
442-
SpaceLeft(n, t, 0);
442+
SpaceLeft(firstToken, t, 0);
443443
break;
444444
}
445445
case FunctionSingleArgSpace::Always: {
446-
SpaceLeft(n, t, 1);
446+
SpaceLeft(firstToken, t, 1);
447447
break;
448448
}
449449
case FunctionSingleArgSpace::Keep: {
450-
SpaceIgnore(n);
450+
SpaceIgnore(firstToken);
451451
break;
452452
}
453453
default: {

CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode n, const Lua
182182
bool IsSingleTableOrStringArg(LuaSyntaxNode n, const LuaSyntaxTree &t) {
183183
auto children = n.GetChildren(t);
184184
for (auto child: children) {
185-
if (child.GetTokenKind(t) == TK_STRING || child.GetTokenKind(t) == TK_LONG_STRING ||
185+
if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression ||
186186
child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
187187
return true;
188188
} else if (
@@ -201,12 +201,12 @@ bool IsSingleTableOrStringArg(LuaSyntaxNode n, const LuaSyntaxTree &t) {
201201
return false;
202202
}
203203

204-
LuaSyntaxNode GetSingleArgStringOrTable(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
205-
auto children = syntaxNode.GetChildren(t);
204+
LuaSyntaxNode GetSingleArgStringOrTable(LuaSyntaxNode n, const LuaSyntaxTree &t) {
205+
auto children = n.GetChildren(t);
206206
for (auto child: children) {
207-
if (child.GetTokenKind(t) == TK_STRING || child.GetTokenKind(t) == TK_LONG_STRING ||
207+
if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression ||
208208
child.GetSyntaxKind(t) == LuaSyntaxNodeKind::TableExpression) {
209-
return syntaxNode;
209+
return child;
210210
} else if (child.GetSyntaxKind(t) == LuaSyntaxNodeKind::ExpressionList) {
211211
auto exprs = child.GetChildSyntaxNodes(LuaSyntaxMultiKind::Expression, t);
212212
if (exprs.size() == 1) {
@@ -266,14 +266,22 @@ void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode n, const
266266
}
267267
case CallArgParentheses::Always: {
268268
auto lbrace = n.GetChildToken('(', t);
269-
if (!lbrace.IsToken(t)) {
269+
auto spaceAnalyzer = f.GetAnalyzer<SpaceAnalyzer>();
270+
if (!lbrace.IsToken(t) && spaceAnalyzer) {
270271
auto node = GetSingleArgStringOrTable(n, t);
271272
if (node.IsToken(t)) {
272273
Mark(node, t, TokenStrategy::WithParentheses);
274+
spaceAnalyzer->SpaceAround(node, t, 0, SpaceAnalyzer::SpacePriority::First);
275+
} else if (node.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression) {
276+
Mark(node.GetFirstToken(t), t, TokenStrategy::WithParentheses);
277+
spaceAnalyzer->SpaceLeft(node.GetFirstToken(t), t, 0, SpaceAnalyzer::SpacePriority::First);
273278
} else {
274279
Mark(node.GetFirstToken(t), t, TokenStrategy::WithLeftParentheses);
280+
spaceAnalyzer->SpaceLeft(node.GetFirstToken(t), t, 0, SpaceAnalyzer::SpacePriority::First);
275281
Mark(node.GetLastToken(t), t, TokenStrategy::WithRightParentheses);
276282
}
283+
284+
return;
277285
}
278286

279287
break;

CodeFormatCore/src/Format/FormatBuilder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,11 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
157157
WriteSyntaxNode(syntaxNode, t);
158158
break;
159159
}
160-
case TokenStrategy::WithRightParentheses:{
160+
case TokenStrategy::WithRightParentheses: {
161161
WriteSyntaxNode(syntaxNode, t);
162162
WriteChar(')');
163+
break;
164+
}
163165
case TokenStrategy::SpaceAfterCommentDash: {
164166
auto text = syntaxNode.GetText(t);
165167
std::size_t pos = 0;

Test2/src/FormatTest2.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88

99
int main() {
1010
std::string buffer = R"(
11-
with_latin_extended_a = {
12-
{ 'aaaa', nil },
13-
{ 'ő', nil }, -- U+0151
14-
{ 'ű', nil }, -- U+0171
15-
}
11+
12+
require "check"
13+
1614
)";
1715

1816
auto file = std::make_shared<LuaSource>(std::move(buffer));

0 commit comments

Comments
 (0)